Posted By: gaelhatchue | Dec 29th, 2005 @ 5:15 PM
page 1 of 1
Comments: 2 | Views: 3689
The following sample is taken from the documentation of Connector/J, the jdbc driver for mysql

        InitialContext ctx = new InitialContext();
        DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQLDB");
        Connection conn = null;
        Statement stmt = null;

        try {
            conn = ds.getConnection();
            stmt = conn.createStatement();
            stmt.execute("SOME SQL QUERY");

            stmt.close();
            stmt = null;

            conn.close();
            conn = null;
        } finally {

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (sqlexception sqlex) {
                    // ignore -- as we can't do anything about it here
                }

                stmt = null;
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (sqlexception sqlex) {
                    // ignore -- as we can't do anything about it here
                }

                conn = null;
            }
        }


gabe19
gabe19
bang bang shoot shoot
Not sure I've got what you're after, but this seems like it would be better written with the closing and nulling (required?) in the finally block. If Java throws exceptions in the try after the finally, you will get exceptions from trying to close, but the close will be executed anyhow by the exception-swallowing finally block. Certainly seems convoluted.
page 1 of 1
Comments: 2 | Views: 3689