It this construction correct? Or are there any errors/bad habbits ?
public static ResultSet getRecord(String id)
{
Connection conn = DataBase.Connect();
try
{
try
{
Statement stm = conn.createStatement();
ResultSet data = stm.executeQuery("SELECT * FROM " + GlobalFields.Records + " WHERE id = '" + id + "';");
return data;
}
finally
{
conn.close();
}
}
catch(SQLException e)
{
e.printStackTrace();
}
return null;
}
You cannot process a resultset after you close the underlying statement or connection or both. If you intend to process it later, you should look at CachedRowSet that are built from the resultset and processed just like resultsets are, at a later point of time after closing the statement/connection.
You are not properly closing the statement and result set. This is needed as closing Connection need not always mean closing the physical connection, particularly when you are using connection pools.
The inner try block is unnecessary, you can as well push that finally block out, under the catch statement.
Instead of handling connection, statement, resultset on your own, there are light-weight frameworks like Spring JdbcTemplate that would take care of closing resources properly.
Also, if you are using JDK 7, then you should use the try-with-resources construct for all those closeable resources viz. connection, statement and resultset.