It’s one of the very common/interesting/tricky interview question. From my experience on JDBC interviews, 99.9% you would be questioned this. Even I have asked this question to few of them. Though it’s a pretty famous question not everyone knows the exact explanation. Remember as a beginner I’m always referring to another beginner.
No the above one is not the actual question. But usually it’s beginning of our question. Let us see the steps involved in establishing JDBC connection.
· For god sake you need load the JDBC driver using class.forName()
· class.forName() is used for dynamic class loading.
· forName() should be catched with ClassNotFoundException
· Establish the connection using DriverManager.getConnection()
· getConnection() accepts jdbc url, db User name and db Password
· getConnection() should be catched with SqlException
· That’s it. getConnection() will returns you the connection
And here is code snippet,
From the above brief steps & code I can ask millions of questions. Few are,
But this post is talking about registering JDBC driver. Yes to establish JDBC connection, we need to register the driver before.
Yes in the above code you are only loading the class and getting the connection. Did you know the answer? Hmmm yes. You are right. Actually loading a class interns take care of registering the driver. Let us go inside little on class loader.
The Java Class loader is a part of the Java Runtime Environment that loads Java classes into the Java Virtual Machine means loads the classes into memory. When the class is being loaded, our JVM automatically executes the static block present in the class. Remember static stuff never require an instance
Here when we load the JDBC driver using class.forName(), JVM executes static block presents in driver class. And the registerDriver() api would in invoked within the static block.
Hope this would help you.
· For god sake you need load the JDBC driver using class.forName()
· class.forName() is used for dynamic class loading.
· forName() should be catched with ClassNotFoundException
· Establish the connection using DriverManager.getConnection()
· getConnection() accepts jdbc url, db User name and db Password
· getConnection() should be catched with SqlException
· That’s it. getConnection() will returns you the connection
And here is code snippet,
Connection con = null;try {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");con = DriverManager.getConnection("jdbc:odbc:dumy", "sa", "");} catch( Exception e ) {e.printStackTrace();}
1) Why we need to load the driver here?
2) What’s the difference between static and dynamic class loaders?
3) What are the class loaders available?
4) Difference between ClassNotFoundException and NoClassFoundError?
5) Why we need to try catch to the class.forName?
6) What is meant by checked exception? etc etc
2) What’s the difference between static and dynamic class loaders?
3) What are the class loaders available?
4) Difference between ClassNotFoundException and NoClassFoundError?
5) Why we need to try catch to the class.forName?
6) What is meant by checked exception? etc etc
Where you are registering the JDBC driver? (OR) How your driver registered here?
Yes in the above code you are only loading the class and getting the connection. Did you know the answer? Hmmm yes. You are right. Actually loading a class interns take care of registering the driver. Let us go inside little on class loader.
The Java Class loader is a part of the Java Runtime Environment that loads Java classes into the Java Virtual Machine means loads the classes into memory. When the class is being loaded, our JVM automatically executes the static block present in the class. Remember static stuff never require an instance
Here when we load the JDBC driver using class.forName(), JVM executes static block presents in driver class. And the registerDriver() api would in invoked within the static block.
static {
DriverManager.registerDriver(new JdbcOdbcDriver());
}
0 comments:
Post a Comment