In a class for handling SQL transactions in Java, I'm find myself using the classes PreparedStatement and ResultSet all the time.
I got curious to know what would be better (more efficient) practice in Java; declaring them as members of the class ...
class SqlThingy {
PreparedStatement pstx;
ResultSet rs;
public void SqlThingyMethod() {
pstx = database.connection.prepareStatement("...");
....
}
}
... or as local variables of individual methods?
class SqlThingy {
public void SqlThingyMethod() {
PreparedStatement pstx;
ResultSet rs;
pstx = database.connection.prepareStatement("...");
}
}
Does the VM merely overwrite the contents of the class member with the (reference to the) new preparedstatement, or will it do some additional initialization that also claims resources and even out the difference of allocating local variables every time?