Re: No suitable driver found for jdbc:postgresql [error]

From: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
To: camilleri(dot)jon(at)gmail(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: No suitable driver found for jdbc:postgresql [error]
Date: 2011-07-13 00:33:56
Message-ID: 4E1CE7F4.8020003@postnewspapers.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 9/07/2011 11:56 PM, Jonathan Camilleri wrote:
>
> In order to enable JDK to connect to the library files, I copied over
> /postgresql-8.4-702.jdbc*3*.jar/, and,
> /postgresql-8.4-702.jdbc*4*.jar/ to /C:\Program Files\Java\jre6\lib\ext/.
>
Argh, don't do that! You're messing with *every* java program on the
system, some of which could have their own bundled copies of different
versions of the PostgreSQL JDBC drivers. Exciting and messy things can
happen.

Just add the JDBC driver to the classpath using the standard "java
-classpath" argument, CLASSPATH env var, or Classpath: jar manifest
entry. See the Java documentation.

When you bundle your app into a .war or .jar for production use, you
typically bundle the JDBC driver within the app jar. See the
documentation on the jar file format.

As for why your program doesn't run even when the driver is on the
classpath: you don't seem to be loading it. In a Java SE environment you
need to force the classloader to find and register a JDBC driver class
before the JDBC DriverManager can find it and use it to handle JDBC
connection URLs. That's usually done with a manual classloader call, like:


Thread.currentThread().getContextClassLoader().loadClass("org.postgresql.Driver");

See the PgJDBC documentation for more detail. Note that the docs use the
old-style static "Class.forName(...)" call, which is fine in simple J2SE
environments but unwise if you start building modular apps, using OSGi
or app servers, etc.

> It was assumed that the file naming indicates type 3 and type 4
> respectively

"Type 3" and "Type 4" sound like inventions of the author of your
textbook for the purposes of classifying and describing different
approaches to writing drivers - though I could be wrong, of course.

The "3" and "4" in PgJDBC refer to the version of the JDBC spec that
driver is for. JDBC4 drivers can only be used on newer JDKs, so a JDBC3
driver has to be available for people who use older JDKs.

> and, I was intending to use type 4, since it is more efficient to use
> a library that translates Java to the database language for Postgre:
>
"Postgres" or "PostgreSQL".

PgJDBC is a "type 4" driver according to that classification scheme,
whether you use the JDBC3 or JDBC4 version.

--
Craig Ringer

POST Newspapers
276 Onslow Rd, Shenton Park
Ph: 08 9381 3088 Fax: 08 9388 2258
ABN: 50 008 917 717
http://www.postnewspapers.com.au/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2011-07-13 00:40:14 Re: Weird problem that enormous locks
Previous Message Craig Ringer 2011-07-13 00:09:38 Re: Alter Columns with Triggers