Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class.forname() return

 
Arvind Mahendra
Ranch Hand
Posts: 1162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't - it is not needed when loading JDBC drivers.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arvind Mahendra
Ranch Hand
Posts: 1162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh. Thanks
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20744
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the jar file contain META-INF/services/java.sql.Driver file ? The file should specify Driver class there.
 
Rob Spoor
Sheriff
Pie
Posts: 20744
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic