publicstaticvoidmain(String[]args)throwsException{Connectionconn=null;PreparedStatementpstm=null;Stringsql="insertintodept(dname,deptno,loc)values(?,?,?)";//TODOAuto-gene...
public static void main(String[] args) throws Exception {
Connection conn = null;
PreparedStatement pstm = null;
String sql = "insert into dept(dname,deptno,loc) values(?,?,?)";
// TODO Auto-generated method stub
// 找到文件
File file = new File("d:" + File.separator + "deptdetails.xml");
// 创建一个解析器
SAXReader reader = new SAXReader();
// 创建一个文档
Document doc = null;
try {
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);
pstm = conn.prepareStatement(sql);
// 读取文档
doc = reader.read(file);
} catch (Exception e) {
e.printStackTrace();
}
// 得到根元素
Element dept = doc.getRootElement();
@SuppressWarnings("rawtypes")
// 得到全部子节点
Iterator iter = dept.elementIterator();
while (iter.hasNext()) {
Element person = (Element) iter.next();
pstm.setString(1, person.elementText("dname"));
pstm.setInt(2, Integer.parseInt((person.element("deptno") + "")));
pstm.setInt(2, Integer.valueOf(person.element("deptno") + "").intValue());
// pstm.setInt(2, 0);
pstm.setString(3, person.elementText("loc"));
System.out.println("姓名:" + person.elementText("dname"));
System.out.println("部门编号:" + person.elementText("deptno"));
System.out.println("描述" + person.elementText("loc"));
pstm.executeUpdate();
}
pstm.close();
}
一下是报错的
Exception in thread "main" java.lang.NumberFormatException: For input string: "org.dom4j.tree.DefaultElement@f39b3a [Element: ]"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
展开