• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class.forName(String) usage in JDBC

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In JDBC, we generally load the driver class using the below statement:

Class.forName(driverclassname);

I have queries:

i. Why do we need to load the driver class using this statement?

ii. Can't we include the driver class on our classpath and hence, can be loaded during the application startup itself along with other classes?

iii. What is the benefit we do achieve using this statement in our code?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I shall move this discussion to our databases fora.
You have to include the driver in the classpath. Otherwise the Class.forName() invocation will not find it.
The driver classes are designed to be used like that. They have static initialisers which you cannot see which load the actual database management program. These static initialisers are executed when the class is loaded, so all you need to write is Class.forName().
 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class.forname loads class dynamically at runtime.
It has static initializer which looks like
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
So after loading driver, it initializes that class properly(forname method is static).
 
Vaibhav G Garg
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cambell for your prompt response!

i am sorry but, if the class is loaded by the class loader as other classes, even then the static intializers will be run. So, I didn't get the actual benefit.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You ’re welcome
As I said, the static initialiser is run when the class is loaded. I don’t understand why you are not getting the benefit.
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This isn't needed anymore when using JDBC 4.0 drivers. All the 4.0 ones in your classpath are automagically loaded.
 
Jan Cumps
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vaibhav G Garg wrote:i. Why do we need to load the driver class using this statement?


Part of the pre-jdbc 4.0 api. The class has a static piece of code to register itself. When a class is loaded in Java, static methods are executed by the VM as part of the load activity.

You load the class -> The class runs its static methods -> Driver registered

Vaibhav G Garg wrote:ii. Can't we include the driver class on our classpath and hence, can be loaded during the application startup itself along with other classes?


From jdbc 4.0 on: yes. Pre-jdbc 4.0: no, because putting these drivers in the classpath didn't run their registration static code.


Vaibhav G Garg wrote:iii. What is the benefit we do achieve using this statement in our code?


Pre-jdbc 4.0 only:
The driver registers itself.
and when you open a jdbc connection in your program, the jdbc api will iterate over all its registered drivers, and will give the task to connect to the first one that recognizes the connection string.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't run any static methods because the class is loaded. It runs static initializer blocks, as shown in harshvardhan ojha's post.
The JDBC 4.0 mechanism uses ServiceLoader to load the Driver classes and initialize them. The current implementation it doesn't do anything with the instances, so it still requires the driver classes to register themselves in a static initializer block. Therefore, the only difference is the absence of the Class.forName calls. The rest of the mechanism stays exactly the same. And you can still use Class.forName as it will not initialize the class again if it already has been done.
 
Jan Cumps
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:It doesn't run any static methods because the class is loaded. It runs static initializer blocks, as shown in harshvardhan ojha's post....


Yes true. I must have been sleeping when I typed that. :à
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic