joseleonweb

Untitled

May 12th, 2021
549
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.FileInputStream;
  2. import java.io.IOException;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.util.Properties;
  7.  
  8. public class MySQLJDBCUtil {
  9.  
  10.     public static Connection getConnection() throws SQLException {
  11.         Connection conn = null;
  12.  
  13.         try (FileInputStream f = new FileInputStream("db.properties")) {
  14.  
  15.             // load the properties file
  16.             Properties pros = new Properties();
  17.             pros.load(f);
  18.  
  19.             // assign db parameters
  20.             String url = pros.getProperty("url");
  21.             String user = pros.getProperty("user");
  22.             String password = pros.getProperty("password");
  23.            
  24.             // create a connection to the database
  25.             conn = DriverManager.getConnection(url, user, password);
  26.         } catch (IOException e) {
  27.             System.out.println(e.getMessage());
  28.         }
  29.         return conn;
  30.     }
  31.  
  32. }
RAW Paste Data