File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Why we require Class.forName() multiple times Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Why we require Class.forName() multiple times" Watch "Why we require Class.forName() multiple times" New topic

Why we require Class.forName() multiple times

as chhipa

Joined: Sep 04, 2006
Posts: 26
Dear all,
I read in books that Class.forName("driverClass") loads the jdbc driver in java virtual machene and Driver.getConnection("url") establishes connection to the database.

My question is if I have two jdbc programs to interact with the same database why do I need to Class.forName("driverClass") in both the programs.
Instead I could use Class.forName("driverClass") in first program and Driver.getConnection("url") in both the programs because the driver class is already loaded in the JVM through the first program.
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
The Class.forName() registers the driver in the current JVM process and it stays registered in that process until it terminates.

It does not register the driver globally on the computer for all processes to see. The registration does not have any effect outside that single process.

You mention that you have two programs. I assume that means each program is running in it's own JVM process. The two processes are independent of each other. What is registered in one process is not visible in the other. Each program will need to register the driver within it's instance of the JVM.

Am I explaining this clearly?
Arun Maalik
Ranch Hand

Joined: Oct 25, 2005
Posts: 216
well see Class.forName() method load the driver for current running instance only not globaly so you need two class.forName() method to load driver
I agree. Here's the link:
subject: Why we require Class.forName() multiple times
It's not a secret anymore!