java web app在netbeans中运行 - SQLException:没有合适的驱动程序

时间:2010-09-18 22:35:32

标签: java mysql maven-2 jdbc

我正在尝试运行在另一台完美运行的机器上开发的应用程序,所以它应该与我正在尝试运行它的机器上的配置有关。我正在使用netbeans 6.9,tomcat 6.0.26和maven。

当我尝试运行它时,它会给我以下错误:

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

我应该提到:

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>

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

2我在我当地的maven存储库中有jar。

3 Netbeans似乎现在推出了'嵌入式maven版本3.0',这是一个测试版。所以我将它配置为外部Maven Home:C:\ Program Files \ apache-maven-2.2.1。运行mvn --version会提供正确的版本,因此maven 2.2.1似乎已正确安装在我的系统上。但出于某种原因,每当我运行应用程序时,我也会得到这个:

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.

构建不会失败,但是上面的警告指向我的链接,让我进入我已经配置'外部Maven Home'的同一个窗口,如上所述。

4添加DriverManager.registerDriver(new com.mysql.jdbc.Driver()); (在stackoverflow上的另一篇文章中看到)在与数据库相关的任何内容之前没有帮助。

5相同的代码在另一台机器上运行。

我很感激为什么我得到jdbc驱动程序错误的任何想法。请记住,应用程序正在另一台机器上运行。

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>

其中database.url是     JDBC:MySQL的://127.0.0.1:3306 /测试autoReconnect的=真

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>

2 个答案:

答案 0 :(得分:1)

java.sql.SQLException: No suitable driver的常见原因是

  • 网址错误
  • 后面没有加载驱动程序。

事实上,以下一行清楚地表明存在错误。

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

JDBC连接字符串不能是http://maven.apache.org

检查您的构建, IS 错误(看起来发生了一些意外的过滤)。

但是你没有给出更详细的答案。

答案 1 :(得分:0)

这看起来就像你告诉Hibernate使用一个提供DataSource的容器,后者又无法找到JDBC驱动程序。

如果是这样,问题是容器类加载器加载的代码无法看到WAR中提供的代码。

您必须将驱动程序jar添加到容器的扩展文件夹中。