Quick and easy setup of Database in Java

    Just ran into a need of a quick setup of database in Java and didn't want to spend time on File and String manipulation, fortunately came across Apache's Derby database and was able to get going within few hours and had enough time to get to other logic aspects. Started off using in-memory database and eventually moved onto a database hosted on a server without changing any of the application code.

    This implementation is based on derby jars (http://db.apache.org/derby/releases/release-10.10.1.1.cgi), derby.jar is for in-memory database and derbyclient.jar is for hosting a database as a service.

public class Database {
    Connection mConnection;
    String         mDbName;
    boolean      mAutoCommit;
    TYPE          mDatabaseType;
   
    public enum TYPE {
        IN_MEMORY,
        LOCAL_SERVER
    }
   
    public Database(String name, boolean autoCommit, TYPE databaseType) throws SQLException, ClassNotFoundException, TypeNotSupportedException, InstantiationException, IllegalAccessException
 {
        mDbName = name;
        mAutoCommit = autoCommit;
        mDatabaseType = databaseType;
       
        if (mDatabaseType == TYPE.IN_MEMORY)
        {
            // load JDBC Drivers for Java DB (In-memory database)
            String className = "org.apache.derby.jdbc.EmbeddedDriver";
            Class.forName(className).newInstance();
           
            // establish an active connection to the database
            mConnection = DriverManager.getConnection( "jdbc:derby:memory:" + mDbName +";create=true" );
        }
        else if (mDatabaseType == TYPE.LOCAL_SERVER)
        {
            // load JDBC Drivers for Java Client DB (database)
            Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
           
            // establish an active connection to the database
            mConnection = DriverManager.getConnection( "jdbc:derby://localhost:1527/" + mDbName + ";create=true" );
        }
    }
   
    // Create, update tables
    public void executeDDLStatement(String query) throws SQLException 
   {
        Statement s = mConnection.createStatement();
        s.execute(query);
    }
   
    // Select queries
    public ResultSet executeQueryStatement(String query) throws SQLException
   {
        Statement s = mConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet rs = s.executeQuery(query);
       
        return rs;
    }
   
    // Update, delete queries
    public void executeUpdateStatement(String query) throws SQLException
   {
        Statement s = mConnection.createStatement();
        s.executeUpdate(query);
    }
   
    public void commit() throws SQLException {
        mConnection.commit();    
    }
   
    public void rollback() throws SQLException {
        mConnection.rollback();
    }
   
    public void close() throws SQLException {
        mConnection.close();
    }
   

  When using derbyclient, the database server runs by default on port 1527 and can be customized and if connections should be allowed from localhost and any other server use the following.

          ...\db-derby-10.10.1.1-bin\bin\startNetworkServer -h 0.0.0.0