views:

825

answers:

2

Hey y'all,

I'm trying to add a database-enabled JSP to an existing Tomcat 5.5 application (GeoServer 2.0.0, if that helps).

The app itself talks to Postgres just fine, so I know that the database is up, user can access it, all that good stuff. What I'm trying to do is a database query in a JSP that I've added. I've used the config example in the Tomcat datasource example pretty much out of the box. The requisite taglibs are in the right place -- no errors occur if I just have the taglib refs, so it's finding those JARs. The postgres jdbc driver, postgresql-8.4.701.jdbc3.jar is in $CATALINA_HOME/common/lib.

Here's the top of the JSP:

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<sql:query var="rs" dataSource="jdbc/mmas">
  select current_validstart as ValidTime from runoff_forecast_valid_time
</sql:query>

The relevant section from $CATALINA_HOME/conf/server.xml, inside the <Host> which is in turn within <Engine>:

<Context path="/gs2" allowLinking="true">
  <Resource name="jdbc/mmas" type="javax.sql.Datasource"
      auth="Container" driverClassName="org.postgresql.Driver"
      maxActive="100" maxIdle="30" maxWait="10000"
      username="mmas" password="very_secure_yess_precious!"
      url="jdbc:postgresql//localhost:5432/mmas" />
</Context>

These lines are the last in the tag in webapps/gs2/WEB-INF/web.xml:

<resource-ref>
  <description>
     The database resource for the MMAS PostGIS database
  </description>
  <res-ref-name>
     jdbc/mmas
  </res-ref-name>
  <res-type>
     javax.sql.DataSource
  </res-type>
  <res-auth>
     Container
  </res-auth>
</resource-ref>

Finally, the exception:

   exception
    org.apache.jasper.JasperException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver"
    [...wads of ensuing goo elided]
+2  A: 
url="jdbc:postgresql//localhost:5432/mmas"

That URL looks wrong, do you need the following?

url="jdbc:postgresql://localhost:5432/mmas"
araqnid
+1  A: 

The infamous “No suitable Driver” exception

This exception can have basically two causes:

  1. The driver is not loaded.
  2. The JDBC URL didn't return true for Driver#acceptsURL() for any of the loaded drivers.

To fix 1, you need to ensure that you have a

Class.forName("com.example.jdbc.Driver");

in your code prior to DriverManager#getConnection() call and that you do not swallow/ignore any ClassNotFoundException which can be thrown by it.

To fix 2, you need to ensure that the JDBC URL syntax conforms the one as specified in the JDBC driver documentation. In case of PostgreSQL it is located here (uh, ugly site, but OK). Here's a quote:

In JDBC all url's begin with jdbc:protocol: This is the standard. After this is driver specific, and no two drivers are the same.

So our url? It's one of the following:

jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database

where database is the database to connect to, host the server to connect to, and port the port number.

If left out, host defaults to localhost (not 127.0.0.1 see applets!) and port to 5432 (configurable at build time).

Hope this helps you and all others who are experiencing the apparently "infamous" problem ;)

In your specific case, it is obviously the URL which is plain wrong. Refer the documentation for the right syntax (which is already pointed out in the answer of araqnid).

BalusC
Rick Wayne
Ah. Comments not so much w/the code formatting. Ahem. Is my newbie showing? (ZIIP!) Anyway, the Jakarta example was all about using the <sql:query> syntax instead of just embedding Java code, and I agree that's a much much cleaner way to do it. One should not need to explicitly call forName() and getConnection(), right?But the old ugly hack-the-driver-code-in-the-presentation-layer method worked just fine, first try. So I'm baffled, but now it's more of a maintenance/aesthetics issue than "This is broken, fix it or brush up on your burger-flipping skills."
Rick Wayne
IMHO using the sql taglib is only slightly better than doing JDBC in scriptlets... much prefer a controller/view pattern where the db stuff is done upfront and the JSP just displays stuff. Each to their own though, and using sql:query keeps things simple :) (ish)
araqnid
I have never said that I agree with using JSTL SQL taglib, but that's not the subject where this topic is about.
BalusC