• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A JDBC Problem That Shouldn't Be

 
Casey Kemp
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm connecting to a PostgreSQL version 9.3 database. I'm running Java jdk1.7.0_45, have the PostgreSQL jars (postgresql-9.3-1102.jdbc4.jar , postgresql-9.3-1102.jdbc41.jar) in my classpath (verified it). By all rights, I should connect to the database, but I'm not and need help to figure out what I'm doing wrong. I'm on an old Windows XP at SP3, and don't think that's the problem.

The error message is: java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/testDBMS

Here's the code.

Help!!!


 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code looks alright.

Looks like it's a conflict JVM doesn't know which jar file to load.

Since you are using Java 7, the jdbc jar postgresql-9.3-1102.jdbc41.jar is needed. The other one (is for Java 6) can be removed from the classpath.
 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How have you verified that the correct driver jar (not more than one) is in the classpath? Which command are you executing to run this code?
 
Casey Kemp
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The resolution is baffling, but I'm not one to look a gift horse in the mouth. First, I may have overlooked inserting the following code: Class.forName("org.postgresql.Driver"); But the syntax for a successful compile is one I'm not familiar with; it is java -cp .;"C:\Program Files\PostgreSQL\9.3\lib\postgresql-9.3-1102.jdbc4.jar" JdbcPostgresqlConnection. Note 2 things, first, the jar folder declaration is in double quotes and 2, the jar folders are in a folder with a space in the name which is the reason for the double quotes. Having moved the PostgreSQL jar to the folder with the java program folder, it's not necessary to use the double quotes, for example, java -cp .;C:\AZ_Fantasy5\postgresql-9.3-1102.jdbc4.jar JdbcPostgresqlConnection works perfectly. And too, you can't use the CLASSPATH environment; you must set it in the compile command, java -cp .;C:\AZ_Fantasy5\postgresql-9.3-1102.jdbc4.jar JdbcPostgresqlConnection
 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Class.forName(...) is only needed if you are using JDBC3 drivers (eg Java 5 and before). Yet having such line doesn't hurt and probably provide backward compatibility by simply replacing the jar file on a older JVM machine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic