John
John

Reputation: 1189

Closing DB Statement

 DBPreparedStatement stmt = null;
    try
    {
        stmt = new DBPreparedStatement(UTIL.getDatabase(), sql.toString(), "Graph.saveLastGraphName");
        stmt.setString(1, DBGlobals.doubleQuote(graphNameUsed));
        stmt.setString(2, userUpper);
        stmt.executeUpdate();
        stmt.close();
    }
    catch (Exception ex)
    {
        DBGlobals.Error("OHMjson.Graph.saveLastGraphName - Error: " + ex.getMessage());
        msg = "Unable to save data";
        status = false;
    }
    finally
    {
        if (stmt != null)
            stmt.close();
    }

I am closing the database statement inside the finally block, is it the right way. Will this cause any memory leakage.

Upvotes: 1

Views: 234

Answers (1)

nstehr
nstehr

Reputation: 8138

Yes, I would agree with you that this is the right way to do it. Closing it in the finally block will ensure that the statement is always closed, even in the exceptional case. You could probably even get rid of the stmt.close() in your try, as it will get called in the finally block.

Upvotes: 3

Related Questions