Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. package database;
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7.  
  8. public class PostgreSQLJDBC {
  9.  
  10.     public static Connection cnx = null;
  11.     public static String dbName = "";
  12.     public static String host = "";
  13.     public static String port = "";
  14.     public static String userName = "";
  15.     public static String password = "";
  16.  
  17.     public static Connection getConnexion() {
  18.  
  19.         if(cnx == null){
  20.             try {
  21.                 Class.forName("org.postgresql.Driver");
  22.                 cnx = DriverManager
  23.                         .getConnection("jdbc:postgresql://" + host + ":" + port + "/" + dbName,
  24.                                 userName , password);
  25.             } catch (Exception e) {
  26.                 e.printStackTrace();
  27.                 System.err.println(e.getClass().getName()+": "+e.getMessage());
  28.                 System.exit(0);
  29.             }
  30.             System.out.println("Opened database successfully");
  31.         }
  32.  
  33.         return cnx;
  34.     }
  35.  
  36.  
  37.     public static void closeConnection(){
  38.         if(cnx != null){
  39.             try {
  40.                 cnx.close();
  41.             } catch (SQLException e) {
  42.                 // TODO Auto-generated catch block
  43.                 e.printStackTrace();
  44.             }
  45.         }
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement