Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package DBLayer;
- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.DriverManager;
- /**
- *
- * @author Kasper
- */
- public class DBConnection {
- //constans used to get access to the database
- //SQL Server
- private static final String driver = "jdbc:odbc:";
- //MySql
- //private static final String driver = "jdbc:mysql://localhost:3306/";
- private static final String databaseName = "Westernstyle";
- //SQL Server
- private static final String userName = "";
- private static final String password = "";
- //MySql password and username
- //private static final String userName = "root";
- //private static final String password = "fisk";
- private DatabaseMetaData dma;
- private static Connection con;
- // an instance of the class is generetated
- private static DBConnection instance = null;
- // the constructor is private to ensure that only one object of this class is createt
- private DBConnection()
- {
- String url = driver + databaseName;
- try{
- //load af driver
- //SQL Server
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- // mySQL
- // Class.forName("org.gjt.mm.mysql.Driver");
- }
- catch(Exception e){
- System.out.println("Can not find the driver");
- System.out.println(e.getMessage());
- }//end catch
- try{
- //connection to the database
- con = DriverManager.getConnection(url, userName,password);
- //set autocommit
- con.setAutoCommit(true);
- dma = con.getMetaData(); // get meta data
- System.out.println("Connection to " + dma.getURL());
- System.out.println("Driver " + dma.getDriverName());
- System.out.println("Database product name " + dma.getDatabaseProductName());
- }//end try
- catch(Exception e){
- System.out.println("Problems with the connection to the database");
- System.out.println(e.getMessage());
- }//end catch
- }//end constructor
- //closeDb: closes the connection to the database
- public static void closeConnection()
- {
- try{
- con.close();
- System.out.println("The connection is closed");
- }
- catch (Exception e){
- System.out.println("Error trying to close the database " + e.getMessage());
- }
- }//end closeDB
- //getDBcon: Get-metode, returnerer forbindelsen til databasen
- public Connection getDBcon()
- {
- return con;
- }
- //this method is used to get the instance of the connection
- public static DBConnection getInstance()
- {
- if (instance == null)
- {
- instance = new DBConnection();
- }
- return instance;
- }
- }//end DbConnection
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement