我得到了一个从MySQL表中获取整数的方法
public int getAddressID(String postcode) throws SQLException {
String q = "SELECT PK_ADDRESS_ID FROM tbl_addresses WHERE postcode =" + "\"" + postcode + "\"";
System.out.println(q);
ResultSet rs = executeSearch(q);
int pc = 0;
while (rs.next()) {
String str = rs.getString("postcode");
pc = Integer.parseInt(str);
}
System.out.println(pc);
return pc;
}这个查询看起来很好,但是当我初始化一些变量并使用这个方法时,我得到了错误Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""。我有遗漏什么吗?谢谢你的帮助!
发布于 2016-11-16 07:16:34
您的SQL是SELECT PK_ADDRESS_ID ...。
但你试着
rs.getString("postcode");更改为
rs.getString("PK_ADDRESS_ID");发布于 2016-11-16 07:16:52
您似乎正在从数据库中选择PK_ADDRESS_ID列,返回的值要么是“(empty)”,要么是NULL。因此,它显示了不能转换为数字的NumberFormatException。
因此,请检查从数据库中得到的值。
https://stackoverflow.com/questions/40625978
复制相似问题