Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Class.forName(driver) silently fails

843841Nov 18 2003 — edited Nov 21 2003
I have encountered a strange problem with Class.forName and I'd appreciate any advice or hints as to what's going wrong.

I have a servlet that, among other things, gets an image stored in a database. The servlet works fine on most machines but on one machine the Class.forName fails; it doesn't seem to load the driver, and while it doesn't throw any errors when the program continues on to the DriverManager.getConnection it crashes with a null pointer exception.

The strange thing is that the same code works fine when placed in a standalone .java program. It only fails here, in this servlet, and this servlet does work on other machines - just not this one, and I don't konw why. The driver exists and can be loaded (using the same code!) in other routines, and it even works in this routine on other machines. The program compiles with no problems. The login settings and driver name passed to the routine are correct.

The routine I am having trouble with is this:

public boolean getNextImage()
{
boolean rc = false;
Vector tmpVec = null;

String companyid = "";
String key1 = "";
String statecode = "";
String juris = "";
String occur = "";
String imgfile = "";

files = explode(9, input);
curElement = 0;

if(files!=null && curElement<files.size())
{
tmpVec = explode('*', (String)files.elementAt(curElement));
curElement++;
companyid = (String)tmpVec.elementAt(0);
key1 = (String)tmpVec.elementAt(1);
statecode = (String)tmpVec.elementAt(2);
juris = (String)tmpVec.elementAt(3);
occur = (String)tmpVec.elementAt(4);

Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
FileOutputStream sos = null;

try
{
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url, user, pass);
}
catch (Exception cnfe)
{
System.out.println("Error: " + cnfe);
}

ps = con.prepareStatement("SELECT Image FROM " + table + " WHERE CompanyId=? AND Key1=? AND StateCode=? AND Jurisdiction=? AND KeyOccurrenceNumber=?");
ps.setString(1, companyid);
ps.setString(2, key1);
ps.setString(3, statecode);
ps.setString(4, juris);
ps.setString(5, occur);
rs = ps.executeQuery();
byte[] bytes;

sos = new FileOutputStream("c:\\eTaxScan\\bp.jpg");
if (rs != null && rs.next())
{
System.out.println("got record back");
bytes = rs.getBytes(1);
sos.write(bytes);
}
rc = true;
}
catch(Exception e)
{
System.out.println("Exception e = "+e);
}
finally
{
try
{
rs.close();
ps.close();
con.close();
sos.close();
}
catch(Exception e2)
{
System.out.println("Exception e2="+e2);
e2.printStackTrace();
}
}
}

The url, user, and pass variables (as well as a few others) are defined elsewhere and do hold correct values.

The truly weird thing is that when it crashes, it does NOT trigger the try/catch where the DriverManager is, nor does it trigger the other one; it instead triggers the one in the finally block. I have tested this by inserting print statements and the code gets right to the con= line and then throws the NullPointerException error, using the finally block.

My question is this: why is DriverManager failing, and why is it skipping two try/catch blocks to hit the one in the finally? (The routine throws no other errors.)

For the record, I am running Windows XP home and use MSDE (MS SQL Server 7) as my data environment. I use net.sourceforge.jtds.jdbc.Driver as a driver. My connection string, username, pwd, and driver works fine in standalone programs, JSP programs, and other servlets on this same machine.

I would appreciate any help! I am at a complete loss as to what is going on.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 19 2003
Added on Nov 18 2003
17 comments
1,126 views