I'm Java beginner, but I thought that when using try-catch-finally I don't have to declare the exception using throws SQLException. However if I don't use it the compiler gives me the error:
"unreported exception java.sql.SQLException; must be caught or declare to be thrown".
I included a catch so I'm not sure why this errors occurs.
public static ResultSet getResultSet ( String query )
{
dbConn = getConnection();
try
{
stmt = dbConn.createStatement( );
ResultSet rs = stmt.executeQuery( query );
return rs;
}
catch (SQLException ex)
{
return null;
}
finally
{
stmt.close();
dbConn.close();
}
}
dbConnandstmtasstatic. This code is definitely not threadsafe. I strongly recommend to redo the basic Java and JDBC tutorials.SQLExceptionswhen you use theResultSet, so you might as well propagate the exception. TheResultSetprobably isn't going to work after the connection is closed. You might want to consider the Execute Around idiom. stackoverflow.com/questions/341971/…ResultSetto aList<Entity>and return that instead. For some examples, head here and here.