Advertisement
andersonalmada

Untitled

Jul 6th, 2022
1,075
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package mandacaru3.dao;
  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 ConnectionFactory {
  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 = ConnectionFactory.class.getClassLoader().getResourceAsStream("db.properties");
  19.             try {
  20.                 prop.load(inputStream);
  21.                 String url = prop.getProperty("url");
  22.                 String user = prop.getProperty("user");
  23.                 String password = prop.getProperty("password");
  24.                 connection = DriverManager.getConnection(url, user, password);
  25.                 return connection;
  26.             } catch (Exception e) {
  27.                 // TODO Auto-generated catch block
  28.                 e.printStackTrace();
  29.             }
  30.         }
  31.  
  32.         return null;
  33.     }
  34.  
  35.     public static void disconnect() {
  36.         try {
  37.             connection.close();
  38.         } catch (SQLException e) {
  39.             // TODO Auto-generated catch block
  40.             e.printStackTrace();
  41.         }
  42.     }
  43. }
  44.  
Advertisement
RAW Paste Data Copied
Advertisement