57 lines
1.9 KiB
Java
57 lines
1.9 KiB
Java
package com.example.services;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.SQLException;
|
|
import java.util.Properties;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class DatabaseConnectXSTORE implements AutoCloseable {
|
|
private static final Logger logger = LoggerFactory.getLogger(DatabaseConnectXSTORE.class);
|
|
|
|
private Connection connection;
|
|
|
|
public DatabaseConnectXSTORE(String dbHost, String username) throws SQLException {
|
|
Properties dbProperties = loadDatabaseProperties();
|
|
|
|
String url = dbProperties.getProperty("xstore.db.url").replace("HOST", dbHost);
|
|
String userpassword = dbProperties.getProperty("xstore.db." + username + ".password");
|
|
|
|
// Initialiser votre connexion à la base de données ici
|
|
connection = DriverManager.getConnection(url, username, userpassword);
|
|
|
|
logger.info("XSTORE Connection OK for user " + username + " on host " + dbHost);
|
|
}
|
|
|
|
public Connection getConnection() {
|
|
return connection;
|
|
}
|
|
|
|
@Override
|
|
public void close() throws SQLException {
|
|
if (connection != null && !connection.isClosed()) {
|
|
connection.close();
|
|
logger.info("XSTORE Connection closed");
|
|
}
|
|
}
|
|
|
|
private Properties loadDatabaseProperties() {
|
|
Properties dbProperties = loadProperties("db.properties");
|
|
return dbProperties;
|
|
}
|
|
|
|
private Properties loadProperties(String fileName) {
|
|
Properties properties = new Properties();
|
|
try (InputStream input = getClass().getClassLoader().getResourceAsStream(fileName)) {
|
|
properties.load(input);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
// Handle the exception correctly for your application
|
|
}
|
|
return properties;
|
|
}
|
|
} |