无法获得 JDBC 连接;嵌套异常是 java.sql.SQLException:无法加载 JDBC 驱动程序类 'org.hsql.jdbcDriver'

Posted

技术标签:

【中文标题】无法获得 JDBC 连接;嵌套异常是 java.sql.SQLException:无法加载 JDBC 驱动程序类 \'org.hsql.jdbcDriver\'【英文标题】:Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot load JDBC driver class 'org.hsql.jdbcDriver'无法获得 JDBC 连接;嵌套异常是 java.sql.SQLException:无法加载 JDBC 驱动程序类 'org.hsql.jdbcDriver' 【发布时间】:2015-07-22 10:08:07 【问题描述】:

我不确定为什么我无法连接到我的数据库。我正在使用 JNDI 和 maven 来帮助处理依赖项。在 Eclipse 中我的服务器文件夹中的 context.xml 看起来像:

Could not get JDBC Connection; nested exception is java.sql.SQLException:  
Cannot load JDBC driver class 'org.hsql.jdbcDriver'

context.xml

<Context>

    <!-- Default set of monitored resources. If one of these changes, the -->
    <!-- web application will be reloaded. -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>$catalina.base/conf/web.xml</WatchedResource>

    <Resource name="jdbc/Spring_Tutorial" auth="Container"
        type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000"
        username="root" driverClassName="org.hsql.jdbcDriver"
        url="jdbc:mysql://localhost:3306/Spring_Tutorial" />

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!-- <Manager pathname="" /> -->

    <!-- Uncomment this to enable Comet connection tacking (provides events 
        on session expiration as well as webapp lifecycle) -->
    <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" 
        /> -->
</Context>

如果无法连接,该类会处理错误:

@ControllerAdvice
public class DatabaseErrorHandler 

    @ExceptionHandler(DataAccessException.class)
    public String handleDatabaseException(DataAccessException ex) 

        System.out.println("Error connecting to Database: "+ex);

        return "error";
    


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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.spring.practice</groupId>
  <artifactId>offers</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.35</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate</artifactId>
        <version>3.5.4-Final</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.1.3.Final</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>1.8.0.10</version>
    </dependency>
  </dependencies>
</project>

web.xml

<description>Database</description>
  <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/Spring_Tutorial</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

我添加了 hsqldb jar 文件,但我真的认为我不需要它

    <dependency>
        <groupId>hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>1.8.0.10</version>
    </dependency>

据我了解,我只需要 mysql-connector-java 依赖项

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.35</version>
</dependency>

我的数据库名称是 Spring_Tutorial,我使用的是 mySQL Workbench。

我是否在这里盲目地遗漏了什么?

【问题讨论】:

【参考方案1】:

您正在使用带有 HSQL 驱动程序的 MySQL URL:

driverClassName="org.hsql.jdbcDriver"
url="jdbc:mysql://localhost:3306/Spring_Tutorial" />

您需要改用 MySQL 驱动程序:

driverClassName="com.mysql.jdbc.Driver"

【讨论】:

我知道我盲目地错过了一些东西。 JNDI 站点的剪切和粘贴错误...感谢启动并运行

以上是关于无法获得 JDBC 连接;嵌套异常是 java.sql.SQLException:无法加载 JDBC 驱动程序类 'org.hsql.jdbcDriver'的主要内容,如果未能解决你的问题,请参考以下文章

无法打开 JPA EntityManager 进行事务处理;嵌套异常是 org.hibernate.exception.SQLGrammarException:无法获取 JDBC 连接

网页显示无法打开JDBC连接事务;嵌套异常java.sql.SQLException:无法连接、 如何处理这种问题

Bug解决:获取JDBC连接失败;嵌套异常是java.sql.SQLException:无法从底层数据库获取连接

Java Derby DB 错误“线程“index-stat-thread”中的异常”和“无法获得 JDBC 连接”错误

无法提交 Hibernate 事务;嵌套异常是 org.hibernate.Transaction 异常:JDBC 提交失败

请求处理失败;嵌套异常是 org.hibernate.exception.ConstraintViolationException:无法执行 JDBC 批量更新