1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class MyConnection {
Properties properties;
private String url;
private String login;
private String password;
private Connection connection;
private static MyConnection instance;
private MyConnection() {
if (connection == null) {
properties = new Properties();
try {
properties.load(new FileInputStream(new File("Configuration.properties")));
url = properties.getProperty("url");
login = properties.getProperty("utilisateur");
password = properties.getProperty("pwd");
connection = DriverManager.getConnection(url, login, password);
System.out.println("Connection established");
} catch (SQLException | IOException ex) {
Logger.getLogger(MyConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public Connection getConnection() {
return connection;
}
public static MyConnection getInstance() {
if (instance == null) {
instance = new MyConnection();
}
return instance;
}
} |
Partager