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);
                }
        }
}



Reply via email to