I am new to programming and trying to assign a value from a sql query to a jLabel. The program compiles and runs with no errors but doesn't change the jLabel. According to my research, this should work, but doesn't. DB connection is fine. When I debug, the problem seems to be with the line: double d = rs.getDouble(1); Here is my code:
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));
If I substitute an arbitrary value like 12.345 for d, it works.
You forgot to call 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