Tomcat 错误:java.sql.SQLException:找不到适合 jdbc:sqlserver:// 的驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11377018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:49:43  来源:igfitidea点击:

Tomcat error: java.sql.SQLException: No suitable driver found for jdbc:sqlserver://

javatomcatapache-commons-dbcpjdbc-pool

提问by Arturas M

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...

我的 Tomcat 有一个可怕的问题,非常糟糕,因为这个问题我已经放弃了这个项目一个多月了......但我仍然需要解决它并继续这个项目......

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

java.sql.SQLException:找不到适合 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...

问题是相同的应用程序在桌面版本 Perfectlz 中运行,但是当涉及到应该在服务器上运行的版本(NetBeans 7.1.2 中的 Tomcat 7.0.22.0)时,它只会抛出错误。它似乎没有加载池驱动程序,或者我什至不知道......

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. :)

我对 Tomcat 不熟悉,直到现在,Netbeans 对 tomcat 的处理都很好......问题是我讨厌这样的错误......如果你在三天内不解决它,你会很沮丧,不要想回到那个,你感觉就像撞墙......现在我尝试在谷歌上搜索很多,但仍然没有太大帮助......所以如果有人能帮助我我会很高兴这。谢谢。:)

回答by M. Abbas

You have to copy the JDBC Driver's jar into $CATALINA_HOME/lib.

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

回答by Bahman_Aries

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).

JDBC-driver JAR file我的ojdbc6)复制到$JAVA_HOME/jre/lib/ext对我来说是C:\Program Files\Java\jdk1.7.0_80\jre\lib\ext)。

Details:

细节:

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

根据Apache Tomcat 7 文档当 Tomcat 启动时,它会创建一组类加载器,这些类加载器被组织成以下父子关系:

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

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

每个类加载器都会在某个目录中搜索 JAR 文件。它们使可见的类和资源解释如下:

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.

Bootstrap:这个类加载器包含 JVM 提供的基本运行时类,以及系统扩展目录中存在的 JAR 文件中的任何类$JAVA_HOME/jre/lib/ext

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

System:这个类加载器通常从CLASSPATH环境变量的内容中初始化。所有这些类对 Tomcat 内部类和 Web 应用程序都是可见的。但是,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/liband $CATALINA_Home/libfor jar files.

Common:这个类加载器包含对 Tomcat 内部类和所有 Web 应用程序都可见的附加类。这个类加载器的外观(默认)在$CATALINA_BASE/lib$CATALINA_Home/libJAR文件。

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/classesdirectory of your web application, plus classes and resources in JAR files under the /WEB-INF/libdirectory of your web application, are made visible to this web application, but not to other ones.

Webapps:为部署在单个 Tomcat 实例中的每个 Web 应用程序创建一个类加载器。/WEB-INF/classesWeb 应用程序目录中的所有解压缩类和资源,以及 Web 应用程序/WEB-INF/lib目录下 JAR 文件中的类和资源,都对本 Web 应用程序可见,但对其他 Web 应用程序不可见。

  • In conclusion, buy putting the ojdbcJAR file inside $JAVA_HOME/jre/lib/extthe JDBC driver, will get visible at the Bootstrap level.
  • 总之,购买将ojdbcJAR 文件放在$JAVA_HOME/jre/lib/extJDBC 驱动程序中,将在 Bootstrap 级别可见。