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) { try { Properties dbProperties = loadDatabaseProperties(); String url = dbProperties.getProperty("xstore.db.url").replace("HOST", dbHost); String userpassword = dbProperties.getProperty("xstore.db." + username + ".password"); connection = DriverManager.getConnection(url, username, userpassword); logger.info("XSTORE Connection OK for user " + username + " on host " + dbHost); } catch (SQLException e) { logger.error("Failed to connect to XSTORE database for user " + username + " on host " + dbHost, e); } } 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; } }