Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement