臭名昭著的java.sql.SQLException:找不到合适的驱动程序 [英] The infamous java.sql.SQLException: No suitable driver found

查看:295
本文介绍了臭名昭著的java.sql.SQLException:找不到合适的驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将启用数据库的JSP添加到现有的Tomcat 5.5应用程序(如果有帮助,则为GeoServer 2.0.0).

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

该应用程序本身与Postgres可以很好地通信,因此我知道数据库已启动,用户可以访问它,所有这些好东西.我正在尝试做的是添加的JSP中的数据库查询.我已经在 Tomcat数据源示例中使用了配置示例几乎是开箱即用的.必需的标记库位于正确的位置-如果我只有标记库引用,则不会发生任何错误,因此它将查找这些JAR. Postgres jdbc驱动程序postgresql-8.4.701.jdbc3.jar位于$ CATALINA_HOME/common/lib中.

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.

这是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>

<Host>内的$ CATALINA_HOME/conf/server.xml中的相关部分,又在<Engine>内:

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>

这些行是webapps/gs2/WEB-INF/web.xml中标记的最后一行:

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>

最后,例外:

   exception
    org.apache.jasper.JasperException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver"
    [...wads of ensuing goo elided]

推荐答案

臭名昭著的java.sql.SQLException:未找到合适的驱动程序

此异常基本上可以有两个原因:

This exception can have basically two causes:

您需要确保将JDBC驱动程序放置在服务器自己的/lib文件夹中.

You need to ensure that the JDBC driver is placed in server's own /lib folder.

或者,当您实际上不使用服务器管理的连接池数据源,而是在WAR中手动摆弄DriverManager#getConnection()时,则需要将JDBC驱动程序放在WAR的/WEB-INF/lib中并执行..

Or, when you're actually not using a server-managed connection pool data source, but are manually fiddling around with DriverManager#getConnection() in WAR, then you need to place the JDBC driver in WAR's /WEB-INF/lib and perform ..

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

..在您的代码之前第一次DriverManager#getConnection()调用中,从而确保您吞咽/忽略任何可能被其抛出的ClassNotFoundException并继续执行代码流,就好像没有发生异常情况一样.另请参见我在哪里放置用于Tomcat连接池的JDBC驱动程序?

.. in your code before the first DriverManager#getConnection() call whereby you make sure that you do not swallow/ignore any ClassNotFoundException which can be thrown by it and continue the code flow as if nothing exceptional happened. See also Where do I have to place the JDBC driver for Tomcat's connection pool?

您需要确保JDBC URL符合JDBC驱动程序文档,并记住它通常区分大小写.当JDBC URL对

You need to ensure that the JDBC URL is conform the JDBC driver documentation and keep in mind that it's usually case sensitive. When the JDBC URL does not return true for Driver#acceptsURL() for any of the loaded drivers, then you will also get exactly this exception.

PostgreSQL 的情况下,此处.

对于JDBC,数据库由URL(统一资源定位符)表示.对于PostgreSQL™,它采用以下形式之一:

With JDBC, a database is represented by a URL (Uniform Resource Locator). With PostgreSQL™, this takes one of the following forms:

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

对于 MySQL ,它已记录在

用于连接到MySQL服务器的JDBC URL的一般格式如下,方括号([ ])中的项目是可选的:

The general format for a JDBC URL for connecting to a MySQL server is as follows, with items in square brackets ([ ]) being optional:

jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]] » [?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]

jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]] » [?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]

如果是 Oracle ,则在此处中进行记录.. >

In case of Oracle it is documented here.

URL语法有2种,旧语法仅适用于SID,而新语法具有Oracle服务名称.

There are 2 URL syntax, old syntax which will only work with SID and the new one with Oracle service name.

旧语法jdbc:oracle:thin:@[HOST][:PORT]:SID

新语法jdbc:oracle:thin:@//[HOST][:PORT]/SERVICE


另请参见:

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆