-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: getting error : No suitable driver while connecting to mysql
PostPosted: Fri May 06, 2016 1:36 pm 
Newbie

Joined: Fri May 06, 2016 1:28 pm
Posts: 3
Hi,
I am learning Hibernate . I am trying to connect mysql5 using hibernate4 .In Eclipse I have Created POJO Class, congiguration file, mapping file and one test class and I added all the necessary hibernate jar files in class path . I added mysql-connector jar also in classpath.
But when i am trying run , i am getting this error :

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

Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Could not open connection
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:131)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:304)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1363)
at test.Client.main(Client.java:27)



hibernate.cfg.xml:
-------------------------------------

<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/sreedb</property>
<property name="connection.username">root</property>
<property name="connection.password">root123</property>
<property name="connection.pool_size">10</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="resources/student.hbm.xml" />

</session-factory>
</hibernate-configuration>


--------------------------------------------
I installed mysql using : mysql-installer-community-5.7.12.0

Mysql driver jar file : mysql-connector-java-5.0.8-bin

downloaded : hibernate-release-4.1.2.Final

I added the jar files to Project -> BuildPath-> Configure BuildPath -> libraries -> add external jars .


Please let me know how to fix this error.

I was able to connect to this database using JDBC with same db url , username and password.
Please help .....

Thanks in advance...
sree


Top
 Profile  
 
 Post subject: Re: getting error : No suitable driver while connecting to mysql
PostPosted: Sat May 07, 2016 7:48 am 
Newbie

Joined: Sun May 01, 2016 11:34 am
Posts: 2
Try something like this(class HibernateUtil):
package initHb;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
*
* @author morar
*/
public class HibernateUtil {

private static SessionFactory sessionFactory;
private static StandardServiceRegistry serviceRegistry;

public static SessionFactory createSessionFactory(String dbName, String dbUser, String dbPwd, String[] mapClass ) throws ClassNotFoundException{
// change params
Configuration cfg = new Configuration();
cfg.configure();
for(int i = 0; i < mapClass.length; i++){
Class c = Class.forName(mapClass[i]);
cfg.addAnnotatedClass(c);//.configure();
}
cfg.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/" + dbName);
cfg.setProperty("hibernate.show_sql", "false");
cfg.setProperty("hibernate.connection.username", dbUser);
cfg.setProperty("hibernate.connection.password", dbPwd);


serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
sessionFactory = cfg.buildSessionFactory((org.hibernate.service.ServiceRegistry) serviceRegistry);

return sessionFactory;
}

}

// You can set anything, anythime, anywhere... Works nice(NetBeans 8.1, Java1.8(Oracle) ScientificLinux 6.7 32 bit, MariaDb 10.1
// Hibernate 4.3 , mysql-connector-java-5.1.23-bin.jar
// I prefer to use, annotation

/* Hibernate.cfg.xml file */
<?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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/my_test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"/>
<!-- <mapping class="docs.DocType"/> -->
<property name="hibernate.bytecode.use_reflection_optimizer">true</property>

</session-factory>
</hibernate-configuration>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.