臭名昭著的 java.sql.SQLException:找不到合适的驱动程序 [英] The infamous java.sql.SQLException: No suitable driver found
问题描述
我正在尝试将启用数据库的 JSP 添加到现有的 Tomcat 5.5 应用程序(GeoServer 2.0.0,如果有帮助).
应用程序本身与 Postgres 的对话很好,所以我知道数据库已启动,用户可以访问它,所有这些好东西.我想要做的是在我添加的 JSP 中进行数据库查询.我在
#2.或者,JDBC URL 的语法错误
您需要确保 JDBC URL 符合 JDBC 驱动程序文档,并记住它通常区分大小写.当 JDBC URL 没有为 Driver#acceptsURL()
对于任何加载的驱动程序,那么您也会得到这个异常.
如果是 PostgreSQL,则记录在 here一>.
<块引用>在 JDBC 中,数据库由 URL(统一资源定位符)表示.对于 PostgreSQL™,这采用以下形式之一:
<块引用>
jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database
如果是 MySQL,则记录在 这里.
<块引用>用于连接 MySQL 服务器的 JDBC URL 的一般格式如下,方括号 ([ ]
) 中的项目是可选的:
<块引用>
jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]] » [?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]
如果是 Oracle,则在此处进行了记录.
<块引用>有两种 URL 语法,旧语法仅适用于 SID,新语法适用于 Oracle 服务名称.
<块引用>
旧语法jdbc:oracle:thin:@[HOST][:PORT]:SID
<块引用>
新语法jdbc:oracle:thin:@//[HOST][:PORT]/SERVICE
###另见:
- 我在哪里必须要为Tomcat的连接池放置JDBC驱动吗?
- 如何在Eclipse Web项目中安装JDBC驱动程序而无需面对java.lang.ClassNotFoundException
- 如何我应该在基于 servlet 的应用程序中连接到 JDBC 数据库/数据源吗?
- 什么是Class.forName()"之间的区别和Class.forName().newInstance()"?
- 将 Java 连接到 MySQL 数据库
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]
The infamous java.sql.SQLException: No suitable driver found
This exception can have basically two causes:
#1. JDBC driver is not loaded
You need to ensure that the JDBC driver is placed in server's own /lib
folder.
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");
.. 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?
#2. Or, JDBC URL is in wrong syntax
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.
In case of PostgreSQL it is documented here.
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
In case of MySQL it is documented here.
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]...]
In case of Oracle it is documented here.
There are 2 URL syntax, old syntax which will only work with SID and the new one with Oracle service name.
Old syntax
jdbc:oracle:thin:@[HOST][:PORT]:SID
New syntax
jdbc:oracle:thin:@//[HOST][:PORT]/SERVICE
###See also:
- Where do I have to place the JDBC driver for Tomcat's connection pool?
- How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception
- How should I connect to JDBC database / datasource in a servlet based application?
- What is the difference between "Class.forName()" and "Class.forName().newInstance()"?
- Connect Java to a MySQL database
这篇关于臭名昭著的 java.sql.SQLException:找不到合适的驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!