简体   繁体   中英

ERROR: No suitable driver found for jdbc:mysql://localhost:3306/test

I am getting no suitable driver exception. Connection is not created.

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>

This is my code and and iam facing the Same issue.

You need to add this property to the hibernate.cfg.xml

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

And you need to have mysql-connector-java jar at the class path.

Your transaction code is not correct (you don't use rollback, for an example). Refer this for how to properly work with that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM