• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading a driver

 
Parijat Mukherjee
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parijat,
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,
Avi.
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parijat,
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
Class.forName("foo.bah.Driver")

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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic