首页 新闻 会员 周边

java.sql.SQLException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'

0
[已关闭问题] 关闭于 2018-06-02 22:07

使用spring、dbcp链接mysql出现错误提示:
java.sql.SQLException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
我的开发环境是:
1.WIN7 64位,旗舰版
2.Mysql 5.7安装在本机,默认端口号3306,在Navicat中使用root用户和对应密码能正常登陆链接到其中的数据库
3.Eclipse版本号是:Oxygen.3a Release (4.7.3a)
4.导包的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.xdf.spring</groupId>
<artifactId>springDBCPMySQL01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>

<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.7</version>
</dependency>


<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.3.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>

 

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
</project>

 

5. db.properties的内容是:

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/pademisaccount
username=root
password=xxx

 

6. spring用到的XML文件 - springContext.xml的内容是:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<!-- 本文件是spring的配置文件 -->
<context:component-scan base-package="comp"/>

<util:properties id="dbConfig" location="classpath:db.properties"></util:properties>

<bean id="ds" class="org.apache.commons.dbcp2.BasicDataSource" >
<property name="driverClassName" value="#{dbConfig.driverClass}" />
<property name="url" value="#{dbConfig.url}" />
<property name="username" value="#{dbConfig.username}" />
<property name="password" value="#{dbConfig.password}" />
</bean>
</beans>

 

7. 在工程中的src/test/java中制作测试用例的代码如下:

package test;

import java.sql.SQLException;

import org.apache.commons.dbcp2.BasicDataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestCase {
@Test
public void test01() throws SQLException {
ApplicationContext ac = new ClassPathXmlApplicationContext("springContext.xml");
BasicDataSource ds = ac.getBean("ds",BasicDataSource.class);
System.out.println(ac.getBean("dbConfig"));
System.out.println(ds);
System.out.println(ds.getConnection());
}
}

 

8. 上面的3个print语句前面两个都打印出来了,运行到第三个的时候出现错误提示:
java.sql.SQLException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'

是什么原因导致的这个问题,如何解决。
thanks for any help!

chanchaw的主页 chanchaw | 初学一级 | 园豆:65
提问于:2018-06-02 18:48

解决了,使用mysql-connector-java 5.1.31就可以了

chanchaw 5年前
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册