Tomcat 配置JNDI数据源问题
我自JSP页面里通过JNDI读取tomcat的服务器配置,但是运行报错如下:
HTTP Status 500 - An exception occurred processing JSP page /First.jsp at line 44
type Exception report
message An exception occurred processing JSP page /First.jsp at line 44
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /First.jsp at line 44
41: Context ctx = new InitialContext();
42: DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/testdb");
43: if (ds == null) return;
44: Connection conn = ds.getConnection();
45: Statement stmt = conn.createStatement();
46: ResultSet rs = stmt.executeQuery("select * from news_inf");
47: while (rs.next()) {
我的tomcat服务器conf目录下context.xml配置如下:
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Resource
name="jdbc/testdb"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javaee"
username="root"
password="123456"
maxActive="5"
maxIdle="2"
maxWait="10000"/>
</Context>
项目中WEB-INF目录下web.xml配置如下:
<?xml version="1.0" encoding="gbk"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>MySQLJSP</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>MySQL DataSource</description>
<res-ref-name>jdbc/testdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
仔细看了也不知道哪里写错了,请高手指点!
[解决办法]
1.server.xml 仔细核对 resoures 标签 的各属性 书否配错,
2.查看你版本 Tomcat 5.5 版本以上 是不是 。
3.运行结果要页面得到,不要到java程序得到
[解决办法]
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
你JDBC的 dirver class 和 URL 怎么都是空啊~?