我正在尝试将支持数据库的 JSP 添加到现有的 Tomcat 5.5 应用程序(GeoServer 2.0.0,如果有帮助的话)。
该应用程序本身与 Postgres 通信正常,所以我知道数据库已启动,用户可以访问它,所有这些好东西。我想要做的是在我添加的 JSP 中进行数据库查询。我在 Tomcat datasource example 中使用了配置示例开箱即用。必要的 taglib 位于正确的位置——如果我只有 taglib refs 就不会发生错误,所以它正在寻找那些 JAR。 postgres jdbc 驱动程序 postgresql-8.4.701.jdbc3.jar 在 $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>
来自 $CATALINA_HOME/conf/server.xml 的相关部分,在 <Host>
中这又在 <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 中标记的最后一行:
<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]
最佳答案
The infamous java.sql.SQLException: No suitable driver found
这个异常基本上可以有两个原因:
1。未加载 JDBC 驱动程序
如果是 Tomcat,您需要确保 JDBC 驱动程序位于服务器自己的 /lib
文件夹中。
或者,当您实际上没有使用服务器管理的连接池数据源,而是在 WAR 中手动摆弄 DriverManager#getConnection()
时,您需要放置 JDBC 驱动程序在 WAR 的 /WEB-INF/lib
中执行 ..
Class.forName("com.example.jdbc.Driver");
.. 在您的代码中在第一个DriverManager#getConnection()
调用时确保您不吞下/忽略任何ClassNotFoundException
可以被它抛出并继续代码流,就好像没有异常发生一样。另见 Where do I have to place the JDBC driver for Tomcat's connection pool?
其他服务器有类似的方式放置 JAR 文件:
- GlassFish:将JAR文件放在
/glassfish/lib
- WildFly:将JAR文件放在
/standalone/deployments
2。或者,JDBC URL 语法错误
您需要确保 JDBC URL 符合 JDBC 驱动程序文档,并记住它通常区分大小写。当 JDBC URL 不为 Driver#acceptsURL()
返回 true
时对于任何已加载的驱动程序,您也会得到这个异常。
在 PostgreSQL 的情况下,记录了 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
在 MySQL 的情况下,它被记录为 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]...]
在 Oracle 的情况下,记录了 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
另见:
- 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
关于postgresql - 臭名昭著的 java.sql.SQLException : No suitable driver found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46376946/