Advertisement
Guest User

DataBase.java

a guest
Jun 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. package ris_i_bd;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class DataBase {  
  8.     private static final String CONNECTION_STRING = "jdbc:postgresql:ris_i_bd";
  9.     private static final String USER_NAME = "postgres";
  10.     private static final String PASSWORD = "837hfi37all";
  11.    
  12.     private static Connection connection;
  13.    
  14.     private DataBase() {
  15.         throw new UnsupportedOperationException();
  16.     }
  17.    
  18.     static {
  19.         try {
  20.             Class.forName("org.postgresql.Driver");
  21.         } catch (ClassNotFoundException e) {
  22.             throw new RuntimeException(e);
  23.         }
  24.     }
  25.    
  26.     public static Connection getConnection() {
  27.         if(connection == null) {
  28.             try {
  29.                 connection =  DriverManager.getConnection(CONNECTION_STRING, USER_NAME, PASSWORD);
  30.                 return connection;
  31.             } catch (SQLException e) {
  32.                 throw new RuntimeException(e);
  33.             }
  34.         } else {
  35.             return connection;
  36.         }      
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement