4

I have a terrible problem with Tomcat, well terrible because of this problem I've thrown away the project for more than a month now... Yet I'll still need to solve it and to go on with the project...

So it's throwing me this error:

java.sql.SQLException: No suitable driver found for jdbc:sqlserver://isd.ktu.lt:1433;DatabaseName=LN2012_bakDB2 java.lang.NullPointerException

The thing is that the same application is working in the desktop version perfectlz, however when it comes to the version which is supposed to be running on the server (Tomcat 7.0.22.0 inside NetBeans 7.1.2) it just throws the Error. It seems it doesn't load the pooling driver or I don't even know...

Well here's the part responsible for that:

public DatabaseConnection(Parameters params) {                

    // parameters and the output                
    this.gui = params.getGui();

    // activate database pool
    connectionPool = new GenericObjectPool(null);
    connectionFactory = new DriverManagerConnectionFactory(params.getDbAdr(), params.getDbUser(), params.getDbPass());
    poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
    driver = new PoolingDriver();
    driver.registerPool("GenTreeDatabase", connectionPool);
    //driver.registerPool("jdbc:apache:commons:dbcp:GenTreeDatabase", connectionPool);        

}

public void openConn() {
    if (allowOutput) gui.print("Getting connection to database");
    try {
        con = DriverManager.getConnection("jdbc:apache:commons:dbcp:GenTreeDatabase");
        if (con != null) {
            if (allowOutput) gui.print("Connection to database was successful");
        }
    } catch (SQLException ex) {
        gui.err(specificError + "Error getting connection to database - " + ex);
    }
}

It happens at the point where it tries to get the connection, then it gets a null pointer exception, since the connection is not retrieved successfuly.

I'm not familiar with Tomcat and up until this moment, Netbeans handled tomcat fine... The thing is I hate errors like this... If you don't solve it in three days, you get so frustrated and don't wanna get back to that, you feel like hitting a wall... Now I tried googling a lot about it, but still it wasn't much of a help... So I'd be really glad if somebody could help me with this. Thanks. :)

4

4 回答 4

6

您必须将 JDBC 驱动程序的 jar 复制到 $CATALINA_HOME/lib 中。

于 2012-07-09T10:20:19.613 回答
1

This is probably too late to answer this question but in order to help with similar issues, Here's how I got around it.

Quick Solution:

Copy the JDBC-driver JAR file (mine was ojdbc6) into $JAVA_HOME/jre/lib/ext (for me it was C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext).

Details:

According to Apache Tomcat 7 documentations When Tomcat is started, it creates a set of class-loaders that are organized into the following parent-child relationships:

     Bootstrap
         |
      System
         |
      Common
      /     \
 Webapp1    Webapp2 ...

Each class loader, searches for JAR files inside a certain directory. The classes and resources that they make visible are explained below:

Bootstrap : This class loader contains the basic runtime classes provided by the JVM, plus any classes from JAR files present in the System Extensions directory $JAVA_HOME/jre/lib/ext.

System : This class loader is normally initialized from the contents of the CLASSPATH environment variable. All such classes are visible to both Tomcat internal classes, and to web applications. However, There are some exceptions for Tomcat.

Common : This class loader contains additional classes that are made visible to both Tomcat internal classes and to all web applications. This class loader looks (by default) in $CATALINA_BASE/lib and $CATALINA_Home/lib for jar files.

Webapps : A class loader is created for each web application that is deployed in a single Tomcat instance. All unpacked classes and resources in the /WEB-INF/classes directory of your web application, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application, are made visible to this web application, but not to other ones.

  • In conclusion, buy putting the ojdbc JAR file inside $JAVA_HOME/jre/lib/ext the JDBC driver, will get visible at the Bootstrap level.
于 2017-08-14T08:03:45.527 回答
0

它帮助我将 mssql-jdbc-9.2.1.jre8.jar 文件复制到 C:\Java\jdk1.8.0_291\jre\lib\ext 文件夹

于 2021-08-12T03:54:57.240 回答
0

我也遇到了同样的问题。这是因为你的 tomcat 服务器没有jar你的JDBC. 我通过将文件复制到tomcat服务器中的JAR文件夹来解决问题。lib

于 2020-09-05T10:55:09.350 回答