You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Nicholas Albion <ni...@vecommerce.com.au> on 2009/02/09 01:10:53 UTC
NullPointerException from DriverManager.getConnection() -> JdbcOdbcDriver.initialize()
The application I'm working on has been working on my development
machine and in production for over a year, but all of a sudden, I'm
getting a NullPointerException from the JDBC-ODBC bridge, but only when
I'm running the same code in Tomcat:
java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.initialize(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
When I run the code below in JUnit, it works perfectly and no matter
what I try, I can't reproduce the bug in JUnit
Could not find database driver class: bad.driver.name
Failed to access DB at :no.protocol. ErrMsg: No suitable driver
Failed to access DB at jdbc:odbc:BadUrl. ErrMsg: [Microsoft][ODBC Driver
Manager] Data source name not found and no default driver specified
Failed to access DB at http://google.com <http://google.com> . ErrMsg:
No suitable driver
!!!!!!!!!! IT WORKED !!!!!!!!!!
Failed to access DB at null. ErrMsg: The url cannot be null
Could not load database driver class: null
!!!!!!!!!! IT WORKED !!!!!!!!!!
!!!!!!!!!! IT WORKED !!!!!!!!!!
Could not load database driver class: null
Failed to access DB at . ErrMsg: No suitable driver
Could not find database driver class:
!!!!!!!!!! IT WORKED !!!!!!!!!!
!!!!!!!!!! IT WORKED !!!!!!!!!!
This one is known work in production (but lately not from Tomcat on my
PC)...
!!!!!!!!!! IT WORKED !!!!!!!!!!
from DB: testdata
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
import static org.junit.Assert.*;
public class DALTest {
private static final String DRIVER_NAME =
"sun.jdbc.odbc.JdbcOdbcDriver";
private static final String DB_URL = "jdbc:odbc:MyTestDB";
public DALTest() {}
@Test
public void testParams() {
checkDB( createConnection( "bad.driver.name", DB_URL,
"", ""), false );
checkDB( createConnection( DRIVER_NAME, " :no.protocol",
"", ""), false );
checkDB( createConnection( DRIVER_NAME,
"jdbc:odbc:BadUrl", "", ""), false );
checkDB( createConnection( DRIVER_NAME,
"http://google.com <http://google.com> <http://google.com
<http://google.com> > ", "", null), false );
checkDB( createConnection( DRIVER_NAME, DB_URL, "",
null), false ); // blank password allowed
checkDB( createConnection( DRIVER_NAME, null, "", null),
false );
checkDB( createConnection( null, DB_URL, "", null),
false );
checkDB( createConnection( DRIVER_NAME, DB_URL, null,
null), false ); // blank password allowed
checkDB( createConnection( DRIVER_NAME, DB_URL, null,
null), false ); // blank password allowed
checkDB( createConnection( null, null, null, null),
false );
checkDB( createConnection( DRIVER_NAME, "", "", ""),
false );
checkDB( createConnection( "", DB_URL, "", null), false
);
checkDB( createConnection( DRIVER_NAME, DB_URL, "",
null), false ); // blank password allowed
checkDB( createConnection( DRIVER_NAME, DB_URL, null,
""), false ); // blank password allowed
}
@Test
public void testCreateConnection() {
System.out.println("This one is known work in production
(but lately not from Tomcat on my PC)...");
Connection con = createConnection( DRIVER_NAME, DB_URL,
"", "");
checkDB( con, true );
}
private void checkDB( Connection con, boolean printSuccessOnly )
{
if( con == null ) {
return;
}
Statement stmt;
try {
stmt = con.createStatement();
ResultSet result = stmt.executeQuery("SELECT *
FROM my_test_table");
if( printSuccessOnly ) {
int limit = 1;
while( result.next() && limit-- != 0 )
System.out.println( " from DB: " + result.getString(1) );
} else {
assertTrue( "Failed to read any records
from the reroute table", result.next() );
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
protected Connection createConnection(String driver, String url,
String username, String password)
{
// System.out.println("driver:'"+driver+"' url:'"+url+"'
username:'"+username+"' password:'"+password+"'");
Class clazz;
try {
clazz = Class.forName(driver);
// System.out.println( "Loaded class: " + driver);
// clazz.newInstance();
// System.out.println( "Created an instance of " +
driver + " (not sure if we actually do anything with it)");
} catch( ClassNotFoundException e ) {
System.out.println("Could not find database
driver class: " + driver);
return null;
// throw new RuntimeException(e);
} catch( ExceptionInInitializerError e ) {
System.out.println("Error initialising driver
class: " + driver);
return null;
// throw e;
} catch( LinkageError e ) {
System.out.println("Linkage error loading
database driver class: " + driver);
return null;
// throw e;
} catch( Throwable e ) {
System.out.println("Could not load database
driver class: " + driver);
return null;
// e.printStackTrace();
// throw new RuntimeException(e);
}
Connection con;
try {
if( username == null ) { username = ""; }
if( password == null ) { password = ""; }
con = DriverManager.getConnection(url, username,
password);
System.out.println(" !!!!!!!!!! IT WORKED
!!!!!!!!!!");
return con;
} catch (SQLException e) {
System.out.println("Failed to access DB at " +
url + ". ErrMsg: " +e.getMessage());
return null;
// throw new RuntimeException(e);
} catch (Throwable e) {
System.out.println("Failed to create DB
Connection. (" + e.getClass().getName() + ") ErrMsg: " +e.getMessage() +
", cause: " + e.getCause());
return null;
// throw new RuntimeException(e);
}
}
}
RE: NullPointerException from DriverManager.getConnection() -> JdbcOdbcDriver.initialize()
Posted by Nicholas Albion <ni...@vecommerce.com.au>.
Tomcat: 5.5.25
Java: 1.6.0_02-b06
It works when I run Tomcat on JRE 1.5.
The class is loaded in Java 1.6, but the NPE is thrown within the
JdbcOdbcDriver code:
> java.lang.NullPointerException
> at sun.jdbc.odbc.JdbcOdbcDriver.initialize(Unknown Source) at
> sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source) at
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
RE: NullPointerException from DriverManager.getConnection()
->JdbcOdbcDriver.initialize()
Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Martin Gainty [mailto:mgainty@hotmail.com]
> Subject: RE: NullPointerException from
> DriverManager.getConnection() ->JdbcOdbcDriver.initialize()
>
> that message does'nt make sense
Why not? It's a NullPointerException, not a ClassNotFoundException.
> please verify rt.jar is on your CLASSPATH
That is utterly stupid. NEVER, NEVER, NEVER put rt.jar in CLASSPATH, under any circumstances. Classes from rt.jar must be loaded only by the JVM's internal bootstrap classloader; classes found via CLASSPATH are handled by the system classloader. The JVM knows where rt.jar is, although that location can be overridden by -Xbootclasspath when needed. Regardless, you don't need to set -Xbootclasspath or CLASSPATH when running Tomcat.
- Chuck
THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
RE: NullPointerException from DriverManager.getConnection() ->
JdbcOdbcDriver.initialize()
Posted by Martin Gainty <mg...@hotmail.com>.
that message does'nt make sense
%JAVA_HOME%\jre\lib\rt.jar
contains the sun.jdbc.odbc.JdbcOdbcDriver class
please verify rt.jar is on your CLASSPATH and display which version JAVA and TC you are using..
Martin
______________________________________________
Disclaimer and confidentiality note
Everything in this e-mail and any attachments relates to the official business of Sender. This transmission is of a confidential nature and Sender does not endorse distribution to any party other than intended recipient. Sender does not necessarily endorse content contained within this transmission.
> Subject: NullPointerException from DriverManager.getConnection() -> JdbcOdbcDriver.initialize()
> Date: Mon, 9 Feb 2009 11:10:53 +1100
> From: nicholas.albion@vecommerce.com.au
> To: users@tomcat.apache.org
>
> The application I'm working on has been working on my development
> machine and in production for over a year, but all of a sudden, I'm
> getting a NullPointerException from the JDBC-ODBC bridge, but only when
> I'm running the same code in Tomcat:
>
> java.lang.NullPointerException
> at sun.jdbc.odbc.JdbcOdbcDriver.initialize(Unknown Source)
> at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
> at java.sql.DriverManager.getConnection(Unknown Source)
> at java.sql.DriverManager.getConnection(Unknown Source)
>
> When I run the code below in JUnit, it works perfectly and no matter
> what I try, I can't reproduce the bug in JUnit
>
>
> Could not find database driver class: bad.driver.name
> Failed to access DB at :no.protocol. ErrMsg: No suitable driver
> Failed to access DB at jdbc:odbc:BadUrl. ErrMsg: [Microsoft][ODBC Driver
> Manager] Data source name not found and no default driver specified
> Failed to access DB at http://google.com <http://google.com> . ErrMsg:
> No suitable driver
> !!!!!!!!!! IT WORKED !!!!!!!!!!
> Failed to access DB at null. ErrMsg: The url cannot be null
> Could not load database driver class: null
> !!!!!!!!!! IT WORKED !!!!!!!!!!
> !!!!!!!!!! IT WORKED !!!!!!!!!!
> Could not load database driver class: null
> Failed to access DB at . ErrMsg: No suitable driver
> Could not find database driver class:
> !!!!!!!!!! IT WORKED !!!!!!!!!!
> !!!!!!!!!! IT WORKED !!!!!!!!!!
> This one is known work in production (but lately not from Tomcat on my
> PC)...
> !!!!!!!!!! IT WORKED !!!!!!!!!!
> from DB: testdata
>
>
>
> import java.sql.Connection;
> import java.sql.DriverManager;
> import java.sql.ResultSet;
> import java.sql.SQLException;
> import java.sql.Statement;
>
> import org.junit.Test;
> import static org.junit.Assert.*;
>
> public class DALTest {
> private static final String DRIVER_NAME =
> "sun.jdbc.odbc.JdbcOdbcDriver";
> private static final String DB_URL = "jdbc:odbc:MyTestDB";
>
> public DALTest() {}
>
> @Test
> public void testParams() {
> checkDB( createConnection( "bad.driver.name", DB_URL,
> "", ""), false );
> checkDB( createConnection( DRIVER_NAME, " :no.protocol",
> "", ""), false );
> checkDB( createConnection( DRIVER_NAME,
> "jdbc:odbc:BadUrl", "", ""), false );
> checkDB( createConnection( DRIVER_NAME,
> "http://google.com <http://google.com> <http://google.com
> <http://google.com> > ", "", null), false );
>
> checkDB( createConnection( DRIVER_NAME, DB_URL, "",
> null), false ); // blank password allowed
>
> checkDB( createConnection( DRIVER_NAME, null, "", null),
> false );
> checkDB( createConnection( null, DB_URL, "", null),
> false );
>
> checkDB( createConnection( DRIVER_NAME, DB_URL, null,
> null), false ); // blank password allowed
> checkDB( createConnection( DRIVER_NAME, DB_URL, null,
> null), false ); // blank password allowed
>
> checkDB( createConnection( null, null, null, null),
> false );
> checkDB( createConnection( DRIVER_NAME, "", "", ""),
> false );
> checkDB( createConnection( "", DB_URL, "", null), false
> );
>
> checkDB( createConnection( DRIVER_NAME, DB_URL, "",
> null), false ); // blank password allowed
> checkDB( createConnection( DRIVER_NAME, DB_URL, null,
> ""), false ); // blank password allowed
> }
>
> @Test
> public void testCreateConnection() {
> System.out.println("This one is known work in production
> (but lately not from Tomcat on my PC)...");
> Connection con = createConnection( DRIVER_NAME, DB_URL,
> "", "");
> checkDB( con, true );
> }
>
> private void checkDB( Connection con, boolean printSuccessOnly )
> {
> if( con == null ) {
> return;
> }
> Statement stmt;
> try {
> stmt = con.createStatement();
> ResultSet result = stmt.executeQuery("SELECT *
> FROM my_test_table");
> if( printSuccessOnly ) {
> int limit = 1;
> while( result.next() && limit-- != 0 )
> System.out.println( " from DB: " + result.getString(1) );
> } else {
> assertTrue( "Failed to read any records
> from the reroute table", result.next() );
> }
> } catch (SQLException e) {
> e.printStackTrace();
> }
> }
>
> @SuppressWarnings("unchecked")
> protected Connection createConnection(String driver, String url,
> String username, String password)
> {
> // System.out.println("driver:'"+driver+"' url:'"+url+"'
> username:'"+username+"' password:'"+password+"'");
>
> Class clazz;
> try {
> clazz = Class.forName(driver);
> // System.out.println( "Loaded class: " + driver);
> // clazz.newInstance();
> // System.out.println( "Created an instance of " +
> driver + " (not sure if we actually do anything with it)");
> } catch( ClassNotFoundException e ) {
> System.out.println("Could not find database
> driver class: " + driver);
> return null;
> // throw new RuntimeException(e);
> } catch( ExceptionInInitializerError e ) {
> System.out.println("Error initialising driver
> class: " + driver);
> return null;
> // throw e;
> } catch( LinkageError e ) {
> System.out.println("Linkage error loading
> database driver class: " + driver);
> return null;
> // throw e;
> } catch( Throwable e ) {
> System.out.println("Could not load database
> driver class: " + driver);
> return null;
> // e.printStackTrace();
> // throw new RuntimeException(e);
> }
>
> Connection con;
> try {
> if( username == null ) { username = ""; }
> if( password == null ) { password = ""; }
> con = DriverManager.getConnection(url, username,
> password);
> System.out.println(" !!!!!!!!!! IT WORKED
> !!!!!!!!!!");
> return con;
> } catch (SQLException e) {
> System.out.println("Failed to access DB at " +
> url + ". ErrMsg: " +e.getMessage());
> return null;
> // throw new RuntimeException(e);
> } catch (Throwable e) {
> System.out.println("Failed to create DB
> Connection. (" + e.getClass().getName() + ") ErrMsg: " +e.getMessage() +
> ", cause: " + e.getCause());
> return null;
> // throw new RuntimeException(e);
> }
> }
> }
>
>
>
_________________________________________________________________
Windows Liveā¢: E-mail. Chat. Share. Get more ways to connect.
http://windowslive.com/explore?ocid=TXT_TAGLM_WL_t2_allup_explore_022009
RE: NullPointerException from DriverManager.getConnection() ->
JdbcOdbcDriver.initialize()
Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Nicholas Albion [mailto:nicholas.albion@vecommerce.com.au]
> Subject: RE: NullPointerException from
> DriverManager.getConnection() -> JdbcOdbcDriver.initialize()
>
> all of a sudden, I'm getting a NullPointerException from
> the JDBC-ODBC bridge
The JDBC-ODBC bridge has never been anywhere close to production quality, and really shouldn't be used at all. You need to use a real database with a real JDBC driver.
- Chuck
THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
RE: NullPointerException from DriverManager.getConnection() -> JdbcOdbcDriver.initialize()
Posted by Nicholas Albion <ni...@vecommerce.com.au>.
For whatever reason, I've had to down-grade from jdk1.6.0_06 to
jdk1.5.0_14/jre and it seems to be working
> all of a sudden, I'm getting a NullPointerException from the JDBC-ODBC
bridge, but only when I'm running the same code in Tomcat:
>
> java.lang.NullPointerException
> at sun.jdbc.odbc.JdbcOdbcDriver.initialize(Unknown Source)
> at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
> at java.sql.DriverManager.getConnection(Unknown Source)
> at java.sql.DriverManager.getConnection(Unknown Source)
> When I run the code below in JUnit, it works perfectly and no matter
what I try, I can't reproduce the bug in JUnit
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org