Monday, January 16, 2012

Nice way to clean up JDBC resources in JDBC 4.1 (Java 7)

I was working on a small prototype of an application and had the luxury of working on top of Java 7. I was struck by how much boiler plate code we can avoid when working with JDBC.

Following is the code sample to read from a table using PreparedStatement.

try(
 Connection connection = ServiceLocator.getConnection;
 PreparedStatement stmnt = connection.prepareStatement(YOUR_SELECT_QUERY_HERE);
){
   stmnt.setString( 1, parameterValue1);
   stmnt.setString( 2, parameterValue2);


   ResultSet rs = stmnt.executeQuery();
   while(rs.next()){
   ......

   }
}


And this is it. No long, ugly and trite finally blocks to close connection, statements and resultsets.

Wednesday, February 16, 2011

Trying my hands on Scala.

Finally taken up Scala. I am very interested in learning the functional programming, there seems to be lots of languages available. I choose Scala because it is a typed language and run on JVM, and, of course, few people also suggested it to me.

Hello to the blog world.

I am going to be posting stuff going on in my life, or something I find interesting around. This blog is just sort of an online diary for myself.