Advertisement
andersonalmada

Untitled

Jul 7th, 2022
1,134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. package mandacaru3.dao;
  2.  
  3. import java.io.InputStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.util.Properties;
  7.  
  8. public class ConnectionFactory {
  9.  
  10.     private static String url = null;
  11.     private static String user = null;
  12.     private static String password = null;
  13.  
  14.     public static Connection getConnection() {
  15.         try {
  16.             if (url == null) {
  17.                 Properties prop = new Properties();
  18.                 InputStream inputStream = ConnectionFactory.class.getClassLoader().getResourceAsStream("db.properties");
  19.                 prop.load(inputStream);
  20.                 url = prop.getProperty("url");
  21.                 user = prop.getProperty("user");
  22.                 password = prop.getProperty("password");
  23.             }
  24.  
  25.             return DriverManager.getConnection(url, user, password);
  26.         } catch (Exception e) {
  27.             e.printStackTrace();
  28.             return null;
  29.         }
  30.     }
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement