I’m having trouble getting any sense back from my mySQL database. I’m passing it a valid query that should return a single row. However it only ever returns me 0, the intial value of int results
public String getResultSet(String query) throws SQLException{
ResultSet rs = null;
int results=0;
try {
con = ds.getConnection();
Statement stmt = (Statement) con.createStatement();
rs = stmt.executeQuery(query);
while(rs.next()){
results = rs.getInt("location_id");
}
} catch (Exception ex ){
} finally {
if (con != null) con.close();
}
return Integer.toString(results);
}
No errors are being thrown, could some advise what I’m doing wrong?
TIA
It looks like you have a catch block for
Exceptionwith an empty body. This will catch and silently ignore any SQL etcetera exceptions. Then you will drop out of the try/catch, and returnresults… which will still be zero.What you are doing is “squashing” the exception; i.e. catching it and throwing it away without doing any recovery or error reporting. This is bad practice. Worse still, you are doing this for
Exception. That means you are doing it not only for the checked exceptions that you (possibly) expect to occur, but also for a whole range of unchecked exceptions that you cannot anticipate.