Advertisement
Guest User

Untitled

a guest
Apr 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. package connector;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11.  
  12. public class Connector {
  13.  
  14. private static final Logger LOGGER = Logger.getLogger(Connector.class.getName());
  15. private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
  16. private static final String DBURL = "jdbc:mysql://localhost:3306/ass2db?autoReconnect=true&useSSL=false";
  17. private static final String USER = "root";
  18. private static final String PASS = "password";
  19.  
  20. private static Connector singleInstance = new Connector();
  21.  
  22. private Connector() {
  23. try {
  24. Class.forName(DRIVER);
  25. } catch (ClassNotFoundException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29.  
  30. private Connection createConnection() {
  31. Connection connection = null;
  32. try {
  33. connection = DriverManager.getConnection(DBURL, USER, PASS);
  34. return connection;
  35. } catch (SQLException e) {
  36. LOGGER.log(Level.WARNING, "An error occured while trying to connect to the database");
  37. e.printStackTrace();
  38. return null;
  39. }
  40. }
  41.  
  42. public static Connection getConnection() {
  43. return singleInstance.createConnection();
  44. }
  45.  
  46. public static void close(Connection connection) {
  47. if (connection != null) {
  48. try {
  49. connection.close();
  50. } catch (SQLException e) {
  51. LOGGER.log(Level.WARNING, "An error occured while trying to close the connection");
  52. }
  53. }
  54. }
  55.  
  56. public static void close(Statement statement) {
  57. if (statement != null) {
  58. try {
  59. statement.close();
  60. } catch (SQLException e) {
  61. LOGGER.log(Level.WARNING, "An error occured while trying to close the statement");
  62. }
  63. }
  64. }
  65.  
  66. public static void close(ResultSet resultSet) {
  67. if (resultSet != null) {
  68. try {
  69. resultSet.close();
  70. } catch (SQLException e) {
  71. LOGGER.log(Level.WARNING, "An error occured while trying to close the ResultSet");
  72. }
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement