我在Java中运行一组SQL查询来检索a,b和c值。之后我想用它们做一些操作并将结果打印给用户。
运行以下代码时,我收到“java.lang.NumberFormatException:For input string”:
try {
String sql1="SELECT COUNT(CASE WHEN Type = 'Sick' THEN 1 ELSE 0 END) AS sick FROM `leave` WHERE `leave`.`EID`=? AND `leave`.`Month`=?";
pst=conn.prepareStatement(sql1);
int a = Integer.parseInt(sql1);
String sql2="SELECT COUNT(CASE WHEN Type = 'annual' THEN 1 ELSE 0 END) AS annual FROM `leave` WHERE `leave`.`EID`=? AND `leave`.`Date`=?";
pst=conn.prepareStatement(sql2);
int b= Integer.parseInt(sql2);
String sql3="SELECT COUNT(CASE WHEN Type = 'Half day' THEN 1 ELSE 0 END) AS Half_day FROM `leave` WHERE `leave.EID`=? AND `leave`.`Date`=?";
pst=conn.prepareStatement(sql3);
rs=pst.executeQuery();
int c= Integer.parseInt(sql3);
if ((a+b+(c/2)) < 14) {
int double_pay = 14 - (a+b+(c/2));
}
else {
int double_pay = 0;
}
txt_double_pay.setText(double_pay+"");
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
有谁知道这个例外的原因?
答案 0 :(得分:2)
原因是
Integer.parseInt(sql3)
要求 sql3 是一个包含数据的字符串,可以将其解析为整数..
你的sql3对象正在改为:
sql3="SELECT COUNT(CASE WHEN Type = 'Half day' THEN 1 ELSE 0 END)
AS Half_day FROM `leave` WHERE `leave.EID`=? AND `leave`.`Date`=?";
所以java永远不会得到任何数量的字符串....