Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1. package rest.util;
  2.  
  3. import java.io.InputStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7. import java.util.Properties;
  8.  
  9. public class DbUtil {
  10.  
  11.     private static Connection connection = null;
  12.  
  13.     public static Connection getConnection() {
  14.         if (connection != null)
  15.             return connection;
  16.         else {
  17.             Properties prop = new Properties();
  18.             InputStream inputStream = DbUtil.class.getClassLoader().getResourceAsStream("db.properties");
  19.             try {
  20.                 prop.load(inputStream);
  21.                 String driver = prop.getProperty("driver");
  22.                 String url = prop.getProperty("url");
  23.                 String user = prop.getProperty("user");
  24.                 String password = prop.getProperty("password");
  25.                 Class.forName(driver);
  26.                 connection = DriverManager.getConnection(url, user, password);
  27.                 return connection;
  28.             } catch (Exception e) {
  29.                 // TODO Auto-generated catch block
  30.                 e.printStackTrace();
  31.             }
  32.         }
  33.        
  34.         return null;
  35.     }
  36.    
  37.     public static void disconnect() {
  38.         try {
  39.             connection.close();
  40.         } catch (SQLException e) {
  41.             // TODO Auto-generated catch block
  42.             e.printStackTrace();
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement