Advertisement
Guest User

Untitled

a guest
Apr 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. package DBLayer;
  2.  
  3. import java.sql.*;
  4.  
  5. /**
  6.  * @Author Kis Boisen Hansen
  7.  * @Version 2006.10.02 - revised marts 2009 FEN
  8.  * @version 2010.10.07 - new connection
  9.  * @version 2011.11.02 - new methods to handle transaction
  10.  * This class is used to create the connection to the database
  11.  * It is implemented as a singleton. The constructor is private to ensure that only
  12.  * one object of the class is generated
  13.  * Version for Sql Server 2014 the database i located on kraka.ucn.dk
  14.  */
  15.  
  16. public class DbConnection
  17. {   //Constants used to get access to the database
  18.     //SQL Server
  19.    private static final String  driver = "jdbc:sqlserver://kraka.ucn.dk:1433";
  20.  //  private static final String  driver = "jdbc:sqlserver://localhost:1433";
  21.    private static final String  databaseName = ";databaseName=Company";
  22.     //SQL Server
  23.   //  private static String  userName = ";user=sa";
  24.     private static String  userName = "; user=company";
  25.     private static String password = ";password=company";
  26.    
  27.  
  28.     private DatabaseMetaData dma;
  29.     private static Connection con;
  30.     // an instance of the class is generetated
  31.     private static DbConnection  instance = null;
  32.  
  33.     // the constructor is private to ensure that only one object of this class is created
  34.     private DbConnection()
  35.     {
  36.         String url = driver + databaseName + userName + password;
  37.  
  38.         try{
  39.             //load af driver
  40.             //SQL Server
  41.             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  42.             System.out.println("Load af class ok");
  43.          
  44.         }
  45.         catch(Exception e){
  46.             System.out.println("Can not find the driver");
  47.             System.out.println(e.getMessage());
  48.         }//end catch
  49.         try{
  50.             //connection to the database
  51.            
  52.             con = DriverManager.getConnection(url);
  53.             //set autocommit
  54.             con.setAutoCommit(true);
  55.             dma = con.getMetaData(); // get meta data
  56.             System.out.println("Connection to " + dma.getURL());
  57.             System.out.println("Driver " + dma.getDriverName());
  58.             System.out.println("Database product name " + dma.getDatabaseProductName());
  59.         }//end try
  60.         catch(Exception e){
  61.  
  62.             System.out.println("Problems with the connection to the database");
  63.             System.out.println(e.getMessage());
  64.             System.out.println(url);
  65.         }//end catch
  66.     }//end  constructor
  67.        
  68.   //closeDb: closes the connection to the database
  69.     public static void closeConnection()
  70.     {
  71.         try{
  72.             con.close();
  73.             System.out.println("The connection is closed");
  74.         }
  75.          catch (Exception e){
  76.             System.out.println("Error trying to close the database " +  e.getMessage());
  77.          }
  78.     }//end closeDB
  79.        
  80.     //getDBcon: Get-method, returns the connection to the database
  81.     public  Connection getDBcon()
  82.     {
  83.        return con;
  84.     }
  85.     //this method is used to get the instance of the connection
  86.     public static DbConnection getInstance()
  87.     {
  88.         if (instance == null)
  89.         {
  90.           instance = new DbConnection();
  91.         }
  92.         return instance;
  93.     }
  94.     public static void startTransaction()
  95.     { try{
  96.         con.setAutoCommit(false);
  97.         }
  98.       catch(Exception e){
  99.         System.out.println("fejl start transaction");
  100.         System.out.println(e.getMessage());
  101.       }
  102.     }
  103.     public static void commitTransaction()
  104.     { try{
  105.         con.setAutoCommit(true);
  106.         }
  107.       catch(Exception e){
  108.         System.out.println("fejl commit transaction");
  109.         System.out.println(e.getMessage());
  110.       }
  111.     }
  112.     public static void rollbackTransaction()
  113.     { try{
  114.         con.rollback();
  115.         con.setAutoCommit(true);
  116.         }
  117.       catch(Exception e){
  118.         System.out.println("fejl rollback transaction");
  119.         System.out.println(e.getMessage());
  120.       }
  121.     }
  122. }//end DbConnection
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement