This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes Loading a driver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Loading a driver" Watch "Loading a driver" New topic

Loading a driver

Parijat Mukherjee

Joined: Sep 21, 2003
Posts: 26
what is the diff between a new operator and using class.forname()
what object does class.forname() return during loading a driver
please some one help me in buiding these concepts in JDBC suggest some books also ....
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1121

I suggest starting with the JDBC Trail in the Java Tutorial, followed by the JDBC Getting Started Guide, followed by the documentation for the JDBC driver you are using (which, hopefully, is not the JDBC-ODBC bridge driver that comes with the J2SE SDK).
Good Luck,
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
You can do a "Class.forName(...)" or a "new ..." and both will work. I think the first is the preferred method because it saves the initial overhead of actually instantiating an object.
If you look at the documentation for the "java.sql.Driver" interface it states that

When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager. This means that a user can load and register a driver by calling

Doing a "Class.forName()" causes the ClassLoader to find the driver class and load it into the JVM. The tack taken by many [most? all?] drivers is to include a "static { ... }" block of code that will register an instance of the driver with the "DriverManager" class. So all of that work is done "automatically" by the driver class itself.
The "Class.forName()" call actually returns an object of type "java.lang.Class", so you could do:
Class myDriver = Class.forName("foo.bah.Driver");
But since you don't really need to use "myDriver" for anything, most people leave off the left-hand side assignment and just do a "Class.forName(...)";
The links that Avi provided are a good place to get started. Good luck!
Consider Paul's rocket mass heater.
subject: Loading a driver
Similar Threads
What is the difference between Class.forName() and DriverManager.registerDriver() ?
some confusions about jdbc...
login in to a mysql database
Doubt regarding Class.forName() in JDBC connection