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)
展开
 我来答
cuichangyang66
2012-11-14 · 超过35用户采纳过TA的回答
知道答主
回答量:97
采纳率:0%
帮助的人:59.9万
展开全部
应该在循环中提取元素值的时候出错的。换个方式从person对象提取元素值,你干脆用elementText试试,格式按你的方式转换一下就变成要的int型了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2012-11-22
展开全部
Integer.paresInt()是将字符串转换为整数,1.25转化时里面有“.”不是整数啊。所以会抛出异常。
Double.Double.parseDouble():将字符串转化为浮点数。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
houseandmouse0
2012-11-15 · 超过32用户采纳过TA的回答
知道答主
回答量:161
采纳率:0%
帮助的人:92万
展开全部
pstm.setInt(2, Integer.parseInt((person.element("deptno") + "")));

改成

pstm.setInt(2, Integer.parseInt(person.element("deptno") ));
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
duzc2
2012-11-14 · TA获得超过2512个赞
知道大有可为答主
回答量:1987
采纳率:50%
帮助的人:2105万
展开全部
你调用的方法没错,可是你传进去的字符串格式有问题
你传个 "" 进去了的话,还真没法给你弄个int出来
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
在双龙洞抽奖的魔芋
2012-11-14
知道小有建树答主
回答量:58
采纳率:0%
帮助的人:20.8万
展开全部
同意楼上的,你断点调试,用watch 看看变量是不是一个数字。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(5)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式