Issue
My Problem database connectvity WARN: SQL Error: 0, SQLState: 08001 May 18, 2021 8:08:09 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: No suitable driver found for I faced problem with database connectivity .I am using Eclispse Database 10g Tomcat 8.5 Ojdbc 14
My Code is hibernate.cfg.xml
<?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="connection.driver_Class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@//localhost:1521/XE</property>
<property name="connection.username">system</property>
<property name="connection.password">system</property>
<property name="hibernate.default_schema">system</property>
<property name="dialect">org.hibernate.dialect.Oracle8iDialect</property>
<property name="hdm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping class="com.entities.Note"/>
</session-factory>
</hibernate-configuration>
**SaveNoteServlet.java**
public class SaveNoteServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public SaveNoteServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//title ,content fetch
String title=request.getParameter("title");
String content=request.getParameter("content");
Note note=new Note(title,content,new Date());
System.out.println(note);
//hibernate :save
Session ses=HibernateUtil.getSessionFactory().openSession();
System.out.println(ses);
Transaction t=ses.beginTransaction();
ses.save(note);
t.commit();
ses.close();
System.out.println("Tble created");
}catch(Exception e) {
}
}
}
**HIbernateUtil.java**
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static SessionFactory buildSessionFactory() {
StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder().
configure("hibernate.cfg.xml").build();
Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().
build();
SessionFactoryBuilder sessionFactoryBuilder = metadata.getSessionFactoryBuilder();
SessionFactory sessionFactory = sessionFactoryBuilder.build();
return sessionFactory;
}
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
sessionFactory = buildSessionFactory();
}
return sessionFactory;
}
public static void close() {
if(sessionFactory.isOpen()) {
sessionFactory.close();
}
}
}
Error code-
May 18, 2021 8:08:04 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.5.Final}
May 18, 2021 8:08:08 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
May 18, 2021 8:08:08 PM
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
May 18, 2021 8:08:09 PM
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [null] at URL []
May 18, 2021 8:08:09 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {password=****, driver_Class=oracle.jdbc.driver.OracleDriver, user=system}
May 18, 2021 8:08:09 PM
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
May 18, 2021 8:08:09 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
May 18, 2021 8:08:09 PM org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : Cannot invoke "org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PoolState.getConnection()" because "this.state" is null
May 18, 2021 8:08:09 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle8iDialect
May 18, 2021 8:08:09 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 08001
May 18, 2021 8:08:09 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: No suitable driver found for
Solution
I also faced the same error, but after searching on various websites and trying their solutions I come to know that the dependency that you have included for MySQL should have the same version installed in your system.
In my case, this dependency works fine-
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
you can also try the different versions.
Answered By - user9327151
Answer Checked By - Mary Flores (JavaFixing Volunteer)