用java编写 xml数据转入到mysql中
\\用java编写xml数据转入到mysql中实在难搞小弟用JODM把mysql的表导出到xml文件到反过来就不知道怎么做了各位大哥帮帮忙啦~~给我好代码,小弟万分感谢!...
\\用java编写 xml数据转入到mysql中
实在难搞
小弟用JODM把mysql的表导出到xml文件
到反过来就不知道怎么做了
各位大哥帮帮忙啦~~给我好代码,小弟万分感谢!
最好做到任何xml文件都可以转入到mysql
先不转换任何格式的xml了,就转换一个简单格式的xml,到mysql. 代码是大概是如何,怎样方式?
我要代码啊~~~~啊~~~~~~~~~~~~~~~~~啊~~~~~~~~~~~~ 展开
实在难搞
小弟用JODM把mysql的表导出到xml文件
到反过来就不知道怎么做了
各位大哥帮帮忙啦~~给我好代码,小弟万分感谢!
最好做到任何xml文件都可以转入到mysql
先不转换任何格式的xml了,就转换一个简单格式的xml,到mysql. 代码是大概是如何,怎样方式?
我要代码啊~~~~啊~~~~~~~~~~~~~~~~~啊~~~~~~~~~~~~ 展开
2个回答
展开全部
给你段以前写过的代码,慢慢看看吧!
/*
* Create On: 2005-12-22
* Author: small grass
* corporation: mylawcorp
*/
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XmlWriteDb {
public static void main(String[] args) throws ClassNotFoundException, SQLException, ParserConfigurationException, SAXException, IOException {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:jtds:sqlserver://localhost:1433/pubs","sa","sa");
PreparedStatement stmt = conn.prepareStatement(
"insert into test values(?,?)");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db =dbf.newDocumentBuilder();
Document d = db.parse("c:/test.xml");
NodeList nl = d.getElementsByTagName("root");
for(int i=0;i<nl.getLength();i++) {
Element test = (Element) nl.item(i);
String id = test.getChildNodes().item(1).getFirstChild().getNodeValue();
String name = test.getChildNodes().item(3).getFirstChild().getNodeValue();
stmt.setInt(1,Integer.parseInt(id));
stmt.setString(2,name);
stmt.execute();
}
stmt.close();
conn.close();
}
}
/*
* Create On: 2005-12-22
* Author: small grass
* corporation: mylawcorp
*/
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XmlWriteDb {
public static void main(String[] args) throws ClassNotFoundException, SQLException, ParserConfigurationException, SAXException, IOException {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:jtds:sqlserver://localhost:1433/pubs","sa","sa");
PreparedStatement stmt = conn.prepareStatement(
"insert into test values(?,?)");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db =dbf.newDocumentBuilder();
Document d = db.parse("c:/test.xml");
NodeList nl = d.getElementsByTagName("root");
for(int i=0;i<nl.getLength();i++) {
Element test = (Element) nl.item(i);
String id = test.getChildNodes().item(1).getFirstChild().getNodeValue();
String name = test.getChildNodes().item(3).getFirstChild().getNodeValue();
stmt.setInt(1,Integer.parseInt(id));
stmt.setString(2,name);
stmt.execute();
}
stmt.close();
conn.close();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询