61 lines
2.0 KiB
Java
61 lines
2.0 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 DatabaseConnectXADMIN implements AutoCloseable {
|
|
private static final Logger logger = LoggerFactory.getLogger(DatabaseConnectXADMIN.class);
|
|
|
|
private Connection connection;
|
|
|
|
public DatabaseConnectXADMIN(String username) throws SQLException {
|
|
try {
|
|
Properties dbProperties = loadDatabaseProperties();
|
|
|
|
String url = dbProperties.getProperty("xadmin.db.url");
|
|
String userpassword = dbProperties.getProperty("xadmin.db." + username + ".password");
|
|
|
|
connection = DriverManager.getConnection(url, username, userpassword);
|
|
|
|
logger.info("XADMIN Connection OK for user " + username);
|
|
} catch (SQLException e) {
|
|
logger.error("Failed to connect to XADMIN database for user " + username, e);
|
|
throw e; // re-throw the exception
|
|
}
|
|
}
|
|
|
|
public Connection getConnection() {
|
|
return connection;
|
|
}
|
|
|
|
@Override
|
|
public void close() throws SQLException {
|
|
if (connection != null && !connection.isClosed()) {
|
|
connection.close();
|
|
logger.info("XADMIN 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;
|
|
}
|
|
} |