Advertisement
Guest User

Untitled

a guest
Aug 5th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. package database;
  2.  
  3. import java.sql.*;
  4.  
  5. public class databaseConnection {
  6.    
  7.     private static Connection singletonInstance = null;
  8.  
  9.     //Private constructor -> Singleton
  10.     private databaseConnection() {
  11.         createConnection();
  12.     }
  13.    
  14.     public synchronized  Connection getDatabaseConnection() {
  15.         if(singletonInstance == null) {
  16.             databaseConnection.singletonInstance = new databaseConnection();
  17.             return databaseConnection.singletonInstance;
  18.         }
  19.         return null;
  20.     }
  21.    
  22.    
  23.    
  24.     private void createConnection() {
  25.         try {
  26.             String driver = "com.mysql.jdbc.Driver";
  27.            
  28.             Class.forName(driver).newInstance();
  29.             singletonInstance = DriverManager.getConnection("jdbc:mysql://localhost:3306/111,111,111");
  30.         }
  31.         catch(Exception e) {
  32.            
  33.         }
  34.     }
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement