Advertisement
Guest User

Untitled

a guest
Jan 13th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. package db;
  2.  
  3. import java.sql.Connection;
  4.  
  5.  
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8.  
  9.  
  10.  
  11. public class DatabaseSingleton {
  12.     private static DatabaseSingleton ref;
  13.  
  14.     private Connection connection;
  15.  
  16.     private DatabaseSingleton() {
  17.         try {
  18.             Class.forName("com.mysql.jdbc.Driver");
  19.         } catch (ClassNotFoundException e) {
  20.             e.printStackTrace();
  21.         }
  22.     }
  23.  
  24.     public static DatabaseSingleton getDatabaseSingleton() {
  25.         if (ref == null)
  26.             ref = new DatabaseSingleton();
  27.         return ref;
  28.     }
  29.  
  30.     public Object clone() throws CloneNotSupportedException {
  31.         throw new CloneNotSupportedException();
  32.  
  33.     }
  34.  
  35.     public Connection getConnection(boolean autoCommit) throws SQLException {
  36.         if (connection == null || connection.isClosed()) {
  37.  
  38.             try {
  39.                 connection = DriverManager.getConnection(
  40.                         "jdbc:mysql://iwt5.ehb.be/...",
  41.                         DatabaseProperties.USERNAME,
  42.                         DatabaseProperties.PASSWORD);
  43.  
  44.             } catch (SQLException ex) {
  45.                 System.out.println("SQLException: " + ex.getMessage());
  46.                 System.out.println("SQLState: " + ex.getSQLState());
  47.                 System.out.println("VendorError: " + ex.getErrorCode());
  48.             }
  49.         }
  50.        
  51.         connection.setAutoCommit(autoCommit);
  52.        
  53.         return connection;
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement