From: Lew <noone@lewscanon.com>
To: pgsql-jdbc@postgresql.org
Subject: Re: jdbc works in java app, fails in servlet: "no suitable jdbc found"
Date: Wed, 02 Jun 2010 18:58:55 -0400
Message-ID: <hu6nna$crj$1@news.albasani.net> (raw)
In-Reply-To: <4C05F409.7060805@postnewspapers.com.au>
References: <8CCD02714A2791E-E2C-7A83@webmail-m042.sysops.aol.com>
<4C05F409.7060805@postnewspapers.com.au>
philfrei@aol.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@aol.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
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: noone@lewscanon.com
Subject: Re: jdbc works in java app, fails in servlet: "no suitable jdbc found"
In-Reply-To: <hu6nna$crj$1@news.albasani.net>
References: <8CCD02714A2791E-E2C-7A83@webmail-m042.sysops.aol.com> <4C05F409.7060805@postnewspapers.com.au> <hu6nna$crj$1@news.albasani.net>
* 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