Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DatabaseConnection {
- private final static Logger logger = LoggerFactory.getLogger(DatabaseConnection.class);
- private static DatabaseConnection instance;
- private Jdbi dbConnection; // wraps a JDBC DataSource, it is the main configuration for the database.
- private final String dbUrl; // the JDBC URL that is needed to create a connection.
- private final String dbUsername; // the database username needed to create a connection.
- private final String dbPassword; // the database password needed to create a connection.
- /**
- * Creates a DatabaseConnection object with a null connection to the database until obj.connect() is implicitly
- * called. The JDBC URL, database username, and database password will be pulled from the database.properties file
- * at the time of creation, however.
- */
- private DatabaseConnection() {
- this.dbUrl = DatabaseConfig.getInstance().getString("db.jdbc.url");
- this.dbUsername = DatabaseConfig.getInstance().getString("db.uid");
- this.dbPassword = DatabaseConfig.getInstance().getString("db.pwd");
- }
- /**
- * Establishes an entry-point to the database by creating a new Jdbi instance, which wraps a JDBC DataSource and
- * serves as the main configuration point for the database session.
- */
- public void connect() {
- if (this.dbUrl == null || this.dbUrl.isEmpty()) {
- throw new RuntimeException("Invalid dbUrl, check that 'db.jdbc.url' in 'database.properties' is valid.");
- }
- loadJdbcDriver(); // Loads the necessary JDBC connector for establishing database connections.
- logger.info("Connecting to " + getDatabaseName() + " at " + getDatabaseHost() + ":" +
- getPortNumber() + " for user '" + this.dbUsername + "'");
- // Creates the database session connection.
- this.dbConnection = Jdbi.create(this.dbUrl, this.dbUsername, this.dbPassword);
- // Loads the Jdbi Plugin needed for SQL Object Annotations.
- this.dbConnection.installPlugin(new SqlObjectPlugin());
- }
- private void loadJdbcDriver() {
- try {
- Class.forName(DatabaseConfig.getInstance().getString("db.jdbc.driver"));
- } catch (ClassNotFoundException e){
- ErrorLog.getLogger().error("The JDBC driver could not be found. " +
- "Check that 'db.jdbc.driver' in database.properties is valid.");
- }
- }
- public Jdbi getDbConnection() {
- return this.dbConnection;
- }
- /**
- * Retrieves the database name from the JDBC URL.
- * @return the database name.
- */
- public String getDatabaseName() {
- String[] splitURL = this.dbUrl.split("/");
- return splitURL[splitURL.length - 1];
- }
- /**
- * Retrieves the port number for the database from the JDBC URL.
- * @return the database port number.
- */
- public String getPortNumber() {
- String[] splitURL = this.dbUrl.split("/");
- return splitURL[2].split(":")[1];
- }
- /**
- * Retrieves the database hostname from the JDBC URL.
- * @return the database hostname.
- */
- public String getDatabaseHost() {
- String[] splitURL = this.dbUrl.split("/");
- return splitURL[2].split(":")[0];
- }
- /**
- * If DatabaseConnection hasn't been initialized yet, a new object will be created. Otherwise the single permitted
- * instance of DatabaseConnection will be returned.
- * @return the single instance of DatabaseConnection
- */
- public static DatabaseConnection getInstance() {
- if (instance == null) {
- instance = new DatabaseConnection();
- }
- return instance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement