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
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?
posted 5 years ago
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
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.
K. Tsang CEng MBCS PMP PMI-ACP OCPJEA OCPJP
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding: