Advertisement
Smryk

Untitled

Mar 2nd, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1. package com.eversis.piuw.exporter.custom;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class JDBC {
  8.  
  9.     private static JDBC instance;
  10.     private Connection connection;
  11.     private String url = "jdbc:mysql://localhost:3306/liferay?serverTimezone=UTC&zeroDateTimeBehavior=convertToNull&useSSL=false";
  12.     private String username = "root";
  13.     private String password = "password";
  14.  
  15.     private JDBC() throws SQLException {
  16.         try {
  17.             Class.forName("com.mysql.jdbc.Driver");
  18.             this.connection = DriverManager.getConnection(url, username, password);
  19.         } catch (ClassNotFoundException ex) {
  20.             System.out.println("Database Connection Creation Failed : " + ex.getMessage());
  21.         }
  22.     }
  23.  
  24.     public Connection getConnection() {
  25.         return connection;
  26.     }
  27.  
  28.     public static JDBC getInstance() {
  29.  
  30.         try {
  31.             if (instance == null || instance.getConnection().isClosed()) {
  32.                 instance = new JDBC();
  33.             }
  34.         } catch (SQLException e) {
  35.             e.printStackTrace();
  36.         }
  37.         return instance;
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement