Cannot connect to database using Hibernate - java

I am working on a sample Hibernate+MySQL application taken from here. I am not able to connect to the database when I try to run the program. Here is the stack trace.
The contents of hibernate.cfg.xml is as follows:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bookstore</property>
<property name="connection.username">root</property>
<property name="connection.password">helloWORLD12</property>
<property name="show_sql">true</property>
<mapping class="net.codejava.hibernate.Book" />
</session-factory>
</hibernate-configuration>
Where am I going wrong? I have checked that the database username and password is correct.

It might be because you're using an older version of the MySQL driver. You should try using the newest version.
To get the newest version, you can check
I think Suddenly we should not go to higher version as it causes more issues. I have just updated with the latest 5.1.x version. You can download it here or add this to your pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>

Based on stack trace issue is with server which is not reachable it seems.
Check if server is up. If possible use client to connect and check if it is up.
Restart mysql server once and check port as well if it is correct.
You configuration looks fine and it should work. Run a test on server and port to check you are using correct ones.

try this
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">helloWORLD12</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/bookstore</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

Related

Connecting to mysql database using IntelliJ [duplicate]

first of all I couldn't find an answer from related questions in this forum. I am new to Hibernate technology and I am trying to run a basic example from this page: http://www.roseindia.net/hibernate/hibernate4/firstHibernateApplication.shtml
Although, in eclipse it worked fine, I've got a problem with Intellij IDEA. Specifically, I have an error in the hibernate.cfg.xml file which says:
" 'com.mysql.jdbc.Driver' is not assignable to 'java.sql.Driver' ".
And when I run the program a message appears saying:
"ERROR: No suitable driver found for jdbc:mysql://127.0.0.1:3306/person Inserting Record Could not open connection"
('person' is my database) I have added the necessary jar files (or at least I think so) and generally I have followed all the steps from the webpage I pasted in the second line of this message. Please look at it and help me if you can. All the files and code I've created can be seen from the website I pasted. I've almost copied-pasted the example. Thank you.
UPDATED
hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/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://127.0.0.1:3306/person
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"/>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
I' ve placed the connector jar file at the directory IdeaProjects\coreHibernateExample\lib. No, I am not running Tomcat, I guess I am running through Intellij. The import of the Mysql database has been made through Wampservers' phpmyadmin.
You have to make the JDBC library jar available to your project. Just adding jar to your project directory does not accomplish that.
Select jar in your project, right click and add as a library.

Getting this Error : -" java.sql.SQLException: No suitable driver found for". When I use Hibernate Config for reverse eng. my DB

I'm trying to reverse Engeneer my simple MYSQL DB using Hibernate Configuration with eclipse IDE.But getting below error.
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3307/client1_db
I went through similar issues. This is a Marven Web Project and I can see the MYSQL_connector_java_5.1.34.jar is there under Marven Dependencies library. Some had suggested "No suitable driver found" means the drivers are loading but an issue with the DB connection. I have tried to connect to the localhost and the DB was created using MySQL workbench and it is accessible. I even tried using myphp admin and the DB is accessible.
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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bookshop</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop the existing tables and create new one -->
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
Then I tried using different server port 3307 as below. Still the same error.
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3307/client1_db</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
The full Error Message
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3307/client1_db
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:189)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:173)
at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.getConnection(AbstractMetaDataDialect.java:121)
at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.getMetaData(AbstractMetaDataDialect.java:60)
at org.hibernate.cfg.reveng.dialect.AbstractMetaDataDialect.caseForSearch(AbstractMetaDataDialect.java:163)
at org.hibernate.cfg.reveng.dialect.JDBCMetaDataDialect.getTables(JDBCMetaDataDialect.java:22)
at org.hibernate.cfg.reveng.JDBCReader.processTables(JDBCReader.java:476)
at org.hibernate.cfg.reveng.JDBCReader.readDatabaseSchema(JDBCReader.java:74)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.jboss.tools.hibernate.runtime.common.Util.invokeMethod(Util.java:43)
at org.jboss.tools.hibernate.runtime.common.AbstractJDBCReaderFacade.readDatabaseSchema(AbstractJDBCReaderFacade.java:44)
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter$2.execute(LazyDatabaseSchemaWorkbenchAdapter.java:132)
at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:63)
at org.hibernate.console.ConsoleConfiguration.execute(ConsoleConfiguration.java:107)
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.readDatabaseSchema(LazyDatabaseSchemaWorkbenchAdapter.java:124)
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.getChildren(LazyDatabaseSchemaWorkbenchAdapter.java:64)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:104)
at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:219)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Hibernate Configuration screenshot
Package Explore
Please visit the below page :
https://javarevisited.blogspot.com/2016/09/javasqlsqlexception-no-suitable-driver-mysql-jdbc-localhost.html
Instead of doing the reverse engineering directly I first added the Database to the Eclipse providing JDBC driver path. After testing the connection was successful then performed the reverse engineering and it was successful.
Steps >
01. Go to Data Source Explore
Add the DB connection
Make sure you map the JDBC drivers
04.Test at the end. Make sure your server is running..

Hibernate Exception while deploying on VPS server

I am using VPS server first time to deploy my application. I have created a Hibernate Project. After Deploying it is giving me following error
org.hibernate.HibernateException: Could not parse configuration:
/hibernate.cfg.xml
org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2246)
org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:230)
org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:71)
org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:212)
org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:71)
org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:206)
util.HibernateUtil.(HibernateUtil.java:26)
services.HibernateServices.(HibernateServices.java:41)
servlet.GetShopProfile.processRequest(GetShopProfile.java:42)
servlet.GetShopProfile.doGet(GetShopProfile.java:77)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
To test if my database is working fine, I created a normal database connection class and it was working fine.
hibernate.cfg.xml
<?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/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.pool_size">1</property>
<property name="hibernate.current_session_context_class">thread</property>
<!--<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>-->
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<mapping class="pojo.TestClass"/>
</session-factory>
</hibernate-configuration>
Directory Structure
WEB-INF
classes
pojo
TestClass.class
services
HibernateService.class
servlet
TestServlet.class
util
HibernateUtil.class
hibernate.cfg.xml
We have Tomcat 7.0 installed.
Kindly help!!!
I solved the problem by reinstalling the tomcat on my server.
Any ways this issue can be solved by changing the DTD as suggested in questions: here
I tried multiple solutions in my case but at last when nothing worked I reinstalled the tomcat and every thing was fine
Thanks every one for their inputs and time.
Can you try adding space in your commented lines?
<!--<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>-->
Replace above line with
<!-- <property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property> -->
I think starting tag < after your comment start section is causing xml parsing issue.
Change the hibernate.cfg.xml as
<?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 error while reading schema

Hey stackoverflow,
I tried to add a Hibernate configuration to my workspace in order to reverse engineer my database.
When I am opening the database point in the 'Hibernate Configurations' view, i get a Reading schema error: null with the following exception:
java.lang.NullPointerException
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter$1.compare(LazyDatabaseSchemaWorkbenchAdapter.java:76)
at java.util.TimSort.countRunAndMakeAscending(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.toArray(BasicWorkbenchAdapter.java:75)
at org.hibernate.eclipse.console.workbench.LazyDatabaseSchemaWorkbenchAdapter.getChildren(LazyDatabaseSchemaWorkbenchAdapter.java:74)
at org.hibernate.eclipse.console.workbench.BasicWorkbenchAdapter.fetchDeferredChildren(BasicWorkbenchAdapter.java:104)
at org.eclipse.ui.progress.DeferredTreeContentManager$1.run(DeferredTreeContentManager.java:238)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
Has someone experienced this before?
I am running a fresh installation of Eclipse Luna / Newest JBoss Tools and trying to connect to a PostgreSQL 9.3 DB.
Thank you in advance
EDIT:
My hibernate.cfg.xml:
<?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">org.postgresql.Driver</property>
<property name="hibernate.connection.password">******</property>
<property name="hibernate.connection.url">jdbc:postgresql://10.244.7.77:5432/netview</property>
<property name="hibernate.connection.username">admin</property>
<property name="hibernate.default_schema">public</property>
</session-factory>
</hibernate-configuration>
EDIT 2:
I just realized, if i add a default schema <property name="hibernate.default_schema"> it works, but just for this schema.
EDIT 3:
It works with an old version of Hibernate tools (3.6.0.M1-v20120827-0757-H1125). Now i am confused.
Try out this config:
<!-- Database connection settings -->
org.postgresql.Driver
jdbc:postgresql://host:port/database
postgres
password
1
org.hibernate.dialect.PostgreSQLDialect
thread
org.hibernate.cache.internal.NoCacheProvider
true
Try out this config:
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://host:port/database</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
</session-factory>
So i found out that it might be a bug in JBoss Tools. So I opened a ticket: https://issues.jboss.org/plugins/servlet/mobile#issue/JBIDE-19830

Netbeans 7.1 Hibernate Reverse Engineering wizard does not find database driver

I am currently trying to get through this netbeans + hibernate + JavaSE tutorial ( http://netbeans.org/kb/docs/java/hibernate-java-se.html). All is pretty fine, but after the creation of the hibernate.cfg.xml when it comes to the part where reverse engineering should be applied it comes to some weird message that the reverse-engineering wizard tells me:
"The database drivers are not added to the project classpath."
"Go to project properties to add database library.".
Well that's kind of weird, because the hibernate.cfg.xml was generated by netbeans. I've checked my database connection with the connection data from the hibernate.cfg.xml and it seems to be all okay, so connecting manually works pretty fine. Anyone knows what is going on here? Am I doing something wrong?
<?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/sakila</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">apassword</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
</session-factory>
</hibernate-configuration>
Add these line of code in hibernate.cfg.xml
<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/DATABASE_NAME</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"/>
</session-factory>
Besides missing jar files, here is another possible way that could cause this error in netbeans: wrong configuration file.
So make sure you have the correct configuration file (.cfg.xml file) to build .reveng
Attached an example configuration:
<?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.password">1234</property>
<property name="hibernate.connection.url">jdbc:mysql://161.58.103.144:3306/exampleDatabase?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">JasonGlez</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
Just change the ip, the name of the database, your username and password
You are missing the JDBC Driver. At the Project View right click the Libraries node under your project and choose Add Library... -> MySQL JDBC Driver.
As mentioned by hello, make sure the hibernate.cfg.xml is good. Try regenerating it though from Netbeans Hibernate Configuration Wizard as opposed to crafting it yourself. Also try recreating the project and make sure the new project wizard populates the hibernate.cfg.xml with database connection settings. It came out blank for me the first time; not sure why. Before it started working, I had also restarted Netbeans and the computer to boot, which might not hurt to try. (No pun intended.)
This required MySQL driver, Add My sql Driver in Library to resolve the issue

Categories