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

From: Manohar Bhattarai <manoharbhattarai(at)gmail(dot)com>
To: philfrei(at)aol(dot)com
Cc: craig(at)postnewspapers(dot)com(dot)au, pgsql-jdbc(at)postgresql(dot)org
Subject: Re: jdbc works in java app, fails in servlet: "no suitable jdbc found"
Date: 2010-06-03 04:15:51
Message-ID: AANLkTinG_zY7VsxYk9ESo0OZ3iYTpVt6NfuZVQh3_8fZ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

On Thu, Jun 3, 2010 at 5:58 AM, <philfrei(at)aol(dot)com> wrote:

> Thank you for the reply!
>
> You need to preload the PostgreSQL driver...
>>
>
> Being new to this, I find myself second-guessing words as simple as
> "preload". The servlet "NearbyTest" has a constructor and a single method
> "doGet" which is invoked by an HTML page. As the first working statement in
> the "doGet" method (at this point everything else is commented out), I tried
> using the recommended statements. Both of them elicited the same error
> message:
>
> try {
> // Class.forName("org.postgresql.Driver");
> this.getClass().getClassLoader().loadClass("org.postgresql.Driver");
> } catch (ClassNotFoundException e1) {
> e1.printStackTrace();
> }
>
> java.lang.ClassNotFoundException: org.postgresql.Driver
> at
> org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.
> java:1516)
> at
> org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.
> java:1361)
> at nearbyGPS.NearByTest.doGet(NearByTest.java:59)
> ...
>
> ---where nearbyGPS is the package and NearByTest is the servlet class.
>
> As an experiment I tried importing the Driver class directly, e.g.:
>
> import org.postgresql.*;
> --and the form--
> import org.postgresql.Driver;
>
> These import statements both compile without complaint. But the same error
> occurs. I'm afraid that there must be something rather fundamental that I am
> missing. Is there another stage (prior to the servlet's "doGet") at which
> the "preloading" should occur?
>
> I just tried placing the recommended connection code in the constructor,
> and get exactly the same error message and stack trace.
>
> Eclipse has the option of right-clicking the project and selecting
> "Configure Build Path". On this project, the PostgreSQL JDBC driver is
> listed alongside the JRE and Apache libraries.
>
> The stack trace indicates code that is part of Apache. Is there possibly
> something that has to be configured in Apache that is missing?
>
> Many, many thanks in advance.
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++
>
>
> On 02/06/10 12:56, 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
>
>>
>> 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.
>>
>> Is a different driver needed in the "Servlet" context?
>>>
>>
>> No, but you might need to load it differently if your container doesn't
>> use the system classloader.
>>
>> You can use the classloader that loaded the currently running object
>>
> with:
>
>>
>> this.getClass().getClassLoader().loadClass('org.postgresql.Driver');
>>
>> --
>> Craig Ringer
>>
>> Tech-related writing: http://soapyfrogs.blogspot.com/
>>
>>
> --
> Sent via pgsql-jdbc mailing list (pgsql-jdbc(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-jdbc
>

I had the same problem of getting exception of class not found as you are
getting. I have solved the problem by simply putting the jar file in
CATALINA_HOME/lib/ directory. Then i restarted Eclipse.
And wow the driver was found. This was solved just yesterday. See if this
helps you.

BTW, now I am getting new exception of"password authentication". Just trying
to solve this.

--
Regards,
Manohar Bhattarai (मनोहर भट्टराई)
Blogs:
http://manoharbhattarai.wordpress.com/
http://manoharbhattarai.posterous.com/
http://manoharbhattarai.blogspot.com/
Microblogs:
Twitter :- http://twitter.com/manoharmailme
Identi.ca :- http://identi.ca/manoharbhattarai

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Craig Ringer 2010-06-03 05:50:10 Re: JDBC SSL with postgresql
Previous Message Jeffrey Baker 2010-06-03 04:08:52 Re: JDBC SSL with postgresql