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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30175545/
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-11-02 16:36:08  来源:igfitidea点击:

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

javamysqlmavenjdbcjndi

提问by Mike3355

I am not to sure why I am unable to connect to my database. I am using JNDI, and maven to help handle the dependencies. In my context.xml in my server folder in eclipse looks like:

我不确定为什么我无法连接到我的数据库。我正在使用 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

上下文.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>

this class handles the error if it is unable to connect:

如果无法连接,此类将处理错误:

@ControllerAdvice
public class DatabaseErrorHandler {

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

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

        return "error";
    }

}

pom.xml

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

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

I added the hsqldb jar file but I really did not think I needed it

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

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

From my understand I just need the mysql-connector-java dependency

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

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

My database's name is Spring_Tutorial and I am using mySQL Workbench.

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

Am I blindly missing something here?

我在这里盲目地错过了什么吗?

回答by ericbn

You're using a MySQL URL with a HSQL driver:

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

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

You need to use a MySQL driver instead:

您需要改用 MySQL 驱动程序:

driverClassName="com.mysql.jdbc.Driver"