Advertisement
Guest User

DBConnection class

a guest
Feb 19th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. import java.sql.*;
  2. /**
  3.  * @Author FEN
  4.  * @Version 2016.02.12
  5.  * This class is used to create the connection to the database
  6.  * It is implemented as a singleton. The constructor is private to ensure that only one connection exists
  7.  */
  8.  
  9. public class DBConnection
  10. {   //Constants used to get access to the database
  11.    
  12.     private static final String  driver = "172.17.241.186:1433";
  13.     private static final String  databaseName = ";databaseName=company";
  14.    
  15.     private static String  userName = "; user=sa";
  16.     private static String password = ";password=masterkey";
  17.    
  18.     private DatabaseMetaData dma;
  19.     private static Connection con;
  20.    
  21.     // an instance of the class is generated
  22.     private static DBConnection  instance = null;
  23.  
  24.     // the constructor is private to ensure that only one object of this class is created
  25.     private DBConnection()
  26.     {
  27.         String url = driver + databaseName + userName + password;
  28.  
  29.         try{
  30.             //load of driver
  31.             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  32.             System.out.println("Driver class loaded ok");
  33.          
  34.         }
  35.         catch(Exception e){
  36.             System.out.println("Cannot find the driver");
  37.             System.out.println(e.getMessage());
  38.         }
  39.         try{
  40.             //connection to the database
  41.             con = DriverManager.getConnection(url);
  42.             con.setAutoCommit(true);
  43.             dma = con.getMetaData(); // get meta data
  44.             System.out.println("Connection to " + dma.getURL());
  45.             System.out.println("Driver " + dma.getDriverName());
  46.             System.out.println("Database product name " + dma.getDatabaseProductName());
  47.         }//end try
  48.         catch(Exception e){
  49.             System.out.println("Problems with the connection to the database:");
  50.             System.out.println(e.getMessage());
  51.             System.out.println(url);
  52.         }//end catch
  53.     }//end  constructor
  54.        
  55.   //closeDb: closes the connection to the database
  56.     public static void closeConnection()
  57.     {
  58.         try{
  59.             con.close();
  60.             instance= null;
  61.             System.out.println("The connection is closed");
  62.         }
  63.          catch (Exception e){
  64.             System.out.println("Error trying to close the database " +  e.getMessage());
  65.          }
  66.     }//end closeDB
  67.        
  68.     //getDBcon: returns the singleton instance of the DB connection
  69.     public  Connection getDBcon()
  70.     {
  71.        return con;
  72.     }
  73.     //this method is used to get the instance of the connection
  74.     public static DBConnection getInstance()
  75.     {
  76.         if (instance == null)
  77.         {
  78.           instance = new DBConnection();
  79.         }
  80.         return instance;
  81.     }
  82.  
  83. }//end DbConnection
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement