首页 文章

驱动程序在apache-tomcat-6.0.36 / lib中加载但仍然没有找到合适的驱动程序

提问于
浏览
0

我正在使用轴2和apache tomcat学习eclipse helios中的Web服务 . 我有两个带有java类的动态Web项目,当我在eclipse中将它们作为简单的java类运行时,它们成功连接到两个数据库 . (我已将外部jar添加到项目的外部构建路径中) . 但是当我在服务器上运行时,我收到一个错误: No suitable driver found . 我知道我需要load the necessary drivers进入 apache-tomcat-6.0.36/lib 并且我已经这样做了(并重新启动了服务器) . (见No suitable driver found) .

我使用此语句在我的Java类中创建驱动程序:

Class.forName("org.postgresql.Driver"); //throws class not found exception w/message "com.postgresql.jdbc.Driver"
conn = DriverManager.getConnection(url, user, password);

为什么apache“看不到”/ lib文件夹中的.jar驱动程序?一些较旧的教程说将.jars放在common / lib中 - 但我在apache tomcat目录结构中看不到该文件夹 . 我该怎么做来调试这个问题?

2 回答

  • 1

    根据您使用的Java版本(以及JDBC版本),您可能需要在调用 DriverManager.getConnection(...) 之前调用 Class.forName() . 这会强制JVM加载类,以便JDBC知道该类是您的连接类型的驱动程序 . 没有它,JDBC不知道你的数据库类型的驱动程序,因此吐出"No suitable driver found" .

    如果您使用Java 7(以及JDBC 4.0)在Eclipse中运行类,则会自动加载在类路径中找到的驱动程序 . 对于7之前的Java版本(以及4.0之前的JDBC),您必须手动注册您的驱动程序,如解释的那样 . 有关详细信息,请参阅JDBC tutorial .

  • 0

    如果在 ${CATALINA_HOME}/conf/server.xml 中取消注释此行:

    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
    

    将其更改为:

    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" driverManagerProtection="false" />
    

    这会将Tomcat返回到 pre-6.0.35 功能,它在 ${CATALINA_HOME}/lib 中正确注册了类型4 JDBC驱动程序 .

相关问题