This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

A JDBC Problem That Shouldn't Be

 
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!!!


 
Bartender
Posts: 3648
16
Mac OS X Firefox Browser 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: 3648
16
Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch.
 
Rancher
Posts: 43011
76
  • 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: 3648
16
Mac OS X Firefox Browser 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.
 
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!