简体   繁体   English

在netbeans中运行的Java Web应用程序-SQLException:没有合适的驱动程序

[英]java web app run in netbeans - SQLException: No suitable driver

I am trying to run an application developed on another machine where it was perfectly running, so it should have something to do with the configurations on the machine I am trying to run it on now. 我正在尝试运行在另一台运行良好的机器上开发的应用程序,因此它应该与我现在尝试在其上运行的机器上的配置有关。 I'm using netbeans 6.9, tomcat 6.0.26 and maven. 我正在使用netbeans 6.9,tomcat 6.0.26和maven。

When I try to run it, it gives me the following error: 当我尝试运行它时,它给了我以下错误:

Sep 19, 2010 12:51:02 AM org.hibernate.cfg.SettingsFactory buildSettings
WARNING: Could not obtain connection metadata
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'http://maven.apache.org'
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1150)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
    at org.hibernate.connection.DatasourceConnectionProvider.getConnection(DatasourceConnectionProvider.java:69)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:84)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2073)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1298)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
    at AppConfiguration.loadConfig(AppConfiguration.java:164)
    at Listener.contextInitialized(Listener.java:40)
    at Org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3972)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4467)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:637)
    at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:563)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:498)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1277)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:321)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
    at org.apache.catalina.core.StandardService.start(StandardService.java:519)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getDriver(DriverManager.java:264)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1143)
    ... 32 more

I should mention that: 我应该提到:

1 I have mysql-connector-java-5.1.13-bin.jar in Apache Tomcat 6.0.26\\lib and in my pom.xml I have: 1我在Apache Tomcat 6.0.26 \\ lib中有mysql-connector-java-5.1.13-bin.jar,在pom.xml中有:

      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <url>http://maven.apache.org</url>

and

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.13</version>
    <scope>provided</scope>
</dependency>

2 I have the jar in my local maven repository. 2我的本地Maven存储库中有jar。

3 Netbeans seems to come now with 'embedded maven version 3.0', which is a beta version. 3 Netbeans现在似乎带有beta版的“嵌入式Maven版本3.0”。 So I've configured it to use as External Maven Home: C:\\Program Files\\apache-maven-2.2.1. 因此,我已将其配置为用作外部Maven主页:C:\\ Program Files \\ apache-maven-2.2.1。 Running mvn --version gives the correct version, so maven 2.2.1 seems to be properly installed on my system. 运行mvn --version会给出正确的版本,因此maven 2.2.1似乎已正确安装在我的系统上。 But for some reason, whenever I run the app, I also get this: 但是由于某种原因,每当我运行该应用程序时,我也会得到以下信息:

WARNING: You are running embedded Maven builds, some build may fail due to     incompatibilities with latest Maven release. To set Maven instance to use for building, click here.

The build does not fail, but the link that the above warning points me to, gets me to the same window where I've already configured 'External Maven Home', as mentioned above. 构建不会失败,但是上面警告所指向的链接将我带到已配置“外部Maven主页”的同一窗口,如上所述。

4 Adding DriverManager.registerDriver(new com.mysql.jdbc.Driver()); 4添加DriverManager.registerDriver(new com.mysql.jdbc.Driver()); (as seen in another post here on stackoverflow) before anything related to the database did not help. (如stackoverflow上的另一篇文章所述)之前,与数据库相关的任何操作均无济于事。

5 The same code was running on another machine. 5同一代码在另一台计算机上运行。

I would appreciate any ideas as to why I am getting the jdbc driver error. 对于我为什么收到jdbc驱动程序错误的任何想法,我将不胜感激。 And please keep in mind that the application was running on another machine. 并且请记住,该应用程序正在另一台计算机上运行。

context.xml: context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/app/test">
  <Resource auth="Container" defaultAutoCommit="false" description="dbcp" driverClassName="com.mysql.jdbc.Driver" initialSize="1" maxActive="10" maxIdle="5" maxWait="1000" minIdle="10" name="jdbc/Test" password="${database.password}" poolPreparedStatements="true" type="javax.sql.DataSource" url="${database.url}" username="${database.username}"/>
  <Realm className="org.apache.catalina.realm.DataSourceRealm" dataSourceName="jdbc/Test" localDataSource="true" roleNameCol="role" userCredCol="pass" userNameCol="user" userRoleTable="roles" userTable="users"/>
  <Valve className="org.apache.catalina.valves.AccessLogValve" fileDateFormat="yyyy-MM-dd" pattern="combined" prefix="test-access" resolveHosts="false" rotatable="true" suffix=".log"/>
</Context>

where database.url is jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true 其中database.url是jdbc:mysql://127.0.0.1:3306 / test?autoReconnect = true

hibernate.cfg.xml: hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.datasource">java:/comp/env/jdbc/Test</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="cache.use_query_cache">false</property>
        <property name="cache.use_minimal_puts">false</property>
        <property name="max_fetch_depth">3</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="generate_statistics">true</property>
        <property name="hbm2ddl.auto">validate</property>
        <property name="current_session_context_class">thread</property>
        <mapping class="Test.Users"/>
        <mapping class="Test.Roles"/>
        <!-- entity bean defs -->
    </session-factory>
</hibernate-configuration>

Common causes for java.sql.SQLException: No suitable driver are java.sql.SQLException: No suitable driver常见原因java.sql.SQLException: No suitable driver

  • the URL is wrong 网址错误
  • followed by the driver isn't loaded. 其次是未加载驱动程序。

And indeed, the following line clearly indicates something wrong. 实际上,以下几行清楚地表明出了问题。

Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'http://maven.apache.org'

The JDBC connection string can't be http://maven.apache.org . JDBC连接字符串不能为http://maven.apache.org

Check your build, something IS wrong (it looks like some unexpected filtering is occurring). 检查你的身材,什么错的(它看起来像一些意想不到的滤波发生)。

But you're not giving enough details for a more detailed answer. 但是您没有提供足够的详细信息来获得更详细的答案。

This looks like you have told Hibernate to use a container provided DataSource, which in turn cannot locate the JDBC-driver. 看起来您已经告诉Hibernate使用容器提供的DataSource,而该容器又无法找到JDBC驱动程序。

If so, the problem is that code loaded by the container class loader cannot see code provided in your WAR . 如果是这样,则问题是由容器类加载器加载的代码无法看到WAR中提供的代码。

You must add the driver jar to the extension folder of the container. 您必须将驱动程序jar添加到容器的扩展文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM