Advertisement
Guest User

DatabaseConnection.java

a guest
Oct 2nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. public class DatabaseConnection {
  2.     private final static Logger logger = LoggerFactory.getLogger(DatabaseConnection.class);
  3.     private static DatabaseConnection instance;
  4.  
  5.     private Jdbi dbConnection; // wraps a JDBC DataSource, it is the main configuration for the database.
  6.  
  7.     private final String dbUrl; // the JDBC URL that is needed to create a connection.
  8.     private final String dbUsername; // the database username needed to create a connection.
  9.     private final String dbPassword; // the database password needed to create a connection.
  10.  
  11.     /**
  12.      * Creates a DatabaseConnection object with a null connection to the database until obj.connect() is implicitly
  13.      * called. The JDBC URL, database username, and database password will be pulled from the database.properties file
  14.      * at the time of creation, however.
  15.      */
  16.     private DatabaseConnection() {
  17.         this.dbUrl = DatabaseConfig.getInstance().getString("db.jdbc.url");
  18.         this.dbUsername = DatabaseConfig.getInstance().getString("db.uid");
  19.         this.dbPassword = DatabaseConfig.getInstance().getString("db.pwd");
  20.     }
  21.  
  22.     /**
  23.      * Establishes an entry-point to the database by creating a new Jdbi instance, which wraps a JDBC DataSource and
  24.      * serves as the main configuration point for the database session.
  25.      */
  26.     public void connect() {
  27.         if (this.dbUrl == null || this.dbUrl.isEmpty()) {
  28.             throw new RuntimeException("Invalid dbUrl, check that 'db.jdbc.url' in 'database.properties' is valid.");
  29.         }
  30.  
  31.         loadJdbcDriver(); // Loads the necessary JDBC connector for establishing database connections.
  32.  
  33.         logger.info("Connecting to " + getDatabaseName() + " at " + getDatabaseHost() + ":" +
  34.                 getPortNumber() + " for user '" + this.dbUsername + "'");
  35.  
  36.         // Creates the database session connection.
  37.         this.dbConnection = Jdbi.create(this.dbUrl, this.dbUsername, this.dbPassword);
  38.  
  39.         // Loads the Jdbi Plugin needed for SQL Object Annotations.
  40.         this.dbConnection.installPlugin(new SqlObjectPlugin());
  41.     }
  42.  
  43.     private void loadJdbcDriver() {
  44.         try {
  45.             Class.forName(DatabaseConfig.getInstance().getString("db.jdbc.driver"));
  46.         } catch (ClassNotFoundException e){
  47.             ErrorLog.getLogger().error("The JDBC driver could not be found. " +
  48.                     "Check that 'db.jdbc.driver' in database.properties is valid.");
  49.         }
  50.     }
  51.  
  52.     public Jdbi getDbConnection() {
  53.         return this.dbConnection;
  54.     }
  55.  
  56.     /**
  57.      * Retrieves the database name from the JDBC URL.
  58.      * @return the database name.
  59.      */
  60.     public String getDatabaseName() {
  61.         String[] splitURL = this.dbUrl.split("/");
  62.  
  63.         return splitURL[splitURL.length - 1];
  64.     }
  65.  
  66.     /**
  67.      * Retrieves the port number for the database from the JDBC URL.
  68.      * @return the database port number.
  69.      */
  70.     public String getPortNumber() {
  71.         String[] splitURL = this.dbUrl.split("/");
  72.  
  73.         return splitURL[2].split(":")[1];
  74.     }
  75.  
  76.     /**
  77.      * Retrieves the database hostname from the JDBC URL.
  78.      * @return the database hostname.
  79.      */
  80.     public String getDatabaseHost() {
  81.         String[] splitURL = this.dbUrl.split("/");
  82.  
  83.         return splitURL[2].split(":")[0];
  84.     }
  85.  
  86.     /**
  87.      *  If DatabaseConnection hasn't been initialized yet, a new object will be created. Otherwise the single permitted
  88.      *  instance of DatabaseConnection will be returned.
  89.      * @return the single instance of DatabaseConnection
  90.      */
  91.     public static DatabaseConnection getInstance() {
  92.         if (instance == null) {
  93.             instance = new DatabaseConnection();
  94.         }
  95.  
  96.         return instance;
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement