Skip to content

Instantly share code, notes, and snippets.

@Angel-Dijoux
Created March 30, 2023 07:05
Show Gist options
  • Save Angel-Dijoux/9bc4aff93ecfa9bc75546511fec70cd4 to your computer and use it in GitHub Desktop.
Save Angel-Dijoux/9bc4aff93ecfa9bc75546511fec70cd4 to your computer and use it in GitHub Desktop.
Connection Provider
package com.hopitalpharmacie.db;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Logger;
import javax.sql.DataSource;
public class ConnectionProvider {
private static final Logger logger = Logger.getLogger(ConnectionProvider.class.getName());
private static DataSource dataSource;
private ConnectionProvider() {
throw new java.lang.UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
static {
dataSource = DataSourceProvider.getDataSource();
}
public static Connection getConnection() throws SQLException {
logger.info("\u001B[32m" + "Connection was opened" + "\u001B[0m");
return dataSource.getConnection();
}
public static void closeConnection(Connection connection) {
if (connection != null) {
try {
connection.close();
logger.info("\u001B[32m" + "Connection was closed" + "\u001B[0m");
} catch (SQLException e) {
logger.warning("\u001B[31m" + e + "\u001B[0m");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment