Advertisement
Guest User

Untitled

a guest
May 21st, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.34 KB | None | 0 0
  1. package services;
  2.  
  3. import services.utils.Logger;
  4. import java.sql.*;
  5. /**
  6.  * This class provides safe access to our database, which
  7.  * is shared among the modules.
  8.  */
  9. public class Database {
  10.    
  11.     /**
  12.      * Driver name (hardcoded to Derby at this time)
  13.      */
  14.     private String drivername = "org.apache.derby.jdbc.EmbeddedDriver";
  15.    
  16.     /**
  17.      * Connection URL (hardcodes the database name at the moment)
  18.      */
  19.     private String connection = "jdbc:derby:services_db;create=true";
  20.  
  21.  
  22.     /**
  23.      * Represents the database connection
  24.      */
  25.     private Connection conn;
  26.     /**
  27.      * The shared Database instance
  28.      */
  29.     private static final Database instance;
  30.  
  31.     static {
  32.         instance = new Database();
  33.     }
  34.    
  35.     /**
  36.      * Nobody can construct this class but us, allowing for safe
  37.      * startup and shutdown, avoiding corruption and safe shared
  38.      * access among the modules
  39.      */
  40.     private Database() {
  41.         Logger.log(Logger.INFO, "Probing database driver..");
  42.         try {
  43.             Class.forName(drivername);
  44.         } catch (ClassNotFoundException ex) {
  45.             Logger.log(Logger.FATAL, "Database driver not installed! Aborting!");
  46.             System.exit(-1);
  47.         }
  48.        
  49.         Logger.log(Logger.INFO, "Connecting to database..");
  50.         try {
  51.             conn = DriverManager.getConnection(connection);
  52.         } catch (SQLException ex) {
  53.             Logger.log(Logger.FATAL, "Database could not be initialised, aborting!");
  54.             System.exit(-1);
  55.         }
  56.         // We can only make it this far if we've got the driver ;)
  57.         Runtime.getRuntime().addShutdownHook(new Thread() {
  58.             public void run() {
  59.                 shutdown();
  60.             }
  61.         });
  62.         Logger.log(Logger.INFO, "Database initialised");
  63.        
  64.         // check sanity of database.
  65.         try {
  66.             Statement s = conn.createStatement();
  67.             ResultSet res = s.executeQuery("SELECT * FROM accounts");
  68.         } catch (SQLException ex) {
  69.             Logger.log(Logger.ERROR, "Tables not found - recreating");
  70.             createTables();
  71.             createHostsTable();
  72.         }
  73.     }
  74.    
  75.     /**
  76.      * (Re)create the account tables in the database
  77.      */
  78.     private void createTables() {
  79.         String create_accounts = ""
  80.         + "CREATE TABLE accounts"
  81.         + "("
  82.         + "ID int NOT NULL GENERATED ALWAYS AS IDENTITY,"
  83.         + "NAME varchar(40) NOT NULL,"
  84.         + "EMAIL varchar(255) NOT NULL,"
  85.         + "PASS varchar(50) NOT NULL,"
  86.         + "PRIMARY KEY (ID)"
  87.         + ")";
  88.         try {
  89.             Statement s = conn.createStatement();
  90.             s.execute(create_accounts);
  91.         } catch (SQLException ex) {
  92.             Logger.log(Logger.FATAL, "Could not create account tables, aborting!");
  93.             System.exit(-1);
  94.             ex.printStackTrace();
  95.         }
  96.     }
  97.    
  98.     /**
  99.      * (Re)create the vhosts table
  100.      */
  101.     private void createHostsTable() {
  102.         String create_hosts = ""
  103.         + "CREATE TABLE hosts"
  104.         + "("
  105.         + "ID int NOT NULL,"
  106.         + "HOST varchar(255) NOT NULL,"
  107.         + "CONFIRMED CHAR NOT NULL"
  108.         + ")";
  109.         try {
  110.             Statement s = conn.createStatement();
  111.             s.execute(create_hosts);
  112.         } catch (SQLException ex) {
  113.             Logger.log(Logger.FATAL, "Could not create vhost tables, aborting!");
  114.             ex.printStackTrace();
  115.         }
  116.     }
  117.    
  118.     /**
  119.      * Returns the safe-shared Database instance
  120.      */
  121.     public static final Database getInstance() {
  122.         return instance;
  123.     }
  124.    
  125.     /**
  126.      * Returns a ResultSet or null, tries to execute the query
  127.      * making Database a factory class
  128.      * @param query the query to execute
  129.      * @return ResultSet or null
  130.      * @throws java.net.SQLException if there is an error.
  131.      */
  132.     public ResultSet executeQuery(String query) throws SQLException {
  133.         Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  134.         return st.executeQuery(query);
  135.     }
  136.    
  137.     /**
  138.      * Shorthand access to Statement.executeUpdate
  139.      * @param sql The SQL to send to the DB
  140.      * @return an integer showing how many (if any) rows were updated
  141.      * @throws java.net.SQLException if there is any sql error
  142.      */
  143.     public int executeUpdate(String sql) throws SQLException {
  144.         Statement st = conn.createStatement();
  145.         return st.executeUpdate(sql);
  146.     }
  147.     /**
  148.      * Shutdown the database.
  149.      */
  150.     private void shutdown() {
  151.         boolean clean = false;
  152.         try {
  153.             Connection c = DriverManager.getConnection("jdbc:derby:;shutdown=true");
  154.         } catch (SQLException ex) {
  155.             if(ex.getSQLState().equals("XJ015")) {
  156.                 clean = true;
  157.             }
  158.         }
  159.         if (!clean) {
  160.             System.out.println("Database did not shut down normally");
  161.             Logger.log(Logger.FATAL, "Database did not shut down cleanly");
  162.         }  else  {
  163.             Logger.log(Logger.INFO, "Database shut down");
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement