Tuesday, August 14, 2012

CrzDB vs Standard JDBC

I tried to make a library that has function for CRUD (Create, Read, Update and Delete) to simplify database connection with JDBC. Let see the example so we can understand what is the main point of the CrzDB:

First create database named "tryout" in MySQL:
  1. mysql> create database tryout;
  2. mysql> use tryout;

And create the table "user":
  1. mysql> create table `user`(
  2.         -> `user_name` varchar(100) NOT NULL,
  3.         -> `address` varchar(200) DEFAULT NULL,
  4.         -> `first_name` varchar(50) NOT NULL,
  5.         -> `last_name` varchar(50) DEFAULT NULL,
  6.         -> `middle_name` varchar(50) DEFAULT NULL,
  7.         -> PRIMARY KEY (`user_name`));
After that insert one record for testing:
  1. mysql> INSERT INTO `user` VALUES('crazenezz', NULL, 'craze', NULL, NULL);

If we using Standard JDBC we will see this kind of code to get all the user.
  1. import com.mysql.jdbc.Connection;
  2. import com.mysql.jdbc.Statement;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. public class SimpleDBConnection {
  9.     public static void main(String[] args) {
  10.         try {
  11.             Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/tryout", "root", "root");           
  12.             Statement state = (Statement) conn.createStatement();
  13.             ResultSet rSet = state.executeQuery("SELECT * FROM user");
  14.             while(rSet.next()) {
  15.                 System.out.println("Username: " + rSet.getString("user_name"));
  16.                 System.out.println("First Name: " + rSet.getString("first_name"));
  17.             }
  18.         } catch (SQLException ex) {            Logger.getLogger(SimpleDBConnection.class.getName()).log(Level.SEVERE, null, ex);
  19.         }
  20.     }
  21. }
And let see if we use the CrzDB:
  1. import java.util.Map;
  2. import org.crz.db.config.Config;
  3. import org.crz.db.config.abs.impl.MySQLConfig;
  4. import org.crz.db.query.SQLQuery;
  5. public class CustomDBConnection {
  6.     public static void main(String[] args) {
  7.         Config config = new Config(MySQLConfig.getInstance("tryout"));
  8.         SQLQuery query = new SQLQuery(config.getState(), "user");
  9.         List<Map> datas = query.getAll();
  10.         for(Map data : datas) {
  11.             System.out.println("Username: " + data.get("user_name"));
  12.             System.out.println("First Name: " + data.get("first_name"));
  13.         }
  14.         config.closeConnection();
  15.     }
  16. }
It can save your time for query and you don't need to know and memorize every queries. For now CrzDB support MySQL, PostgreSQL and Oracle for database connection, in the future I want to expand the connection to other databases. And no need to change for the query inside if we want to immigrate to other database e.q from MySQL to Oracle or PostgreSQL to DB2. Because in my opinion application programmer don't need to be like database programmer.
 
If you more interested about this library you can check it out from here. and you can see another example in here.

I'm using:
  • Netbeans 7.1.1 as IDE
  • JDK 1.7.0_05