
java怎么把字符串转变为整型
publicstaticvoidmain(String[]args)throwsException{Connectionconn=null;PreparedStateme...
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: <deptno attributes: []/>]"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497) 展开
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: <deptno attributes: []/>]"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497) 展开
7个回答
展开全部
应该在循环中提取元素值的时候出错的。换个方式从person对象提取元素值,你干脆用elementText试试,格式按你的方式转换一下就变成要的int型了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-11-22
展开全部
Integer.paresInt()是将字符串转换为整数,1.25转化时里面有“.”不是整数啊。所以会抛出异常。
Double.Double.parseDouble():将字符串转化为浮点数。
Double.Double.parseDouble():将字符串转化为浮点数。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
pstm.setInt(2, Integer.parseInt((person.element("deptno") + "")));
改成
pstm.setInt(2, Integer.parseInt(person.element("deptno") ));
改成
pstm.setInt(2, Integer.parseInt(person.element("deptno") ));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你调用的方法没错,可是你传进去的字符串格式有问题
你传个 "" 进去了的话,还真没法给你弄个int出来
你传个 "" 进去了的话,还真没法给你弄个int出来
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
同意楼上的,你断点调试,用watch 看看变量是不是一个数字。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询