无法使用Hibernate连接到数据库

时间:2018-11-30 01:54:07

标签: java hibernate

我有此错误堆栈,并且我的第一个休眠程序未运行。

例外:

  org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
            at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:122)
            at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:140)
            at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:58)
            at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:75)
            at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106)
            at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
            at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
            at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
            at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
            at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
            at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
            at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
            at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
            at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
            at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
            at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
            at hibernate.StudentManager.main(StudentManager.java:22)
        Caused by: java.sql.SQLException: NL Exception was generated
            at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
            at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
            at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
            at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:480)
            at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:413)
            at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:508)
            at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:203)
            at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
            at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:510)
            at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:55)
            ... 14 more
        Caused by: oracle.net.ns.NetException: NL Exception was generated
            at oracle.net.resolver.AddrResolution.resolveAddrTree(AddrResolution.java:613)
            at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:403)
            at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:630)
            at oracle.net.ns.NSProtocol.connect(NSProtocol.java:206)
            at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966)
            at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292)

我尝试了不同的连接URL,因为它是SQL异常/错误,但我总是会遇到这个问题

配置文件:

<hibernate-configuration>
  <session-factory>  
        <property name="hbm2ddl.auto">update</property>  
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>  
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe;databaseName=HibernatePractice</property>  
        <property name="connection.username">system</property>  
        <property name="connection.password">manager</property> 
        <property name="show_sql">true</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
    <mapping resource="hbm/hibernate.hbm.xml"/>
    </session-factory>  
</hibernate-configuration>

1 个答案:

答案 0 :(得分:0)

我建议检查以下内容

  1. 检查端口1521上的DB是否可访问
  2. 我怀疑您的网址配置有误

应该是这样

<property name=”hibernate.connection.url”>jdbc:oracle:thin:@host:1521:databaseName</property>

所以您的URl是

<property name="connection.url">jdbc:oracle:thin:@localhost:1521:HibernatePractice</property> 

或尝试

<property name="connection.url">jdbc:oracle:thin:@localhost:1521/HibernatePractice</property>
  1. 该问题可能与您使用的驱动程序jar版本有关。

更多信息请参考here