• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The Class class

 
Nicholas Carrier
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This could possibly go into the beginner section, but since I'm just learning JDBC I figure I'll post it here.

There's a line of code that says

Class.forName(driver).newInstance();


Now, what that looked like to me was a Static Method being called from the Class class.

So I went to the API for Class and found that Class.forName()

Returns the Class object associated with the class or interface with the given string name.


So that means that it returns the class object associated with the given String name, which in this case is the String driver, and then calls newIntance(); on the Class object that was returned from the forName(method).

The problem is, is that I have absolutely no idea what this is for (and the API didn't really explain it), and at no point in the code (attached link) does it seem to do anything.

Would someone be kind enough to explain it me or point me in the right direction towards something that will?

I apologize ahead of time if it needs to be move, and thanks ahead of time for all of those who help.

Nick

source code
[ February 03, 2006: Message edited by: Nicholas Carrier ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33703
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas,
What that's doing is loading the driver class into memory. That way when you try to get a connection from the driver, the JVM knows about it.
 
Nicholas Carrier
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aight (sic), I think get it.

Thanks.
 
stu derby
Ranch Hand
Posts: 333
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A proper JDBC driver always has a static initialization section, invoked when the class is loaded, that causes the driver to call DriverManager.registerDriver().

Then later, when DriverManager.getConnection() is called, it will iterate through all the loaded drivers, asking them (Driver).acceptsURL() ?, until it finds one that does, or exhausts the lists of drivers.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic