you do it exactly the same way I do (well I do that and surround the
whole thing with a try{}catch(SQLException e){throw new
RuntimeException(e)}.
First time I've ever seen anyone else handle resource closing this
way.
Usually I see something like (this is mostly real code):
Connection conn = conManager.getConnection();
PreparedStatement updateStmt = null;
PreparedStatement deleteContainmentStmt = null;
PreparedStatement addContainmentStmt = null;
try {
updateStmt = conn.prepareStatement("UPDATE BLAH SET BLAH
= ?, BLAH2 = ? WHERE " +
"D = ?");
updateStmt.setFloat(1, safeWeight);
updateStmt.executeUpdate();
deleteContainmentStmt = conn.prepareStatement("DELETE FROM
BLAH = ?");
deleteContainmentStmt.setLong(1, blah.ID());
deleteContainmentStmt.executeUpdate();
final String insertSql = "INSERT INTO BLAH(BLAH_ID) VALUES
(?)";
addContainmentStmt = conn.prepareStatement(insertSql);
addContainmentStmt.setLong(1, 39393);
try {
addContainmentStmt.execute();
} catch (SQLException e) {
final StringBuilder msg = new StringBuilder();
msg.append("failed SQL [" + insertSql + "] ");
LoggingUtil.log(Storage.class, Level.DEBUG,
msg.toString(), e);
throw e;
}
}
} catch (SQLException e) {
conn.setLastException(e);
LoggingUtil.log(getClass(), Level.DEBUG, e.getMessage(),
e);
} finally {
if (updateStmt != null) {
try {
updateStmt.close();
} catch (SQLException e) {
LoggingUtil.log(getClass(), Level.DEBUG,
e.getMessage(), e);
}
}
if (deleteContainmentStmt != null) {
try {
deleteContainmentStmt.close();
} catch (SQLException e) {
LoggingUtil.log(getClass(), Level.DEBUG,
e.getMessage(), e);
}
}
if (addContainmentStmt != null) {
try {
addContainmentStmt.close();
} catch (SQLException e) {
LoggingUtil.log(getClass(), Level.DEBUG,
e.getMessage(), e);
}
}
conManager.freeConnection(conn);
}
On Aug 17, 7:34 pm, Christian Catchpole <[email protected]>
wrote:
> Go the "finally".. (there is that risk of the original exception
> being lost and knock on ones cropping up)
>
> PreparedStatement ps = connection.prepareStatement(sql);
> try {
> ResultSet resultSet = ps.executeQuery();
> try {
> while (resultSet.next()) {
> // stuff
> }
> } finally {
> resultSet.close();
> }
> } finally {
> ps.close();
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---