Re: jdbc works in java app, fails in servlet: "no suitable jdbc found"

From: Lew <noone(at)lewscanon(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: jdbc works in java app, fails in servlet: "no suitable jdbc found"
Date: 2010-06-02 22:58:55
Message-ID: hu6nna$crj$1@news.albasani.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

philfrei(at)aol(dot)com wrote:
>> Hi - I am new to Postgresql (and Eclipse and Tomcat--triple threat),and
>> would have posted this question in the general section, but thought it
>> might be more appropriate here since it concerns the JDCB.
>>
>> I have successfully installed and run some example programs from Core
>> Java II by Horstmann. I wish to connect to a database from a Java
>> Servlet, and tried using the same code and properties as is being used
>> in a Java application program (which successfully connects to the same
>> db), but am getting the following error when executing the
>> DriverManager.getConnection(url, username, password) code:
>>
>> java.sql.SQLException: No suitable driver found for jdbc:postgresql:gpsdata

Craig Ringer wrote:
> The JRE's JDBC is complaining that it doesn't know what to do with URLs
> of type 'jdbc:postgresql'. This means that the PostgreSQL JDBC driver
> hasn't been loaded.
>
> You need to preload the PostgreSQL driver, org.postgresql.Driver , to
> register it with JDBC so that JDBC knows to use it for 'postgresql' URLs.
>
> Load the JDBC driver with with 'Class.forName' if it's on the System
> classpath, or (preferably) the classloader that loaded your servelet.

Better yet, do it Tomcat's way, configuring a 'DataSource' in the context.

philfrei(at)aol(dot)com wrote:
>> Is a different driver needed in the "Servlet" context?

Craig Ringer wrote:
> No, but you might need to load it differently if your container doesn't
> use the system classloader.

He said he was using Tomcat. Tomcat's classloader use is very well documented.
<http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html>

> You can use the classloader that loaded the currently running object with:
>
> this.getClass().getClassLoader().loadClass('org.postgresql.Driver');

The driver should be loaded in a static context, not an instance context.
Furthermore, that code as shown will not compile. Further furthermore, I see
nothing in the documentation for 'ClassLoader#loadClass()' that promises to
initialize the class, unlike 'Class.forName()'. If the class is not
initialized, the driver won't register with the 'DriverManager'. I think
you're selling a pig in a poke here.

Much better, follow the directions at:
<http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html>
and
<http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html>

I've used this technique a lot and it works beautifully. For one thing, it's
a wonder to have the connection parameters externalized to a deployment
descriptor when you're moving the application around from development server
to test server to production to different development server.

Go with the Tomcat way and forget the kludgey direct uses of Java reflection.

--
Lew

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message philfrei 2010-06-03 00:28:13 Re: jdbc works in java app, fails in servlet: "no suitable jdbc found"
Previous Message Manohar Bhattarai 2010-06-02 18:06:11 Re: Not able to connect to postgresql database