What mysql driver am i using spring / hibernate?

A bit confusing are the 'driverclassname' and 'hibernate.dialect' referring to the mysql driver?

What should i use? Is connector J the one to use?

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.hsqldb.jdbcDriver"/> <property name="url" value="jdbc:mysql://localhost/blah"/> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect 

I use Maven, so if I can get a driver from maven that would be ideal.

Running my application on tomcat I get an error message:

 Cannot create JDBC driver of class 'org.hsqldb.jdbcDriver' for connect URL 
+5
java spring mysql hibernate
source share
3 answers

A bit confusing are the 'driverclassname' and 'hibernate.dialect' referring to the mysql driver?

No, it is not. driverclassname refers to the name of the driver class, which is the class from this JDBC driver that implements java.sql.Driver . The driver class name is driver specific.

When using the MySQL JDBC driver aka MySQL Connector / J, this class is com.mysql.jdbc.Driver , as described in the MySQL Connector / J documentation:

20.3.4.1. Driver / Data Source Class Names, URL Syntax, and Configuration Properties for Connector / J

The name of the class that implements java.sql.Driver in the MySQL Connector / J com.mysql.jdbc.Driver . (...)

And in fact, they even provide instructions for using their driver with Spring . See Section 20.3.5.2.4. Using Connector / J with Spring .

hibernate.dialect is different, this configuration property is used to determine the name of the Hibernate class org.hibernate.dialect.Dialect , which allows Hibernate to generate SQL optimized for a particular relational database. Again, this is explained in the Hibernate documentation:

3.4. Additional configuration properties

(...) The name of the hibernate class class is org.hibernate.dialect.Dialect , which allows Hibernate to generate SQL optimized for a particular relational database.

e.g. full.classname.of.Dialect

In most cases, Hibernate will be able to select the correct org.hibernate.dialect.Dialect JDBC-based implementation metadata returned by the JDBC driver.

For MySQL 5.x, you should use org.hibernate.dialect.MySQL5InnoDBDialect if you are using InnoDB tables (that would be my recommendation) or org.hibernate.dialect.MySQL5Dialect if you haven’t. See Section 3.4.1. SQL Dialects for a (non-exhaustive) list.

The last point, the Maven part that you did not even mention in your question ... The MySQL JDBC driver is available in the central Maven repository, and you should use the engine storage search (as I already suggested ). For example, the following query:

http://www.jarvana.com/jarvana/search?search_type=project&project=mysql

allows you to find the maven coordinates of the final version in two clicks:

 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.13</version> </dependency> 

PS: I do not want to be rude, and I am happy to help, but you really should try to use the documentation about the products or frameworks that you use. What you ask in this question is well documented (as I showed) and it can be easily found. Learning how to find basic information on your own is a fundamental skill for a software developer, in my opinion.

+20
source share

driverClassName should refer to the name of the JDBC driver class that you want to load (as usual, using Class#forName() in "plain" JDBC). The one you are currently specifying is correct for the MySQL JDBC driver ( Update ): you quickly edited it as a JQLBC HSQLDB driver, this is only true for a hypersonic database, not a MySQL database).

hibernate.dialect should refer to the name of the Hibernate Dialect class that you want to use for a particular database, so that Hibernate knows that it understands the database so that it can automatically generate suitable SQL statements. The one you are specifying is correct for the MySQL database.


However, it looks like you have problems with this. Perhaps you did not install the MySQL JDBC driver? Do you get a ClassNotFoundException on it? Just downloading Connector / J , extracting the zip and placing the JAR file in the runtime class path should be sufficient.

+1
source share

regarding the definition of maven mysql, seems to work here.

 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.12</version> </dependency> 
+1
source share

All Articles