Skip to content

Metabolomics Fiehn Lab

Sections
Personal tools
You are here: Home » Members » Gert Wohlgemuth » java » How to close automaticly JDBC statements

How to close automaticly JDBC statements

Document Actions
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().
Created by zwluxx
Last modified 2005-08-01 02:46 AM
 

Powered by Plone

This site conforms to the following standards: