Advertisement
Guest User

Java

a guest
Mar 31st, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. package org.example.connection;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8. import java.util.Properties;
  9.  
  10. public class ConnectionMariaDB {
  11.     private static Connection instance;
  12.  
  13.     private ConnectionMariaDB() {
  14.     }
  15.    
  16.     public static Connection getConnection() {
  17.         if (instance == null) {
  18.             instance = getInstance();
  19.         }
  20.         return instance;
  21.     }
  22.  
  23.     private static Connection getInstance() {
  24.         Properties props = new Properties();
  25.         FileInputStream fis;
  26.         try {
  27.             fis = new FileInputStream("src/main/resources/db.properties");
  28.             props.load(fis);
  29.         } catch (IOException e) {
  30.             e.printStackTrace();
  31.         }
  32.         String drivers = props.getProperty("db.driver");
  33.         if (drivers != null) {
  34.             System.setProperty("db.driver", drivers);
  35.         }
  36.         String url = props.getProperty("db.url");
  37.         String username = props.getProperty("db.user");
  38.         String password = props.getProperty("db.password");
  39.  
  40.         Connection connection = null;
  41.  
  42.         try {
  43.             connection =  DriverManager.getConnection(url, username, password);
  44.         } catch (SQLException e) {
  45.             e.printStackTrace();
  46.         }
  47.         return connection;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement