Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app.workers;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.Properties;
- import org.apache.commons.dbcp2.BasicDataSource;
- public class ConnectionDS {
- private final static String ERREUR_MSG = "Erreur ! ";
- private BasicDataSource ds;
- private String error;
- private String fileNameConfig;
- public ConnectionDS(String fileNameConfig) {
- this.fileNameConfig = fileNameConfig;
- ds = new BasicDataSource();
- Properties prop = readProperties(fileNameConfig);
- String url = prop.getProperty("DB_URL");
- String user = prop.getProperty("DB_USER");
- String password = prop.getProperty("DB_USER_PASSWORD");
- ds.setUsername(user);
- ds.setPassword(password);
- ds.setUrl(url);
- }
- public Connection getConnection() {
- Connection result = null;
- try {
- result = ds.getConnection();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return result;
- }
- public String getError() {
- return error;
- }
- private Properties readProperties(String filename) {
- Properties prop = new Properties();
- try {
- prop.load(new FileInputStream(filename));
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- return prop;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement