Advertisement
Guest User

Untitled

a guest
Sep 1st, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. package app.workers;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.SQLException;
  7. import java.util.Properties;
  8. import org.apache.commons.dbcp2.BasicDataSource;
  9.  
  10. public class ConnectionDS {
  11.  
  12.     private final static String ERREUR_MSG = "Erreur ! ";
  13.  
  14.     private BasicDataSource ds;
  15.  
  16.     private String error;
  17.  
  18.     private String fileNameConfig;
  19.  
  20.     public ConnectionDS(String fileNameConfig) {
  21.         this.fileNameConfig = fileNameConfig;
  22.         ds = new BasicDataSource();
  23.         Properties prop = readProperties(fileNameConfig);
  24.         String url = prop.getProperty("DB_URL");
  25.         String user = prop.getProperty("DB_USER");
  26.         String password = prop.getProperty("DB_USER_PASSWORD");
  27.         ds.setUsername(user);
  28.         ds.setPassword(password);
  29.         ds.setUrl(url);
  30.        
  31.     }
  32.  
  33.     public Connection getConnection() {
  34.         Connection result = null;
  35.         try {
  36.             result = ds.getConnection();
  37.         } catch (SQLException ex) {
  38.             ex.printStackTrace();
  39.         }
  40.         return result;
  41.     }
  42.  
  43.     public String getError() {
  44.         return error;
  45.     }
  46.  
  47.     private Properties readProperties(String filename) {
  48.         Properties prop = new Properties();
  49.         try {
  50.             prop.load(new FileInputStream(filename));
  51.         } catch (IOException ex) {
  52.             ex.printStackTrace();
  53.         }
  54.         return prop;
  55.  
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement