Friday, June 19, 2009
How driver manger works internally? (in java)
Different ways of getting database connection
Thursday, June 18, 2009
Get query string from PreparedStatement
try {
String YOUR_QUERY = "SELECT * FROM table WHERE id = ? ";
Preparedstatement stamnt = con.prepareStatement( YOUR_QUERY )
stamnt.setString(1, 100);
}
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.
(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 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);
}
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.