Advertisement
Guest User

DBConnection

a guest
Apr 13th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. package com.socialka.db;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5.  
  6. public class DBConnection {
  7.     private final Connection connection;
  8.     private static DBConnection instance;
  9.    
  10.     private static final String DB_HOST = "localhost";
  11.     private static final String DB_USER = "root";
  12.     private static final String DB_PASS = "MySqlRoot123";
  13.     private static final String DB_PORT = "3306";
  14.     private static final String DB_SCHEMA = "mysocialka";
  15.     private static final String DB_OPTIONS = "autoReconnect=true&useSSL=false";
  16.    
  17.     private DBConnection() throws Exception {
  18.         Class.forName("com.mysql.jdbc.Driver");
  19.         this.connection = DriverManager.getConnection(
  20.                 String.format("jdbc:mysql://%s:%s/%s?%s", DB_HOST, DB_PORT, DB_SCHEMA, DB_OPTIONS), DB_USER, DB_PASS);
  21.        
  22.     }
  23.    
  24.     public static DBConnection getInstance() throws Exception {
  25.         if (instance == null) {
  26.             instance = new DBConnection();
  27.         }
  28.         return instance;
  29.     }
  30.    
  31.     public Connection getConnection() {
  32.         return connection;
  33.     }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement