Advertisement
Guest User

Untitled

a guest
Aug 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class CloseAllResourcesWhenYouAreDonePreJava7
  4. {
  5. public static void main(final String[] args)
  6. {
  7. try
  8. {
  9. final Connection cn = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "");
  10. try
  11. {
  12. final Statement ps = cn.createStatement();
  13. try
  14. {
  15. final ResultSet rs = ps.executeQuery("SELECT STATEMENT GOES HERE");
  16. try
  17. {
  18. if (rs.next())
  19. {
  20. /* process the ResultSet */
  21. }
  22. else
  23. {
  24. throw new SQLException("no rows returned for \"SELECT MAX(msg_id) FROM msgs\"");
  25. }
  26. }
  27. catch (final SQLException e)
  28. {
  29. throw new RuntimeException(e);
  30. }
  31. finally
  32. {
  33. try { rs.close(); } catch (final SQLException e) { System.err.print(e.getMessage()); }
  34. }
  35. }
  36. catch (final SQLException e)
  37. {
  38. throw new RuntimeException(e);
  39. }
  40. finally
  41. {
  42. try { ps.close(); } catch (final SQLException e) { System.err.print(e.getMessage()); }
  43. }
  44. }
  45. catch (final SQLException e)
  46. {
  47. throw new RuntimeException(e);
  48. }
  49. finally
  50. {
  51. try { cn.close(); } catch (final SQLException e) { System.err.print(e.getMessage()); }
  52. }
  53. }
  54. catch (final SQLException e)
  55. {
  56. throw new RuntimeException(e);
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement