Friday, June 19, 2009

How driver manger works internally? (in java)

DriverManager has a static registry of JDBC drivers. It has static methods registerDriver and unregisterDriver. When you load a JDBC Driver class, it calls the registerDriver method and registers itself. As many drivers can be registered to the DriverManager as you want. The order of registered drivers is significant. Because when you pass the connectionURL to the method, DriverManager.getConnection(), then it calls acceptsURL() one by one on each driver and the first driver which returns true, is used to get a connection.

Different ways of getting database connection

There are two ways of getting JDBC connection in java. One is from Driver Manager and the other is from an object of javax.sql.DataSource. The data source can be obtained from JNDI look up or by any other means. The implementation of Data Source internally uses the DriverManager but can pool the connections and reuse them and can give wrappers on actual connection so that when you call the close method, the connection is returned to the pool instead of actually closing the underlying connection.

Thursday, June 18, 2009

Get query string from PreparedStatement

When performing an SQL query, If you would like to catch any SQLException and throw a RunTimeException containing the failed query, like:

try {
String YOUR_QUERY = "SELECT * FROM table WHERE id = ? ";
Preparedstatement stamnt = con.prepareStatement( YOUR_QUERY )
stamnt.setString(1, 100);
stamnt.executeUpdate( );
}
catch (SQLException exc) {
throw new RuntimeException( );
}

where query would be "SELECT * FROM TABLE WHERE id = 100". However, it it is not possible to retrieve this query from a prepared statement.
There are two ways to solve this problem:-

(1) the easiest way is encapsulate the parameters and query string in the exception. It will not solve your problem 100%, but at least it will be enough descriptive to handle the error. Code snippet as below-

String param = 100;
String YOUR_QUERY = "SELECT * FROM table WHERE id = ? ";
try {
Preparedstatement stamnt = con.prepareStatement( YOUR_QUERY );
stamnt.setString(1, param);
stamnt.executeUpdate( );
}
catch (SQLException exc) {
throw new RuntimeException(YOUR_QUERY + “ parameters are :- ” + param );
}

(2) You can create a utility method in which we will pass the query string and list of parameters. Simply replace all the ‘?’ with corresponding parameters. Code snippet as below-

String param = 100;
String YOUR_QUERY = "SELECT * FROM table WHERE id = ? ";
try {
stamnt.setString(1, param);
Preparedstatement stamnt = con.prepareStatement( YOUR_QUERY );
stamnt.executeUpdate( );
}
catch (SQLException exc) {
String query = getQueryString(QUERY_STRING, param);
throw new RuntimeException(query);
}

String getQueryString(String query, Object[] params) {
    int idx = query.indexOf("?");
    int count = 0;
    while (idx != -1) {
        Object value = params[count++];
        if (value instanceof String) {
            value = "'" + value.toString() + "'";
        }
        query = query.replaceFirst("\\?", value.toString());
        idx = query.indexOf("?");
    }
    return query;
}



Wednesday, June 17, 2009

Software Engineering and Social Life

We all know that Society and technology are tightly coupled together. For a layman technology means some mechanical or chemical or electrical or medical inventions but not software engineering.

But software industry too can contribute a lot to society that can have an impact on social life of human being. You can clearly imagine the way people connect with each other. Most of the people like me are now connected to their peers with this most obvious invention of software industry – the internet.

There are lots n lots of social networking site through which we can remain in touch with our friends and family members and take regular updates. Sites like http://www.facebook.com/ and http://www.orkut.com/ are most famous social networking sites. Similarly there are lots of professional networking sites and http://www.linkedin.com/ is the most effective of all.