gpt4 book ai didi

java - HTTP 状态 500 嵌套异常无法加载类 [oracle.jdbc.driver.OracleDriver]

转载 作者:行者123 更新时间:2023-12-02 03:10:50 25 4
gpt4 key购买 nike

我正在使用 hibernate 4.3.6 创建基本的 spring MVC 项目,并使用 oracle 11g 作为我的数据库。但我总是遇到异常

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [oracle.jdbc.driver.OracleDriver]

我已经在本地安装了 ojdbc6 jar 并将其添加为 Maven 依赖项,并且我尝试添加为外部 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>
<groupId>springMVC</groupId>
<artifactId>assignment</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>assignment Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>

</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
<!-- ORACLE database driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
<scope>system</scope>
<systemPath>F:\ojdbc6.jar</systemPath>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

</dependencies>
<build>
<finalName>assignment</finalName>
</build>
</project>

这是我的 config.java

package assignment.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.springframework.stereotype.Component;

import assignment.service.UserEntity;

@Component
public class HibernateUtil {
private static SessionFactory sessionFactorty=null;
public static SessionFactory getSessionFactory(){
if(sessionFactorty==null){
System.out.println("inside hibernate Util");
Configuration configuration=new Configuration();
configuration.addAnnotatedClass(UserEntity.class);

configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
configuration.setProperty("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");
configuration.setProperty("hibernate.connection.username", "SYSTEM");
configuration.setProperty("hibernate.connection.password", "sa");
configuration.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@127.0.0.1:1158:myDb");
SchemaExport schemaExport=new SchemaExport(configuration);
schemaExport.create(true, true);
StandardServiceRegistryBuilder srb=new StandardServiceRegistryBuilder();
srb.applySettings(configuration.getProperties());
ServiceRegistry serviceRegistry=srb.build();
sessionFactorty=configuration.buildSessionFactory(serviceRegistry);

}
return sessionFactorty;


}
public static void shutDown(){
if(sessionFactorty==null)
sessionFactorty.close();
}
}

编辑1:低于项目结构的 SS enter image description here

编辑2:LoginController.java

package assignment.view;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import assignment.model.User;
import assignment.model.Validator;

@Controller
@RequestMapping("/login")
public class LoginController {
@Autowired
Validator validator;
@RequestMapping(method=RequestMethod.POST)
public String userLogin(@ModelAttribute("userForm") User user){
System.out.println(user);
validator.process(user);
return "success";
}

@RequestMapping(method=RequestMethod.GET)
public String register(Model model){
User user=new User();
model.addAttribute("userForm", user);
return "Register";
}
}

UserEntity.java

package assignment.service;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="User")
public class UserEntity implements Serializable {

private static final long serialVersionUID = -6620152467355557520L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer userId;
@Column(nullable=false)
private String userName;
@Column(nullable=false)
private String email;
@Column(nullable=false)
@Temporal(TemporalType.DATE)
private Date dob;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}



}

我的服务类

package assignment.service;

import org.hibernate.Session;
import org.springframework.stereotype.Component;

import assignment.model.User;
import assignment.util.HibernateUtil;
import assignment.util.ServiceUtil;

@Component
public class UserService {


public void saveUser(User user) {
Session session=HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
UserEntity userEntity=new UserEntity();
userEntity.setDob(ServiceUtil.stringToDateConverter(user.getDob()));
userEntity.setEmail(user.getEmail());
userEntity.setUserName(user.getUserName());
session.save(userEntity);
session.getTransaction().commit();
session.disconnect();
HibernateUtil.shutDown();
}

}

我的 webIntilizer(web.xml 的 java 版本)

package assignment.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebInitilizer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {

return new Class<?>[]{RootConfig.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {

return new Class<?>[]{WebConifg.class};
}

@Override
protected String[] getServletMappings() {

return new String[]{"/"};
}

}

最佳答案

(编辑以提供替代方案)

比下面的方法更好的方法是在 Maven 存储库中安装 ojdbc6.jar

假设您位于 ojdbc6.jar 所在的库中,并假设在命令行运行中安装了 mvn(将版本替换为您的版本):

mvn install:install-file -Dfile=ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 -Dpackaging=jar -DgeneratePom=true

成功完成后,您可以在 pom.xml 中使用新的 Maven 坐标引用它:

<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<小时/>

该错误消息实质上表明您的 Oracle jdbc 驱动程序尚未成功导入到您的项目中。我建议直接在项目中创建一个 lib 目录,将 ojdbc6.jar 放入其中,并将驱动程序的 Maven 坐标更改为:

    <!-- ORACLE database driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/ojdbc6.jar</systemPath>
</dependency>

关于java - HTTP 状态 500 嵌套异常无法加载类 [oracle.jdbc.driver.OracleDriver],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41079407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com