Re: Another Problem Connecting to JDBC7.0

From: jlemcke(at)netspace(dot)net(dot)au
To: pgsql-jdbc(at)postgresql(dot)org
Cc: Matthew Hyatt <mhyatt(at)studiosite(dot)com>, nusa <maheswara(at)freeuk(dot)com>
Subject: Re: Another Problem Connecting to JDBC7.0
Date: 2001-04-22 23:53:08
Message-ID: 987983588.3ae36ee4ca84f@www.netspace.net.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc


These are the 3 basic steps to establishing a jdbc connection which work for me.

1. Installation of postgresql.jar
---------------------------------
If the postgresql.jar file is not accessible to the Java runtime environment you
will get a 'java.lang.ClassNotFoundException: org.postgresql.Driver' exception.
If the reported class name is not "org.postgresql.Driver" see step 2.

The easiest way to make the postgresql jdbc driver accessible to your
applications is to install it as a Java Extension. The only thing you need to do
is cp, mv or ln the postgresql.jar file into the Java extension directory. There
is no need to set CLASSPATH if you do this.
The Java extension directory is $JAVA_HOME/jre/lib/ext ($JAVA_HOME on my system
is /usr/java/jdk1.3).

This makes life REALLY easy; I don't set CLASSPATH at all in my environment!

You do need to be running Java2 (jre1.2 or jre1.3) and have write access to the
extension directory.

Check out Sun's doco at :
http://java.sun.com/docs/books/tutorial/ext/
and
http://java.sun.com/products/jdk/1.2/docs/guide/extensions/

Failing that you need to set the CLASSPATH environment variable. The syntax for
this depends on your shell; I use tcsh (great for command line, lousy for
scripting) and would use
setenv CLASSPATH ${CLASSPATH}:/usr/local/lib/postgresql.jar
(if I needed to ;-)

2. Loading the Driver
---------------------
If the postgresql.jar file is installed correctly then a call to
Class.forName("org.postgresql.Driver");
should succeed. If not you have probably misspelled the driver name.
This will result in a 'java.lang.ClassNotFoundException: ogr.potsgresql.Driver'
exception.
One common cause of this is that the driver class name was changed from
'postgresql.Driver' to 'org.postgresql.Driver' (for 7.0 I think, anyone??).
The use of outdated documentation is hazardous to your sanity!

3. Creating the Connection
--------------------------
Creating the actual Connection object requires a valid url, username and
password. The code usually looks something like this :

String url = "jdbc:postgresql://hostname:portnumber/dbname";
String username = "fred";
String password = "secret";
Connection con = DriverManager.getConnection(url, username, password);

You can leave out the ':portnumber/' part of the url if you are connecting on
the default port(5432), and you can leave out the '//hostname:portnumber/' if
you are connecting to the local host on the default port.

An incorrect url can result in quite a variety of exceptions, most of which are
reasonably self-explanatory - here's a few examples

"jdbc:BOGUS://hostname:portnumber/dbname" gives
java.sql.SQLException: No suitable driver

"jdbc:postgresql://BOGUS:portnumber/dbname" gives
The connection attempt failed because java.net.UnknownHostException: BOGUS

"jdbc:postgresql://hostname:9999/dbname" gives
Connection refused. Check that the hostname and port is correct, and that the
postmaster is running with the -i flag, which enables TCP/IP networking.

"jdbc:postgresql://hostname:portnumber/BOGUS" gives
java.sql.SQLException: FATAL 1: Database "BOGUS" does not exist in the system
catalog.

and interestingly
"BOGUS:postgresql://hostname:portnumber/dbname"
works fine on my 7.0.3 system(!!) but
":postgresql://hostname:portnumber/dbname" gives
java.sql.SQLException: No suitable driver

Using the wrong username gives
java.sql.SQLException: FATAL 1: SetUserId: user 'BOGUS' is not in 'pg_shadow'

I haven't actually set up a password on my databases but I'm sure that an
incorrect password would result in as obvious an exception message as the
examples above :-)

--------------------------------

Quoting Matthew Hyatt <mhyatt(at)studiosite(dot)com>:

> Read several threads in this group, especially "Problem connecting to
> JDBC7.0-1.2 driver" posted by BNV Raman and Brett McCoy. I've done most
> everything BNV Raman did, but I'm still not connecting.
> Background:
> + Red Hat 7.0.x
> + postgresql 7.0.3-2 (ran rpm updates using 10 rpm's directly from
> postgresql.org)
> + jdk1.3.0_02
> + j2sdkee1.2.1
> + postmaster -i (yep)
> + copied JDBC7.0-1.2.jar to postgresql.jar
> + set userconfig.sh to:
>
J2EE_CLASSPATH="/usr/lib/pgsql/postgresql.jar:/usr/java/j2sdkee1.2.1/lib/system/postgresql.jar"
> + set /etc/profile J2EE_CLASSPATH to the same as above. I point to two
> postgresql.jar files, because it sounds like Raman left them in
> /usr/lib/psql directory, and Sun documents said to copy them into the
> ...j2sdkee.../lib/system directory. So, I did both.
> + Did NOT set CLASSPATH, because Sun j2sdkee docs said to remove
> CLASSPATH references
> + pgsql runs query statements, I've made and populated several
> databases
> + java code compiles, jsp runs (tomcat)
> + Class.forName("org.postgresql.Driver")
> "No suitable driver... error"
> Since the CLASSPATH setting is one difference between Raman's success,
> and my failures, I tried pointing to the postgresql.jar using -classpath
> when compiling, still, "No suitable driver".
>
> Raman and McCoy made mention of "interfaces" dir. I cannot find
> interfaces. I ran all the rpm's:
> postgresql-7.0.3-2.i386.rpm
> postgresql-devel-7.0.3-2.i386.rpm
> postgresql-jdbc-7.0.3-2.i386.rpm
> postgresql-odbc-7.0.3-2.i386.rpm
> postgresql-perl-7.0.3-2.i386.rpm
> postgresql-python-7.0.3-2.i386.rpm
> postgresql-server-7.0.3-2.i386.rpm
> postgresql-tcl-7.0.3-2.i386.rpm
> postgresql-test-7.0.3-2.i386.rpm
> postgresql-tk-7.0.3-2.i386.rpm
>
> All of the above rpm's verify.
>
> What did I do wrong? Please advise, before I pull out ALL of my hair!
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>

-------------------------------------------------
This mail sent through IMP: www.netspace.net.au

-------------------------------------------------
This mail sent through IMP: www.netspace.net.au

Browse pgsql-jdbc by date

  From Date Subject
Next Message B.N.V. Raman 2001-04-23 10:17:55 RE: Another Problem Connecting to JDBC7.0
Previous Message Chris Czeyka 2001-04-22 08:28:31 Lost in unicode?