Advertisement
Guest User

JDBConnection

a guest
Nov 23rd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. import java.sql.*;
  2. import java.util.ArrayList;
  3.  
  4. public final class JDBConnection { //zodanig niemand eraan kan
  5.     private static JDBConnection instance = null;
  6.     private Connection conn = null;
  7.    
  8.     private JDBConnection(){}
  9.    
  10.     public void openConnection(String database, String user, String pwd){
  11.         try{
  12.             Class.forName("com.mysql.jdbc.Driver");
  13.             String url = "jdbc:mysql://127.0.0.1/" + database;
  14.             conn = DriverManager.getConnection(url, user, pwd);
  15.         }
  16.         catch (Exception e ){
  17.             System.out.println("Error: " + e);
  18.         }
  19.     }
  20.    
  21.     public void closeConnection(){
  22.        
  23.         if(conn != null){
  24.             try{
  25.                 conn.close();
  26.             }
  27.             catch (SQLException ex){
  28.                 System.out.println("Error: " + ex);
  29.             }
  30.         }
  31.     }
  32.    
  33.     public static synchronized JDBConnection getJDBConnection() {
  34.        
  35.         if(instance == null){
  36.             instance = new JDBConnection();
  37.         }
  38.         return instance;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement