org.hibernate.exception.JDBCConnectionException:无法打开连接

时间:2016-05-06 11:03:22

标签: java hibernate

出于测试目的,我编写了非常小的代码,它反复抛出与jdbc驱动程序相关的异常。

   package sally;

   import org.hibernate.Session;
   import org.hibernate.Transaction;

   public class MainClass {

   public static void main(String args[]){

    try{
        Session session=HibernateUtil.getSession();
        Transaction tx=session.beginTransaction();
        Employee ee=new Employee("abx","asd",2000);
        session.save(ee);
        tx.commit();


    }catch(Exception e){
        e.printStackTrace();
    }
  }
}

这是我用于映射的ben / pojo类

package sally;

   public class Employee {
   private int id;
   private String firstName; 
   private String lastName;   
   private int salary;  

  public Employee() {}
  public Employee(String fname, String lname, int salary) {
  this.firstName = fname;
  this.lastName = lname;
  this.salary = salary;
  }
   public int getId() {
   return id;
 }
  public void setId( int id ) {
   this.id = id;
 }
  public String getFirstName() {
  return firstName;
  }
      public void setFirstName( String first_name ) {
      this.firstName = first_name;
      }
      public String getLastName() {
      return lastName;
       }
      public void setLastName( String last_name ) {
      this.lastName = last_name;
      }
        public int getSalary() {
        return salary;
      }
        public void setSalary( int salary ) {
        this.salary = salary;
      }
}


  <?xml version="1.0" encoding="utf-8"?>
  <!DOCTYPE hibernate-mapping PUBLIC 
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

 <hibernate-mapping>
   <class name="sally.Employee" table="EMPLOYEE">
     <meta attribute="class-description">
     This class contains the employee detail. 
     </meta>
     <id name="id" type="int" column="id">
      <generator class="native"/>
     </id>
    <property name="firstName" column="first_name" type="string"/>
    <property name="lastName" column="last_name" type="string"/>
    <property name="salary" column="salary" type="int"/>
  </class>
  </hibernate-mapping>

这是pojo类的cfg文件:

  <?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property    name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
    <property name="hibernate.connection.password"></property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>


    <mapping class="sally.Employee"></mapping>
</session-factory>

但每次我运行代码时都会抛出exxception:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
org.hibernate.exception.JDBCConnectionException: Cannot open connection
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
    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 sally.MainClass.main(MainClass.java:12)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test
    at java.sql.DriverManager.getConnection(DriverManager.java:596)
    at java.sql.DriverManager.getConnection(DriverManager.java:187)
    at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
    ... 5 more

任何人都可以在这里说错了吗?

这是db列表:first_name last_name salary。

3 个答案:

答案 0 :(得分:1)

您需要将mysql JDBC驱动程序JAR放在类路径

答案 1 :(得分:0)

“ org.hibernate.exception.JDBCConnectionException:无法打开连接”发生在您尚未启动xampp和MySql时。

答案 2 :(得分:0)

驱动程序类的名称必须以及服务器时区进行更新(对我来说这很疯狂,但必须这样做

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/hibernate?serverTimezone=UTC</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    </session-factory>
</hibernate-configuration>