该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
String sql="insert into t1(CreateTime, Question, Category, Place,Type) values (?, ?, ?, ?,?)";
try
{
Class.forName(driverName);//加载JDBC驱动程序
dbConn=DriverManager.getConnection(dbURL,userName,userPwd);//建立驱动程序与数据库之间的链接
Statement stmt=dbConn.createStatement();//利用连接对象dbConn创建Statement接口对象stmt
java.sql.PreparedStatement pstmt=dbConn.prepareStatement(sql);
java.io.OutputStream out = new java.io.FileOutputStream("C:/Users/Administrator/Desktop/data.xml");
java.io.Writer wr = new java.io.OutputStreamWriter(out, "UTF-8");
Document doc=new SAXReader().read("C:/Users/Administrator/Desktop/data.xml");
List itemList=doc.selectNodes("xml");//选择xml文件的节点
for(Iterator iter=itemList.iterator();iter.hasNext();){
Element el=(Element)iter.next();
//读取节点内容
String createtime=el.elementText("CreateTime");
String question = el.elementText("Question");
String category = el.elementText("Category");
String place = el.elementText("Place");
String type = el.elementText("Type");
//为sql语句赋值
pstmt.setString(1, createtime);
pstmt.setString(2, question);
pstmt.setString(3, category);
pstmt.setString(4, place);
pstmt.setString(5, type);
pstmt.addBatch();
}
pstmt.executeBatch();
System.out.print("将XML导入数据库成功");
System.out.print(pstmt);
dbConn.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.print("返回结果失败");
}
}