From: Craig Ringer <craig@postnewspapers.com.au>
To: Lior K <liork20002000@yahoo.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Can't connect to posgresql through the jdbc driver
Date: Wed, 03 Mar 2010 08:55:46 +0800
Message-ID: <4B8DB392.2020608@postnewspapers.com.au> (raw)
In-Reply-To: <759461.26544.qm@web35603.mail.mud.yahoo.com>
References: <759461.26544.qm@web35603.mail.mud.yahoo.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: craig@postnewspapers.com.au, liork20002000@yahoo.com
Subject: Re: Can't connect to posgresql through the jdbc driver
In-Reply-To: <4B8DB392.2020608@postnewspapers.com.au>
References: <759461.26544.qm@web35603.mail.mud.yahoo.com> <4B8DB392.2020608@postnewspapers.com.au>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox