2016-02-04 70 views
1

我沒有找到合適的驅動程序例外。連接未被創建。錯誤:找不到適合jdbc的驅動程序:mysql:// localhost:3306/test

package org.srtmun.student.dao.impl; 
import javax.transaction.Transaction; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.srtmun.student.dao.RegestrationDAO; 
import org.srtmun.student.hibernateplugin.HibernatePlug; 
import org.srtmun.student.model.Registration; 

public class RegistrationDaoImpl implements RegestrationDAO{ 
    public void addStudent(Registration register) { 
     System.out.println("RegistrationDaoImpl class1"); 
     SessionFactory factory = HibernatePlug.getFactory(); 
     System.out.println("1"); 
     Session session=factory.openSession(); 
     org.hibernate.Transaction tx=session.beginTransaction(); 
     session.save(register); 
     tx.commit(); 
     session.close(); 
    } 
} 


<?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.password">123</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <mapping resource="Registration.hbm.xml" /> 
    </session-factory> 
</hibernate-configuration> 

這是我的代碼,和iam面臨同樣的問題。

+0

你的驅動程序在哪裏? ? – logger

回答

1

您需要將此屬性添加到hibernate.cfg.xml

<property 
    name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 

而且你需要有mysql-connector-java jar在類路徑中。

您的交易代碼不正確(例如,您不使用回滾)。請參閱this以瞭解如何正確使用。

相關問題