vendredi 10 juillet 2015

What is the accurate syntax for SQLite JDBC Connections?

This is may look like a repeated question but, it is not. I have tried looking for an answer for this over 48 hours with no result.

Firstly, is closing PreparedStatment & ResultSet necessary in SQLite JDBC Connection? because; I am unable to do so.

try {
            Class.forName(database.getJDBC_DRIVER());
            cnn = DriverManager.getConnection(database.getDB_URL());
            p = cnn.prepareStatement(query);
            rs = p.executeQuery();
            p.close();
            cnn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                System.out.println(p.isClosed());
                if (cnn != null) cnn.close();
                System.out.println(p.isClosed());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

As you can see, I closed PreparedStatement inside the try block. However, When I'm checking the state of same outside the try block, the result is always false. (which means it isn't closed).

Besides, if I have below code in the finally block, it throws an error stating the connection is closed. I am super confused on what to do. Should I just leave it as it is. Wouldn't my code have some leakage?

} finally {
            try {
                if (p != null) p.close();
                if (cnn != null) cnn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

Aucun commentaire:

Enregistrer un commentaire