Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package org.secondstack.db;
  6.  
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.SQLException;
  10.  
  11. /**
  12. *
  13. * @author Deny Prasetyo
  14. */
  15. public class DatabaseConnection {
  16.  
  17. private static DatabaseConnection instance;
  18. private Connection connection;
  19. private String url = "jdbc:postgresql://localhost:5432/jdbc";
  20. private String username = "root";
  21. private String password = "localhost";
  22.  
  23. private DatabaseConnection() throws SQLException {
  24. try {
  25. Class.forName("org.postgresql.Driver");
  26. this.connection = DriverManager.getConnection(url, username, password);
  27. } catch (ClassNotFoundException ex) {
  28. System.out.println("Database Connection Creation Failed : " + ex.getMessage());
  29. }
  30. }
  31.  
  32. public Connection getConnection() {
  33. return connection;
  34. }
  35.  
  36. public static DatabaseConnection getInstance() throws SQLException {
  37. if (instance == null) {
  38. instance = new DatabaseConnection();
  39. } else if (instance.getConnection().isClosed()) {
  40. instance = new DatabaseConnection();
  41. }
  42.  
  43. return instance;
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement