wood burning stoves*
The moose likes JDBC and the fly likes Class.forName() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Class.forName()" Watch "Class.forName()" New topic
Author

Class.forName()

Paul O'Neill
Ranch Hand

Joined: Mar 13, 2007
Posts: 40
I have a DAO class getting a connection from another DAO class and it ultimately fails. The line it fails on is where it looks up Class.forName(com.microsoft.jdbc.sqlserver.SQLServerDriver). I've only figured this out by putting System.outs everywhere. There's absolutely nothing that tells me why it fails at that line. It works when I run it on my dekstop using WSAD and fails out on a server, which indicates it's a classpath issue - I think. The mssqlserver.jar file is part of the classpath.

Thanks for any help out there.
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi Paul,

do you have a try/catch around the Class.forName(...) line?
It might throw a ClassNotFoundException.

Herman
Paul O'Neill
Ranch Hand

Joined: Mar 13, 2007
Posts: 40
I've tried that and it returns nothing. It's like it doesn't error but it never gets to the line after Class.forName either. I'm calling this method from another class.

Here's the code :

if (mConnection == null) {
try {
Class.forName(jdbcDriverName);
log.info("Executing getConnection() to get a new connection to " + jdbcURL);
mConnection = DriverManager.getConnection(jdbcURL,jdbcUserName,jdbcPassword);
mConnection.setAutoCommit(false);
log.info("Got Connection()");
}
catch (Exception e) {
e.printStackTrace();
log.warn("Error getting connection. Attempting retry.",e);
retryConnection();
}
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi again,

A couple of questions:

-how do you know that your code fails at all, if it doesn't log any errors?
-is mConnection equal to null in this case?
-does the logging itself work: can you show me the log messages that you do get? (just copy/paste, don't type).
-maybe you should try to catch Throwable, so that you will also catch instances of java.lang.Error
-what do you do in retryConnection()?

Herman
Paul O'Neill
Ranch Hand

Joined: Mar 13, 2007
Posts: 40
Herman. I owe you!!

I never thought of using Throwable as more of a catch-all than Exception. This showed up I was missing a couple of classes because I didn't have the msbase.jar or msutil.jar in my classpath. I had been try-catching with Exception and it just didn't tell me anything.

Many thanks!!

Paul
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi Paul,

you don't owe me anything, but thanks anyway.

Herman
 
 
subject: Class.forName()
 
Similar Threads
error using class.forName()
JDBC, mysql connector/j, face a problem to connect mysql 5.1 and JSE.
Instance already exist exception
setting classpath dynamically
retrieving records from the database