在 spring jdbc javapoint 中执行 spring jdbc 示例时出现以下错误,
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ds' defined in class path resource [sjdbc1.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [oracle.jdbc.driver.OracleDriver]
Employee.java 我修改如下,其余的都是我从 javapoint 链接获取的,
1.Employee.java
package com.pro.sjdbc1;
public class Employee {
private int id;
private String name;
private float salary;
// no-arg and parameterized constructors
// getters and setters
Employee(int id,String name,float salary){
id=this.id;
name=this.name;
salary=this.salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
}
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>
<groupId>com.pro</groupId>
<artifactId>Test1</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Test1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.0.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/xdoclet/xdoclet -->
<dependency>
<groupId>xdoclet</groupId>
<artifactId>xdoclet</artifactId>
<version>1.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/xjavadoc/xjavadoc -->
<dependency>
<groupId>xjavadoc</groupId>
<artifactId>xjavadoc</artifactId>
<version>1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjtools -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>Test1</finalName>
</build>
</project>
谢谢。
最佳答案
您的类路径中没有 Oracle JDBC 驱动程序。您需要在 POM 文件中添加依赖项。
例如
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc7</artifactId>
<version>x.xx</version>
</dependency>
请注意,这是一个示例。您将需要添加正确的驱动程序版本。 您可能还需要添加适当的 Oracle Maven 存储库 (https://maven.oracle.com)。 AFAIK Oracle JDBC 驱动程序不在标准 Maven 中央存储库中。
关于java - Spring 无法加载 JDBC 驱动程序类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41118698/