Saturday, August 24, 2013

Prepared Statement and Precompilation (What is precompilation?)


JDBC driver sends a special statement (i.e. PreparedStatement) to DB to get it executed.

 

Now, it’s up to DB level settings whether it will store the query in DB’s cache or not and it has nothing to do with prepared statement. Actually Driver sends the request to DBMS that I need you to give me the parsed/compiled query when I send it for the first time. This 'compiled' query object will be stored in PreparedStatement object (in Java). Mind here that if you use same query with different PreparedStatement object, it will again be parsed :) See the example below:

 

PreparedStatement ps1 = con.prepareStatement("Update Employee set name = ? where id = ?");  // In this statement the parsed or in fact compiled query is stored in ps1 regardless DB caches it nor not.

 

ps1.setString(1, "Bob");

ps1.setInt(2,5);

ResultSet rs = ps1.executeQuery();

 

ps1.setString(1, "Robert");

ps1.setInt(2,6);

rs = ps1.executeQuery();

 

In both above scenarios query is already compiled and just parameter is set which gives you good performance,

 

PreparedStatement ps1 = con.prepareStatement("Update Employee set name = ? where id = ?"); // In this statement the query is parsed again - mind the word – “again".  This compiled query object is stored in ps2.

 

And if you want to use normal Statement object

 

normalStatement.executeUpdate("Update Employee set name = ‘Bob’ where id = 6");

In above case query will be parsed/compiled on runtime only 'again'.

Tuesday, September 15, 2009

Web Server and Application Server

Web Server: It is a server which host web application. Form Client Point of View, Server know only two thing:

1.HTTP Request
2.HTTP Response

Application Server: It is a server which host application. From client point of View, Application server is who provide them a way to interact business application. By use of Application Server and business application, client can ask question and get answers (Invoke methods and get Results).Application server can be any language like .net application server, Mainframe Application Server, J2EE application Server.

J2EE compliant Application Server: It is an application server who meets the J2EE specifications.

Converting a Chinese/ other language string into Unicode (UTF-16)

String b = --some chinese or other language text ---

StringBuffer str = new StringBuffer();
for(int i=0; i<b.length(); i++)
{
str.append(Integer.toHexString(b.charAt(i)));
}
System.out.println(" changed:--- "+str.toString());

Tuesday, September 8, 2009

Redirect And Forward

Redirect Versus Forward

1. Redirect sends the request back to browser and all previous request context and parameters are lost and a new request will be send to redirected page.
Forward does the decorator work for the request and can add more parameters to the request and send to the forwarded page.
2. Redirect is slower than Forward
3. Redirect will show the redirected page URL in address bar. Forward will not.
4. On refresh, only redirected page will be called not the page which has redirected i.e. if the flow is from A to B then on refresh only B will be called.
But in case of forward, if the flow is A to B then on refresh First A and then B will be called in the same sequence as occurred before.
5. Redirect can be used to send the request to another running web application. Forward will work within a web application only.
6. Redirect is done when servlet’s service method get completed i.e. statements after redirect call will execute first and after completion of service method redirect will occur.
But forward does the work of a method call i.e. the statements after forward call be executed when the forwarded page has done its work.
Example with a code snippet.
FirstServlet.java
...
System.out.println("Before Next Page");
response.sendRedirect("NextServlet");
System.out.println("After Next Page");

In NextServlet.java
System.out.println("Next Servlet called....");

In this case following will be the output:
Before Next Page
After Next Page
Next Servlet Called

I.e. in case of redirect first servlet ‘s service method will complete and then next servlet is called..
Now if we could have used forward instead of sendRedirect then following will be the output
Before Next Page
Next Servlet Called
After Next Page

Declaring and iterating over 2D array in javascripts.

Declaring and iterating over 2D array in javascripts.

A Code snippet. Self explainatory.

var b = new Array( 2 );
b[ 0 ] = new Array( 3 );
b[ 1 ] = new Array( 3 );

for ( var i in grades )
{

for ( var j in grades[ i ] )
{


}
}

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.