How to close automaticly JDBC statements
this show how you can close jdbc statements
with this little code fragment you can close automaticly statements. But you should implement a routine to get sure that the statement is still open!
Class current = this.getClass();
Field[] f = current.getDeclaredFields();
for (int i = 0; i < f.length; i++) {
f[i].setAccessible(true);
Class type = f[i].getType();
if (type.getName().matches(
"java.sql.Statement") ||
"java.sql.PreparedStatement".equals(type.getName()))
{
Object o = f[i].get(this);
if (o != null) {
Statement stm = (Statement) o;
stm.close();
stm = null;
}
}
}
it's pretty ugly to compare the strings a better way is to create a default ipmlementation of java.sql.Statement and than use instanceof instead of matches().
Class current = this.getClass();
Field[] f = current.getDeclaredFields();
for (int i = 0; i < f.length; i++) {
f[i].setAccessible(true);
Class type = f[i].getType();
if (type.getName().matches(
"java.sql.Statement") ||
"java.sql.PreparedStatement".equals(type.getName()))
{
Object o = f[i].get(this);
if (o != null) {
Statement stm = (Statement) o;
stm.close();
stm = null;
}
}
}
it's pretty ugly to compare the strings a better way is to create a default ipmlementation of java.sql.Statement and than use instanceof instead of matches().
Created by
zwluxx
Last modified 2005-08-01 02:46 AM
Last modified 2005-08-01 02:46 AM