Guest User

Untitled

a guest
Jan 15th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. // Post: A connection is opened if the current connection is not valid
  2. private static boolean open() throws SQLException {
  3. // If connection is still valid, return without doing anything
  4. try {
  5. if (connection != null && connection.isValid(1)) {
  6. return true;
  7. }
  8. }
  9. catch (SQLException e1) {
  10. close();
  11. }
  12.  
  13. // Start connecting
  14. String driver = "com.mysql.jdbc.Driver";
  15. String url = "jdbc:mysql://mysql.starredout.com/" + database;
  16. String user = "starredout";
  17. String password = "starredout";
  18. try {
  19. Class.forName(driver).newInstance();
  20. }
  21. catch (Exception e) {
  22. e.printStackTrace();
  23. return false;
  24. }
  25. connection = DriverManager.getConnection(url,user,password);
  26. return true;
  27. }
  28.  
  29. // Post: If a connection was open, it is closed
  30. private static void close() {
  31. try {
  32. if (connection == null || connection.isClosed())
  33. return;
  34. connection.close();
  35. }
  36. catch (Exception e) {
  37. e.printStackTrace();
  38. System.out.println(e.toString());
  39. }
  40. }
Add Comment
Please, Sign In to add comment