SHARE
TWEET

Untitled

a guest Apr 19th, 2019 127 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top