aspose file tools
The moose likes JDBC and the fly likes Class.forname() return Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Class.forname() return" Watch "Class.forname() return" New topic
Author

Class.forname() return

Arvind Mahendra
Ranch Hand

Joined: Jul 14, 2007
Posts: 1162
Out of curiosity, The return type of Class.forName() is of type Class so when we load the driver where do we save this return type?


I want to be like marc
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
You don't - it is not needed when loading JDBC drivers.


Android appsImageJ pluginsJava web charts
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56185
    
  13

The only reason to do the Class.forName() is for the side effect of causing the class definition to be loaded. The actual Class instance, as Ulf pointed out, is not required at any point.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Arvind Mahendra
Ranch Hand

Joined: Jul 14, 2007
Posts: 1162
oh. Thanks
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8876

Just FYI. In Java 6 we would no longer be needing class.forName. Just put the vendor jar in classpath, the JDK will load the driver class.


Groovy
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Originally posted by Prad Dip:
Just FYI. In Java 6 we would no longer be needing class.forName. Just put the vendor jar in classpath, the JDK will load the driver class.

Can you explain how this should work, since it doesn't work for me.

Elaborating:
I connect to MSSQL 2005 using a correct connection string:
jdbc:sqlserver://<host>;DatabaseName=<database> (using the default port 1433)

When I use this in DriverManager.getConnection without loading the class it fails, stating it can't find the driver. If I do load the class manually, no problems.

The JAR file is in my <JRE home>\lib\ext folder, but it also doesn't work when specified on the classpath.


So, what's the magic I'm forgetting?
[ January 03, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8876

Does the jar file contain META-INF/services/java.sql.Driver file ? The file should specify Driver class there.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Ah right, that's the mechanism. I forgot.

Unfortunately, none of my SQL drivers have that, including the just newly downloaded HSQLDB driver. Shame.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8876

Originally posted by Rob Prime:
Ah right, that's the mechanism. I forgot.

Unfortunately, none of my SQL drivers have that, including the just newly downloaded HSQLDB driver. Shame.


Oracle JDBC driver jar have a oracle.jdbc.OracleDriver in the java.sql.Driver. Download file ojdbc6.jar. This is only for those who are using Oracle
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Class.forname() return
 
Similar Threads
Why use Class.forName() to load driver
Doubt regarding Class.forName() in JDBC connection
jdbc
Class.forName() doubt
What Class.forName() does?