Advertisement
Guest User

Untitled

a guest
May 13th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package DBLayer;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DatabaseMetaData;
  10. import java.sql.DriverManager;
  11.  
  12. /**
  13.  *
  14.  * @author Kasper
  15.  */
  16. public class DBConnection {
  17.  
  18.     //constans used to get access to the database
  19.     //SQL Server
  20.     private static final String  driver = "jdbc:odbc:";
  21.     //MySql
  22.     //private static final String  driver = "jdbc:mysql://localhost:3306/";
  23.     private static final String  databaseName = "Westernstyle";
  24.     //SQL Server
  25.     private static final String  userName = "";
  26.     private static final String password = "";
  27.     //MySql password and username
  28.     //private static final String  userName = "root";
  29.     //private static final String password = "fisk";
  30.     private DatabaseMetaData dma;
  31.     private static Connection con;
  32.     // an instance of the class is generetated
  33.     private static DBConnection  instance = null;
  34.  
  35.     // the constructor is private to ensure that only one object of this class is createt
  36.     private DBConnection()
  37.     {
  38.         String url = driver + databaseName;
  39.         try{
  40.             //load af driver
  41.             //SQL Server
  42.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  43.             // mySQL
  44.             //  Class.forName("org.gjt.mm.mysql.Driver");
  45.         }
  46.         catch(Exception e){
  47.             System.out.println("Can not find the driver");
  48.             System.out.println(e.getMessage());
  49.         }//end catch
  50.         try{
  51.             //connection to the database
  52.             con = DriverManager.getConnection(url, userName,password);
  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.             System.out.println("Problems with the connection to the database");
  62.             System.out.println(e.getMessage());
  63.         }//end catch
  64.     }//end  constructor
  65.  
  66.   //closeDb: closes the connection to the database
  67.     public static void closeConnection()
  68.     {
  69.         try{
  70.             con.close();
  71.             System.out.println("The connection is closed");
  72.         }
  73.          catch (Exception e){
  74.             System.out.println("Error trying to close the database " +  e.getMessage());
  75.          }
  76.     }//end closeDB
  77.  
  78.     //getDBcon: Get-metode, returnerer forbindelsen til databasen
  79.     public  Connection getDBcon()
  80.     {
  81.        return con;
  82.     }
  83.     //this method is used to get the instance of the connection
  84.     public static DBConnection getInstance()
  85.     {
  86.         if (instance == null)
  87.         {
  88.           instance = new DBConnection();
  89.         }
  90.         return instance;
  91.     }
  92.  
  93. }//end DbConnection
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement