Connecting to mysql database using IntelliJ [duplicate] - java

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.

Related

Cannot resolve class dialect

I am trying to fix an issue for hours but nothing seems to work.I tried everything available online. I am using Intellij IDEA. I am using a Maven project. I have created hibernate.cfg.xml with following content
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="User"/>
</session-factory>
</hibernate-configuration>
Following line is throwing an error that cannot resolve class dialect
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
My External Libraries structure looks like this.
any help would be appreciated.
I fixed the problem. The problem was Intellij IDEA was not able to download maven files for Hibernate. I used maven repository for adding the repository.

Cannot connect to database using Hibernate

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>

Hibernate - MySQL - Connection to DB

I'm trying to connect to a MySQL database using Hibernate 3.6.10 with NetBeans 7.4, but when I try to create a new "Hibernate Mapping Wizard" I reply this error message:
Unable to connect: Cannot establish a connection jdbc:mysql://localhost:3306/mydbname using apache.org.derby.jdbc.ClientDriver(Unable to find a suitable driver)
This is my "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/database</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
</session-factory>
</hibernate-configuration>
I've found another similar problem here: Hibernate - Cannot connect to DB but I still not understand what's wrong.
Best regards.
Andrea
I have never used Hibernate Mapping Wizard but as far as I know this is useful to create a hibernate mapping file with an extension .hbm.xml: see this link as reference.
So the result is a .hbm.xml file that is the mapping of your class to your table.
The point is the in your hibernate.cfg.xml there is no explicit reference to this .hbm.xml file.
Try to add:
<mapping resource="<your_generated_file>.hbm.xml"/>
in your hibernate.cfg.xml and be sure to give all the data needed when using the Hibernate Mapping Wizard tool.
Hope this will be useful!
Ciao!
Nambari and Paolo,
I had to put my config.prop's and mysql driver's "Path" library into CLASSPATH variable. Now it works.
Thankyou all!
Ciao Paolo, grazie!

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

Derby database don't save entity after application running

I'm using Embedded Derby DB with hibernate. I'm saving some entities to database. After shutting down the application there is no entities in DB. Why it could be so?
Below my Hibernate 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="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
<property name="connection.url">jdbc:derby:\bases\localbase;create=true</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<mapping class="com.example.model.HistoryItem"/>
<mapping class="com.example.model.User"/>
<mapping class="com.example.model.BaseAbstractEntity"/>
</session-factory>
</hibernate-configuration>
This is weird because Derby sync to the disk after each commit by default (unless you set the property derby.system.durability=test). And your url looks correct (although I would use forward slashes).
This begs the question: how do you managed transactions? Are you sure that you are committing them?
Derby likes to hang on to the data - ie keep it in memory. If you try shutting it down by trying something like:
DriverManager.getConnection("jdbc:derby:;shutdown=true");
This should make sure the data is flushed to the disk and derby is shut down cleanly.
At the moment I am trying to get Hibernate 3.6 to do this for me - no luck so far.
Perhaps you are looking in the wrong place. Perhaps the Hibernate application is creating a Derby database in one location on your hard disk, but when you look for the database contents later, you are looking in a different location on your hard disk. Since your Derby URL says "create=true", Derby will quietly create a new empty database for you if you don't get exactly the same location specification as in your original app.

Categories