线程" main"中的例外情况org.hibernate.exception.GenericJDBCException:无法打开连接

时间:2015-05-05 14:58:19

标签: java hibernate hibernate-mapping

我正在尝试运行一个简单的java项目来查看hibernate的工作但是当我执行我的代码(StoreData.java)时它给出了一个hibernate异常,我使用的是oracle 11g R2和eclipse galileo,我的oracle SID是" ORCL"连接名称是" mysql"用户名是" sys"密码是" oracle"。这是代码:

Employee.java:

    *package com.persist;
public class Employee {

    private int id;
    private String firstName;
    private String lastName;


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }


}*

employee.hbm.xml:

    *<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

 <hibernate-mapping>

 <class name="com.persist.Employee" table="Emp">

 <id name="id">
 <generator class="assigned"></generator>
 </id>
 <property name="firstName"></property>
 <property name="lastName"></property>

  </class>

 </hibernate-mapping>*

hibernate.cfg.xml:

    *<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">sys</property>
<property name="connectio.password">oracle</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping  resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>*

StoreData.java:

    *package com.DAO;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.persist.Employee;
public class StoreData {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Configuration cf = new Configuration();
        cf.configure("hibernate.cfg.xml");

        SessionFactory factory =cf.buildSessionFactory();

        Session session =factory.openSession();

         Transaction t =session.beginTransaction();

         Employee e = new Employee();
         e.setId(1);
         e.setFirstName("samar");
         e.setLastName("singh");

         session.persist(e);

         t.commit();
         session.close();

         System.out.println("successfully saved");
    }
}*

控制台:

    *log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Cannot open connection
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
    at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
    at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
    at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
    at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
    at com.DAO.StoreData.main(StoreData.java:27)
Caused by: java.sql.SQLException: invalid arguments in call
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:236)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
    ... 5 more*

项目层次结构:

hiber is my project name

included jar files

2 个答案:

答案 0 :(得分:0)

尝试使用以下属性:

<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
<property name="connection.url">jdbc:Oracle:thin:@localhost:1521:orcl</property>  
<property name="connection.username">sys</property>  
<property name="connection.password">oracle</property>  
<property name="dialect">org.hibernate.dialect.OracleDialect</property>  

答案 1 :(得分:0)

尝试找到最新的ojdbc jar,旧的ojdbc14 jar不会使用最新的db,我得到了同样的问题我用ojdbc7 jar问题解决了