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

Wednesday, July 25, 2012

import package.* vs. import package.class_name

  1. import java.util.*;
  2. public class ReadArgs {
  3.     public static void main(String[] args) {
  4.         Scanner input = new Scanner(System.in);
  5.         String text = input.next();
  6.         System.out.println(text);
  7.     }
  8. }
Code 1
  1. import java.util.Scanner;
  2. public class ReadArgs {
  3.     public static void main(String[] args) {
  4.         Scanner input = new Scanner(System.in);
  5.         String text = input.next();
  6.         System.out.println(text);
  7.     }
  8. }
Code 2

When you see two codes above, you will see 1 different in the first line. What is the difference with import java.util.* and import java.util.Scanner?
  1. import java.util.* will make all classes inside the java.util package visible and can be used in the class that import the package (in this case ReadArgs class). import.java.util.Scanner will make only Scanner class will be visible in the class that import the package.
  2. There is no different performance or size after the class compiled.
  3. Better to use import package.class_name for documentation and don't forget to order by class name for readability. There are drawback for this if you don't use tools or IDE such as Netbeans or Eclipse for build the code. You will get some difficulties when delete the unused class when the application get bigger. 
So, there is no different whether you use the import package.* and import package.class_name if we see on performance, and you can use the import package.class_name for readability and documentation purpose.


References:
Packages and Import

Friday, July 13, 2012

Playing with Pseudocode - Sequence of Numbers

If I have string of numbers like this "12345" for the input, and I want the output as a sequence of 3 numbers:
  1. 123
  2. 234
  3. 345
  4. 451
  5. 512
How to convert it to become Java Program?

First, let us make the pseudo-code before trying to code instead.
  1. Declare `n` = "12345"
  2. Declare `length` = length of `n`
  3. Declare `i` as integer
  4. Loop start from `i` = 0 to `length`- 1
    1. Display sub-string of `n` from `i` to `i` + 3

And convert it to code.
  1.  public class SequenceNumber {
  2.     public static void main(String[] args) {
  3.         String n = "12345";
  4.         int length = n.length();
  5.         int i;

  6.         for(i = 0; i < length; i++) {
  7.             System.out.println(n.substring(i, i + 3));
  8.         }
  9.     }
  10. }

 And let us check the output.
  1. 123
  2. 234
  3. 345
  4. Exception in thread "main" java.lang.StringIndexOutOfBoundException: String index out of range: 6
  5.     at java.lang.String.substring(String.java:1955)
  6.     at SequenceNumber.main(SequenceNumber.java:8)

 Wow, for the first time to code and we already make an exception, nice. Let's try figure out, in the line 8 of class Sequence Number the compiler said that "String index out of range: 6" that means when `i` = 3 it will get the sub-string of `n` from 3 to 6. If you try to display the `length` it will print 5, so the problem that make this exception is `i` + 3. Let's change the pseudo-code to make it avoid the exception.
  1. Declare `n` = "12345"
  2. Declare `length` = length of `n`
  3. Declare `i` as integer
  4. Declare `x` as integer
  5. Loop start from `i` = 0 to `length` - 1
    1. Check if `i` + 3 > `length`
      1. Set `x` = length of sub-string of `n` from `i`
      2. Display sub-string of `n` from `i` concat with sub-string of `n` from 0 to 3 - `x`
    2. Else
      1. Display sub-string of `n` from `i` to `i` + 3

And make some update in the current code.
  1. public class SequenceNumber {
  2.     public static void main(String[] args) {
  3.         String n = "12345";
  4.         int length = n.length();
  5.         int i;
  6.         int x;
  7.        
  8.         for (i = 0; i < length; i++) {
  9.             if (i + 3 > length) {
  10.                 x = n.substring(i).length();
  11.                 System.out.println(n.substring(i) + n.substring(0, 3 - x));
  12.             } else {
  13.                 System.out.println(n.substring(i, i + 3));
  14.             }
  15.         }
  16.     }
  17. }

If you want to make it shorter in the code, try this one.
  1. public class SequenceNumber {
  2.     public static void main(String[] args) {
  3.         char[] digits = Integer.toString(12345).toCharArray();
  4.         for (int i = 0; i < digits.length; i++) {
  5.             System.out.println("" + digits[i] + digits[(i + 1) % digits.length] + digits[(i + 2) % digits.length]);
  6.         }
  7.     }
  8. }
 Good luck...

References:
How can get the possible 3 digit combination from a number having more than 3 digits in java.