我是编程并尝试将sql查询中的值分配给jLabel的新手。该程序编译并运行没有错误,但不会更改jLabel。根据我的研究,这应该有效,但不是。数据库连接很好。当我调试时,问题似乎与行: double d = rs.getDouble(1); 这是我的代码:
String sum = "SELECT SUM(TOTAL) FROM PUNCHES WHERE EID = 1";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sum); //rs should have 1 row, 1 column
double d = rs.getDouble(1);
lblGrandTotal.setText(String.valueOf(d));
如果我用12.345这样的任意值代替d,它就可以了。
答案 0 :(得分:1)
您忘了拨打rs.next()
ResultSet rs = st.executeQuery(sum); //rs should have 1 row, 1 column
if (rs.next())
{
double d = rs.getDouble(1);
...
}
else
// Failed to get result, do something useful here