"Correct driver problem" with Hibernate3, PostgreSQL 8.3 and Java 5
Does anyone know what's going on here:
I am running hibernate 3.2.6 against PostgreSQL 8.3 database (installed via fink) on my Mac OS X. The setup works fine when I use Java 6 and 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 on 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 is the problem? I do not see it. Here is my hibernate config:
<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>
The EDIT: . The longer, more common form of the connection url: jdbc: postgresql: // localhost / test has the same behavior.
The driver box is definitely on the classpath and I am also unable to get any errors with this straight 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 do not see you specifying the driver class in your Hibernate config. Try adding the following:
<hibernate-configuration>
<session-factory>
.
.
<property name="connection.driver_class">org.postgresql.Driver</property>
.
</session-factory>
</hibernate-configuration>
Have you noticed that the connection url is incomplete
<property name="connection.url">jdbc:postgresql:test</property>
Unlike
<property name="connection.url">jdbc:postgresql://localhost/test</property>
You say, "Work with Java 5 and (therefore) JDBC 3 (postgresql-8.3-603.jdbc3)". Perhaps this is an erroneous assumption.
the download page is confusing me. It sounds like you need JDBC3 for Java 1.5, but it's not 100% clear. I'm not sure why the JDBC4 driver won't work with Java 1.5 (we are using the JDBC4 DB2 driver since Java 1.5).
Have you tried the JDBC4 driver with Java 1.5?
One of the new features in JDBC4 is automatic loading through the service provider mechanism. Including the META-INF / services / java.sql.Driver file in the jar file, you no longer need to do Class.forName (""). This only works with 1.6 JVM.
I had the same problem as a "driver not found" using a servlet, the solution for registering the driver is:
Class driverClass = Class.forName("org.postgresql.Driver");
DriverManager.registerDriver((Driver) driverClass.newInstance());
Found a 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/