Advertisement
Guest User

Untitled

a guest
May 9th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. package db;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class Conexao {
  8.  
  9.     private static Connection instance;
  10.     private static String banco = "postgres";
  11.     private static String host = "localhost";
  12.     private static String porta = "5432";
  13.     private static String user = "postgres";
  14.     private static String passwd = "postgres";
  15.     private static String url = "jdbc:postgresql://" + host + ":" + porta + "/" + banco;
  16.  
  17.     private Conexao() {
  18.     }
  19.  
  20.     public static Connection getInstance() throws SQLException, ClassNotFoundException {
  21.         if (instance == null) {
  22.             Class.forName("org.postgresql.Driver");
  23.             instance = DriverManager.getConnection(url);
  24.         }
  25.         return instance;
  26.     }
  27.  
  28.     public static void restartConnection() throws ClassNotFoundException, SQLException {
  29.         instance = DriverManager.getConnection(url, user, passwd);
  30.     }
  31.  
  32.     public static void closeConnection() throws SQLException {
  33.         instance.close();
  34.         instance = null;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement