Hibernate 不想加载 Oracle 驱动程序

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

Hibernate doesn't want to load Oracle driver

javaoraclehibernate

提问by Martin Dimitrov

I downloaded Hibernate 4.1.2 and am using Oracle Database 10g Release 2. The JDBC driver I am using is ojdbc14.jar.

我下载了 Hibernate 4.1.2 并使用了 Oracle Database 10g 第 2 版。我使用的 JDBC 驱动程序是ojdbc14.jar.

I set up HibernateUtil class as:

我将 HibernateUtil 类设置为:

public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        // Create the SessionFactory from hibernate.cfg.xml
        try{
            Configuration configuration = new Configuration();
            configuration.configure();
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
            return configuration.buildSessionFactory(serviceRegistry);
        }catch(HibernateException ex){
            ex.printStackTrace();
            throw ex;
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

In hibernate.propertiesI have:

hibernate.properties我有:

hibernate.dialect org.hibernate.dialect.OracleDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username HR
hibernate.connection.password HR
hibernate.connection.url jdbc:oracle:thin:@localhost:1521/xe

But Hibernate doesn't want to load the driver. It throws an exception saying 'No appropriate driver found'.

但是 Hibernate 不想加载驱动程序。它抛出一个异常,说“找不到合适的驱动程序”。

I tried to load the driver with Class.forName("oracle.jdbc.driver.OracleDriver");and it works fine.

我尝试加载驱动程序,Class.forName("oracle.jdbc.driver.OracleDriver");它工作正常。

回答by Martin Dimitrov

The problem was in using the wrong JDBC Oracle driver. When I tried with ojdbc6.jareverything worked fine.

问题在于使用了错误的 JDBC Oracle 驱动程序。当我尝试ojdbc6.jar一切正常时。

回答by nwinkler

A couple of things:

几件事:

  • Try to make the properties file valid by putting =between key and value
  • Check that there aren't any trailing spaces after the values
  • Use oracle.jdbc.OracleDriverinstead of oracle.jdbc.driver.OracleDriver. See Difference between Oracle jdbc driver classes?for further reference.
  • 尝试通过在键和值之间放置=来使属性文件有效
  • 检查值后是否没有任何尾随空格
  • 使用oracle.jdbc.OracleDriver代替oracle.jdbc.driver.OracleDriver。请参阅Oracle jdbc 驱动程序类之间的区别?以供进一步参考。

回答by Pau Kiat Wee

Your connection URL is configured wrongly, should be:

您的连接 URL 配置错误,应该是:

hibernate.connection.url jdbc:oracle:thin:@localhost:1521:xe

More information for Oracle's URL can refer here.

有关 Oracle 的 URL 的更多信息,请参阅此处

As other answer point out:

正如其他答案指出的那样:

Use oracle.jdbc.OracleDriverinstead of oracle.jdbc.driver.OracleDriver

使用oracle.jdbc.OracleDriver代替oracle.jdbc.driver.OracleDriver