Skip to main content

db4j: Simple Database API for Java

Posted by aschunkjava on September 28, 2007 at 12:04 PM PDT

db4j: A simple Database API

If you are working a lot with Java DB then you repeatedly perform a few tasks every time and some only once.

For instance establishing a connection with a database and open a table of database usually happens at least once until you are working with more than one database. With JDBC you need to do the following for establishing the connection:

 try{ 
    conn = DriverManager.getConnection(dbname, username, password);
         return SQL_DB_OPEN_ERROR;
      }catch(SQLException e){
          System.out.println("Error! Unable to connect to " + dbname);
          System.out.println("Cause: " + e.getSQLState());
      }

Given a Connection object called conn the code above is used for setting up a connection with a database with your username and password.

With db4j you can connect to a database as follows:

DBConnection db = new DBConnection();
String dbname = "BeispielDB.txt";
String usrname = "admin";
String password = "adminadmin";
String constring = "sun.jdbc.odbc.JdbcOdbcDriver";
Connection conn;
try{
   db.connect(conn,constring, "admin", "adminadmin");
        db.open(conn, constring, usrname, password);
       }catch(Exception e){
          
       }

This simplifies the connection process to creating an object of class DBConnection and calling its open() method with a few parameters.

The DBConnection class offers basic methods for connecting to a database, open a database and close the connection.

To reduce programming with JDBC or derby db4j offers a set of classes that consists of the following classes:

  • DBConnection
  • DBActions
  • DBDriverInfo
  • DBLogger
  • DBMetadata

The name of these classes are almost self describing. The DBActions class provides database transactions such as Auto-Commit, Rollback etc.

Note that your DBMS might not support all these featurs so have to consult your DBMS documentation.

DBLogger is a simple Logger class that uses JDBC Logger methods. The DBDriverInfo provides information about the database driver.

In a future release i will add a statement class and some utility methods for retrieving and setting database values.

Related Topics >>