"No suitable driver" problem with Hibernate3, PostgreSQL 8.3 and Java 5 - java

Does anybody know what's going on here:
I run hibernate 3.2.6 against a PostgreSQL 8.3 (installed via fink) database on my Mac OS X. The setup works fine when I use Java 6 and the JDBC 4 driver (postgresql-8.3-603.jdbc4). However, I need this stuff to work with Java 5 and (hence) JDBC 3 (postgresql-8.3-603.jdbc3). When I change the jar in the classpath and switch to Java 5 (I do this in eclipse), I get the following error:
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
<Rows clipped for readability>
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:545)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:423)
What's the problem here? I cannot see it. Here is my hibernate configuration:
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:postgresql:test</property>
<property name="connection.username">postgres</property>
<property name="connection.password">p</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<mapping resource="com/mydomain/MyClass.hbm.xml"/>
</session-factory>
</hibernate-configuration>
EDIT: The longer, more usual form of the connection URL: jdbc:postgresql://localhost/test has the exact same behaviour.
The driver jar is definitely in the classpath, and I also do not manage to get any errors with this direct JDBC test code:
public static void main(String[] args) throws Exception {
Class.forName("org.postgresql.Driver");
Connection con=DriverManager.getConnection("jdbc:postgresql://localhost/test","postgres", "p");
}

I don't see you specifying the driver class in your Hibernate configuration. Try adding the following:
<hibernate-configuration>
<session-factory>
.
.
<property name="connection.driver_class">org.postgresql.Driver</property>
.
</session-factory>
</hibernate-configuration>

did you notice that the connection url is incomplete
<property name="connection.url">jdbc:postgresql:test</property>
as opposed to
<property name="connection.url">jdbc:postgresql://localhost/test</property>

You say "work with Java 5 and (hence) JDBC 3 (postgresql-8.3-603.jdbc3)." Maybe this is a faulty assumption.
The download page is confusing to me. It seems to imply that for Java 1.5 you need JDBC3 but it's not 100% clear. I'm not sure why the JDBC4 driver won't work with Java 1.5 (we use a DB2 JDBC4 driver with Java 1.5).
Have you tried the JDBC4 driver with Java 1.5?

One of the new JDBC4 features is automatic loading via the Service Provider mechanism. By including a META-INF/services/java.sql.Driver file in the jar file, there is no longer the need to do Class.forName(""). This only works with the 1.6 JVM.

I had the same problem "no suiteble driver found" using a servlet the solution to register a driver is:
Class driverClass = Class.forName("org.postgresql.Driver");
DriverManager.registerDriver((Driver) driverClass.newInstance());
Found the solution here:
http://www.java2s.com/Tutorial/Java/0340__Database/DriverManagergetDriversenumeratealltheloadedJDBCdrivers.htm
http://codingexplorer.wordpress.com/2009/09/06/%E2%80%9Cno-suitable-driver%E2%80%9D-for-postgresql/

Related

MySQL table not create . Why this error come in run spring boot project? [duplicate]

I am getting this error for my Java code
Caused by :`com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException`: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB` server version for the right syntax to use near 'type = `MyISAM`' at line 1
This is the query passed by hibernate:
Hibernate: create table EMPLOYEE (emp_id integer not null, FNAME varchar(255), LNAME varchar(255), primary key (emp_id)) type=MyISAM
I have looked at all questions related to this error. But in all that questions the user itself is passing query "type = MyISAM" so they can change "type" to "engine", but here hibernate is responsible for creating table, so I don't understand where the mistake is, and how I can fix it.
This is my configuration file:
<hibernate-configuration>
<session-factory >
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/servletcheck</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"> </property>
<property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
This is my mapping file:
<hibernate-mapping>
<class name="first.Employee" table="EMPLOYEE">
<id name="id" type="int">
<column name="emp_id" />
<generator class="assigned" />
</id>
<property name="fname" type="java.lang.String">
<column name="FNAME" />
</property>
<property name="lname" type="java.lang.String">
<column name="LNAME" />
</property>
</class>
</hibernate-mapping>
This is my class file:
public class StoreData {
public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory=cfg.buildSessionFactory();
Session session=factory.openSession();
org.hibernate.Transaction t=session.beginTransaction();
Employee e=new Employee();
e.setId(1);
e.setFname("yogesh");
e.setLname("Meghnani");
session.persist(e);
t.commit();
}
}
The problem is that - in Hibernate 5.x and earlier - the dialect org.hibernate.dialect.MySQLDialect is for MySQL 4.x or earlier. The fragment TYPE=MYISAM that is generated by this dialect was deprecated in MySQL 4.0 and removed in 5.5.
Given that you use MariaDB, you need to use (depending on the version of MariaDB and - maybe - the version of Hibernate) one of:
org.hibernate.dialect.MariaDBDialect
org.hibernate.dialect.MariaDB53Dialect
or higher versions (e.g. org.hibernate.dialect.MariaDB106Dialect)
If you are using MySQL, or if the above two dialects for MariaDB don't exist in your version of Hibernate:
org.hibernate.dialect.MySQL5Dialect
org.hibernate.dialect.MySQL55Dialect
org.hibernate.dialect.MySQL57Dialect
org.hibernate.dialect.MySQL8Dialect
or variants of these dialects (e.g. org.hibernate.dialect.MySQL57InnoDBDialect)
NOTE: With Hibernate 6, you should once again use MySQLDialect or MariaDBDialect, as Hibernate 6 dialects will configure themselves based on the actual connected version.
It's a Dialect related issue, instead of org.hibernate.dialect.MySQLDialect you can go with org.hibernate.dialect.MySQL5Dialect. It will work happily.
This error may arise with increasing frequency as more organizations move to install MySQL 8.x while their Hibernate code is still using the old version 5 Dialect.
The latest Hibernate 5.4 distribution as of 6/6/2020 includes the following MySQL dialects:
MySQL55Dialect
MySQL57Dialect
MySQL57InnoDBDialect
MySQL5Dialect
MySQL8Dialect
MySQLDialect
MySQLInnoDBDialect
MySQLISAMDialect
Current Hibernate 5.4 MySQL dialects
Here's the dialects in the core jar file filtered for MySQL. Use the right one and that type=MyISAM MySQL exception will be a thing of the past.
Before MySQL 4.x.x version,TYPE MYISAM engine is used to store tables but in MySQL 5.x.x or later version MySQL is used ENGINE = MYISAM to store tables.
e.g
In MySQL 4.x.x or < 4.x.x
CREATE TABLE t (i INT) TYPE = MYISAM;
In MySQL 5.x.x or > 5.x.x
CREATE TABLE t (i INT) ENGINE = MYISAM;
Now lets talk about hibernate,
In hibernate you must use given below dialect
MySQL <= 4.x.x
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
MySQL>=5.x.x.
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
It will work fine.
I know this is a old post but I am sharing a different experience. I was also seeing the same exact exception but i had MySQL 8 with Springboot+ JPA+ Hibernate .
My dialect looks like
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
But I was still seeing the issue. After some troubleshooting I noticed that the issue was in the schema sql where I had the table creation script in all Caps and it was expected to have it in small. For example
CREATE TABLE item_catalog
instead of
CREATE TABLE ITEM_CATALOG
After changing the case in the schema sql to lower case table name, It worked fine.
MySQL dropped MyISAM as the default engine. Now it uses InnoDB. You can verify this using SequelPro by clicking on any table, and then looking in the bottom quadrant for the Table Information:
And at the top of your window to see the MySQL Version:
Using the above information, I know that I need the dialect:
org.hibernate.dialect.MySQL5InnoDBDialect
I can set the dialect in my application.properties:
It is possible to find the available dialects, no matter which database you need a dialect that works on your system.
> Project> Libraries> hibernate-core> org.hibernate.dialect
Then you will see a package of several "dialect" classes for different databases and you can test one that works. This case is for hibernate.

Cannot get JDBC Connection in Elastic Beanstalk

i want to deploy my application in AWS.
i have setup my env.
and in root-context.xml of my application i have setup data source.
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#mydbinstance.cnn31xputfwg.ap-northeast-2.rds.amazonaws.com:1521:orcl" />
<property name="username" value="myid" />
<property name="password" value="mypassword" />
</bean>
this setting works fine locally in tomcat server.
but in AWS, it throws this error message
nested exception is org.apache.ibatis.exceptions.PersistenceException:
Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is
java.sql.SQLException: Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver'
The error may exist in file [/var/lib/tomcat8/webapps/ROOT/WEB-INF/classes/mappers/emp/emp-mapper.xml]
The error may involve EmpDAO.List ### The error occurred while executing a query
Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is
java.sql.SQLException: Cannot load JDBC driver class 'oracle.jdbc.driver.OracleDriver'
Now my question is: what should i do to use get jdbc connection in AWS?
Looks like when you run the app on AWS it lacks Oracle JDBC driver in its CLASSPATH. So the answer is to add the driver to the classpath.
And I foresee the next question: "how to do this?", and simplest way is just to copy the driver to your classpath manually.
If you don't like manual work, you can generate Spring application template using Spring Initializr. Its build script has org.springframework.boot plugin, which does two important things:
builds single jar file which includes Tomcat, so the application can be started just by java -jar my-app.jar (avoiding deployment to tomcat)
includes all dependencies from build-script dependencies section to this jar file (including Oracle JDBC driver)

Invalid syntax error "type= MyISAM" in DDL generated by Hibernate

I am getting this error for my Java code
Caused by :`com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException`: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB` server version for the right syntax to use near 'type = `MyISAM`' at line 1
This is the query passed by hibernate:
Hibernate: create table EMPLOYEE (emp_id integer not null, FNAME varchar(255), LNAME varchar(255), primary key (emp_id)) type=MyISAM
I have looked at all questions related to this error. But in all that questions the user itself is passing query "type = MyISAM" so they can change "type" to "engine", but here hibernate is responsible for creating table, so I don't understand where the mistake is, and how I can fix it.
This is my configuration file:
<hibernate-configuration>
<session-factory >
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/servletcheck</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"> </property>
<property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
This is my mapping file:
<hibernate-mapping>
<class name="first.Employee" table="EMPLOYEE">
<id name="id" type="int">
<column name="emp_id" />
<generator class="assigned" />
</id>
<property name="fname" type="java.lang.String">
<column name="FNAME" />
</property>
<property name="lname" type="java.lang.String">
<column name="LNAME" />
</property>
</class>
</hibernate-mapping>
This is my class file:
public class StoreData {
public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory=cfg.buildSessionFactory();
Session session=factory.openSession();
org.hibernate.Transaction t=session.beginTransaction();
Employee e=new Employee();
e.setId(1);
e.setFname("yogesh");
e.setLname("Meghnani");
session.persist(e);
t.commit();
}
}
The problem is that - in Hibernate 5.x and earlier - the dialect org.hibernate.dialect.MySQLDialect is for MySQL 4.x or earlier. The fragment TYPE=MYISAM that is generated by this dialect was deprecated in MySQL 4.0 and removed in 5.5.
Given that you use MariaDB, you need to use (depending on the version of MariaDB and - maybe - the version of Hibernate) one of:
org.hibernate.dialect.MariaDBDialect
org.hibernate.dialect.MariaDB53Dialect
or higher versions (e.g. org.hibernate.dialect.MariaDB106Dialect)
If you are using MySQL, or if the above two dialects for MariaDB don't exist in your version of Hibernate:
org.hibernate.dialect.MySQL5Dialect
org.hibernate.dialect.MySQL55Dialect
org.hibernate.dialect.MySQL57Dialect
org.hibernate.dialect.MySQL8Dialect
or variants of these dialects (e.g. org.hibernate.dialect.MySQL57InnoDBDialect)
NOTE: With Hibernate 6, you should once again use MySQLDialect or MariaDBDialect, as Hibernate 6 dialects will configure themselves based on the actual connected version.
It's a Dialect related issue, instead of org.hibernate.dialect.MySQLDialect you can go with org.hibernate.dialect.MySQL5Dialect. It will work happily.
This error may arise with increasing frequency as more organizations move to install MySQL 8.x while their Hibernate code is still using the old version 5 Dialect.
The latest Hibernate 5.4 distribution as of 6/6/2020 includes the following MySQL dialects:
MySQL55Dialect
MySQL57Dialect
MySQL57InnoDBDialect
MySQL5Dialect
MySQL8Dialect
MySQLDialect
MySQLInnoDBDialect
MySQLISAMDialect
Current Hibernate 5.4 MySQL dialects
Here's the dialects in the core jar file filtered for MySQL. Use the right one and that type=MyISAM MySQL exception will be a thing of the past.
Before MySQL 4.x.x version,TYPE MYISAM engine is used to store tables but in MySQL 5.x.x or later version MySQL is used ENGINE = MYISAM to store tables.
e.g
In MySQL 4.x.x or < 4.x.x
CREATE TABLE t (i INT) TYPE = MYISAM;
In MySQL 5.x.x or > 5.x.x
CREATE TABLE t (i INT) ENGINE = MYISAM;
Now lets talk about hibernate,
In hibernate you must use given below dialect
MySQL <= 4.x.x
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
MySQL>=5.x.x.
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
It will work fine.
I know this is a old post but I am sharing a different experience. I was also seeing the same exact exception but i had MySQL 8 with Springboot+ JPA+ Hibernate .
My dialect looks like
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
But I was still seeing the issue. After some troubleshooting I noticed that the issue was in the schema sql where I had the table creation script in all Caps and it was expected to have it in small. For example
CREATE TABLE item_catalog
instead of
CREATE TABLE ITEM_CATALOG
After changing the case in the schema sql to lower case table name, It worked fine.
MySQL dropped MyISAM as the default engine. Now it uses InnoDB. You can verify this using SequelPro by clicking on any table, and then looking in the bottom quadrant for the Table Information:
And at the top of your window to see the MySQL Version:
Using the above information, I know that I need the dialect:
org.hibernate.dialect.MySQL5InnoDBDialect
I can set the dialect in my application.properties:
It is possible to find the available dialects, no matter which database you need a dialect that works on your system.
> Project> Libraries> hibernate-core> org.hibernate.dialect
Then you will see a package of several "dialect" classes for different databases and you can test one that works. This case is for hibernate.

Where the database should be with hibernate

I have a project that I didn't made which uses hibernate to connect to a database. I found the config file of hibernate in this project : hibernate.cfg.xml and it says :
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/persystddo</property>
<property name="connection.username">something</property>
<property name="connection.password">something</property>
I have an error when I try to run the project about hibernate. So I think that my database is not at the right place. So where should I put the database, in which folder ?
Thanks.
EDIT :
Exception : 500 - Transaction not successfully started
org.hibernate.TransactionException; transaction not successfully started
org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:183)
fr.inra.grignon.persyst.web.servlet.DoLogin.doPost(DoLogin.java:67
javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.Server.WsFilter.doFilter(WsFilter.java:52
jdbc:mysql://localhost:3306/persystddo
Please make sure that you have a database named "persystddo" exists in your mysql db. And the port is correct.
After that, also check you can login the database by username/password: something/something

Problems using Hibernate - JDBC Driver class not found: com.mysql.jdbc.Driver

I have a really strange issue when using hibernate to connect to a MySQLDB and add data.
This is the error I get:
JDBC Driver class not found:
com.mysql.jdbc.Driver
This is how my hibernate.cfg.xml looks like
<!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.url">jdbc:mysql://localhost:3306/fpa-webapp</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
</session-factory>
</hibernate-configuration>
I dont understand why I see a 500 Error when I navigate to the application; it says that the driver is not found.
HTTP ERROR 500
Problem accessing /fpa-webapp/.
Reason:
Exception constructing service 'ValueEncoderSource': Error invoking
service builder method
org.apache.tapestry5.services.TapestryModule.buildValueEncoderSource(Map,
InvalidationEventHub) (at
TapestryModule.java:2287) (for service
'ValueEncoderSource'): Error invoking
service contribution method
org.apache.tapestry5.hibernate.HibernateModule.contributeValueEncoderSource(MappedConfiguration,
boolean, HibernateSessionSource,
Session, TypeCoercer, PropertyAccess,
LoggerSource): Exception constructing
service 'HibernateSessionSource':
Error invoking service builder method
org.apache.tapestry5.hibernate.HibernateCoreModule.buildHibernateSessionSource(Logger,
List, RegistryShutdownHub) (at
HibernateCoreModule.java:123) (for
service 'HibernateSessionSource'):
JDBC Driver class not found:
com.mysql.jdbc.Driver
I'm sure the driver is in the class path.
What it could be?
Your driver is not on the classpath.
There are two ways to ensure it's on the classpath:
Add it to the global lib directory. For Tomcat this is TOMCAT_HOME/lib.
Include it in the war.
It depends on your requirements which you use.
If you're going to use Tomcat to manage the connection pool, you'll need to add it to the TOMCAT_HOME/lib and instead of defining your datasource directly in the hibernate configuration, you'll reference it via jndi.
The only plausible explanation is that the Driver class is not on the CLASSPATH.
Check to make sure that the mysql-connector-java (or other relevant) jar is indeed in a place where it will get loaded. If you're 100% positive that it is, it might help to provide more information about how you know the class is being loaded, so that we can identify other possible causes.

Categories