简体   繁体   中英

HTTP Status 500 nested exception is Unable to load class [oracle.jdbc.driver.OracleDriver]

I am creating basic spring MVC project with hibernate 4.3.6 and I am using oracle 11g as my database. but I am constantly getting below exception

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

I have installed ojdbc6 jar in my local and add it as maven dependency as well as I tried by adding as external jar but it seems its not working.please let me where I am wrong?

Here's my 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>

Here is my 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();
}
}

Edit 1: below SS of project structure 在此处输入图片说明

Edit 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;
}



}

My Service class

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();
    }

}

My webIntilizer (web.xml's java version)

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[]{"/"};
    }

}

( edit to provide an alternative )

A better approach than the below is to install the ojdbc6.jar in your Maven repo.

Supposing your are in the lib where ojdbc6.jar is and supposing mvn is installed in command line run (replace version with your version):

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

After this completes successfully you can refer to it in your pom.xml with the new Maven coordinates:

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

The error message essentially says that your Oracle jdbc driver hasn't been successfully imported into your project. I suggest creating a lib directory into your project directly, put the ojdbc6.jar in there and change the Maven coordinates for the driver to:

    <!-- 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>

您是否尝试过将ojdbc6.jar放在[JAVA_HOME] / jre / lib / ext中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM