Re: Not able to connect to postgresql database

From: Lew <noone(at)lewscanon(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Not able to connect to postgresql database
Date: 2010-06-01 23:38:32
Message-ID: hu45l7$d93$1@news.albasani.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

On 06/01/2010 04:27 PM, Kevin Grittner wrote:
Manohar Bhattarai wrote:
>
>> shows an exception that it cannot find driver.
>
>> Please help me and let me know what i am doing wrong and what i am
>> missing.
>
> The jar is not on your classpath, or you are not spelling the class
> name ("org.postgresql.Driver") correctly. I don't know of any other
> reason that the recommended class loading:
>
> Class.forName("org.postgresql.Driver");

Just as an aside, nowadays the recommended way from Sun is to load a driver is
through DataSource descriptors, which has its advantages but is a lot less
simple at first. With JPA you configure the data source in the
"persistence.xml" file for a local application, other XML files like
"context.xml" for app servers.

The 'Class.forName()' method is reliable but don't fall into the trap of
calling it more than once per program execution.

> would throw a ClassNotFoundException. (If that's not what you're
> doing, or that's not the statement on which it fails, or that's not
> your exception, you need to give us more clues.)

All techniques require proper placement of the driver JAR file in your
classpath. What that classpath is depends utterly on how you package and
deploy your app.

There's clear documentation on java.sun.com about JDBC, and about how to
specify the classpath for the "java ... foo.foo" ("-classpath" option) and
"java -jar foo.jar" (manifest file "Class-path:" entry) commands. For the
various app servers you have to check the rules for the particular app server,
e.g.,
<http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html>

--
Lew

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message philfrei 2010-06-02 04:56:22 jdbc works in java app, fails in servlet: "no suitable jdbc found"
Previous Message Jason Tesser 2010-06-01 21:10:21 Re: Found a Bug in latest Driver (I THINK) and pg 8.4