Re: Can't connect to posgresql through the jdbc driver - Mailing list pgsql-jdbc

From Craig Ringer
Subject Re: Can't connect to posgresql through the jdbc driver
Date
Msg-id 4B8DB392.2020608@postnewspapers.com.au
Whole thread Raw
In response to Can't connect to posgresql through the jdbc driver  (Lior K <liork20002000@yahoo.com>)
List pgsql-jdbc
Lior K wrote:
> Hi
> I'm using postgresql 8.3 and the postgresql-8.4-701.jdbc4.jar ( a jar file which is supposed to include the jdbc
driver:org.postgresql.Driver)  

It certainly should! If yours doesn't - where did you download it from?

Please post the output of:

   unzip -l postgresql-8.4-701.jdbc.jar | grep Driver

You should see these lines among others:

    14725  2009-06-30 22:30   org/postgresql/Driver.class
       22  2007-07-18 20:23   META-INF/services/java.sql.Driver

> I set the CLASSPATH environment variable to the correct place and ran my JAVA code which first tried to call the jdbc
driver: "Class.forName("org.postgresql.Driver");" 

What's the exact value of CLASSPATH as you set it?

Did you set it to the directory containing the .jar ? Or to the .jar its
self? You need to put the actual .jar on the CLASSPATH.

> But to no avail ("java.lang.ClassNotFoundException:
> org.postgresql.Driver") . At first I thought I set the CLASSPATH wrong
> (did it thorugh my .bashrc and exported) but after trying a few
> variations of CLASSPATH I opened the file postgresql-8.4-701.jdbc4.jar
> with the ZIP tool that came with UBUNTU.
> I noticed that the jdbc driver postgresql-8.4-701.jdbc4.jar was not in that bundled jar file...

Of course not. postgresql-8.4-701.jdbc4.jar is the jar file containing
the Driver class and all the other classes required for it to work. It
can't contain its self.

> I did notice that there was another driver in there called "java.sql.Driver"
> I put it in my code (just to check the CLASSPATH) : "Class.forName("java.sql.Driver");"
> compiled and ran .

That's because you're loading the JDBC driver interface
(java.sql.Driver), not an actual driver. "Class.forName" is just a call
to the classloader asking it to load a particular class from a file.
It's not magic, and it doesn't know what you want, only what you ask for.

> This time it passed that line correctly

Well, that statement ran without error, but it didn't load the
PostgreSQL JDBC driver because that's not what you asked for.

> (it did get stuck on the actual line that operated  the JDBC URL, but at least it showed me that my CLASSPATH was
right

Bad assumption. It did nothing of the sort.

  Class.forName("java.sql.Driver");

will run without error on a stock-standard J2SE install with no added
JDBC drivers.

I still think your classpath is probably wrong.

( On a side note: Maybe you should look into using an IDE like NetBeans?
It makes this sort of thing easier for newbies to Java. The Maven
plugin, if you use that, even takes care of updating your dependencies
and their dependencies for you automatically. )

--
Craig Ringer

pgsql-jdbc by date:

Previous
From: Lior K
Date:
Subject: Can't connect to posgresql through the jdbc driver
Next
From: Craig Ringer
Date:
Subject: Re: Can't connect to posgresql through the jdbc driver