Created
August 25, 2017 19:34
-
-
Save jarrodhroberson/83d6b7a5549c9839ad1a030e694d0ce7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
public class CloseAllResourcesWhenYouAreDonePreJava7 | |
{ | |
public static void main(final String[] args) | |
{ | |
try | |
{ | |
final Connection cn = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", ""); | |
try | |
{ | |
final Statement ps = cn.createStatement(); | |
try | |
{ | |
final ResultSet rs = ps.executeQuery("SELECT STATEMENT GOES HERE"); | |
try | |
{ | |
if (rs.next()) | |
{ | |
/* process the ResultSet */ | |
} | |
else | |
{ | |
throw new SQLException("no rows returned for \"SELECT MAX(msg_id) FROM msgs\""); | |
} | |
} | |
catch (final SQLException e) | |
{ | |
throw new RuntimeException(e); | |
} | |
finally | |
{ | |
try { rs.close(); } catch (final SQLException e) { System.err.print(e.getMessage()); } | |
} | |
} | |
catch (final SQLException e) | |
{ | |
throw new RuntimeException(e); | |
} | |
finally | |
{ | |
try { ps.close(); } catch (final SQLException e) { System.err.print(e.getMessage()); } | |
} | |
} | |
catch (final SQLException e) | |
{ | |
throw new RuntimeException(e); | |
} | |
finally | |
{ | |
try { cn.close(); } catch (final SQLException e) { System.err.print(e.getMessage()); } | |
} | |
} | |
catch (final SQLException e) | |
{ | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment