Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package mssql;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.SQLException;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13.  
  14. /**
  15.  *
  16.  * @author Ticu
  17.  */
  18. public class DatabaseConnection {
  19.  
  20.     private static DatabaseConnection instance;
  21.     private Connection connection;
  22.     private String url = "jdbc:sqlserver://DESKTOP-G3JRQ2G;databaseName=boeken;user=Ticu;password=Adm123";
  23.  
  24.     private DatabaseConnection() {
  25.         try {
  26.             this.connection = DriverManager.getConnection(url);
  27.         } catch (SQLException ex) {
  28.             Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
  29.         }
  30.     }
  31.  
  32.     public Connection getConnection() {
  33.         return connection;
  34.     }
  35.  
  36.     public static DatabaseConnection getInstance() throws SQLException {
  37.         if (instance == null) {
  38.             instance = new DatabaseConnection();
  39.         } else if (instance.getConnection().isClosed()) {
  40.             instance = new DatabaseConnection();
  41.         }
  42.  
  43.         return instance;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement