tomcat6.0.32配置jndi数据源失败

在使用Tomcat 6.0.32配置JNDI数据源时遇到失败,通过检查META-INF/context.xml和WEB-INF/web.xml文件内容发现配置正确,但依旧无法连接数据库。分析错误日志发现,问题在于JDBC驱动类名未正确获取。经过排查,确认问题出现在数据库驱动类名解析上,最终定位到是JSP页面中使用的JDBC驱动类名与实际部署的驱动类名不一致导致的。解决方案是确保JSP页面中使用的驱动类名与实际部署的驱动类名保持一致。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tomcat6.0.32配置jndi数据源失败

META-INF/context.xml内容如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!--
 3 Licensed to the Apache Software Foundation (ASF) under one or more
 4 contributor license agreements. See the NOTICE file distributed with
 5 this work for additional information regarding copyright ownership.
 6 The ASF licenses this file to You under the Apache License, Version 2.0
 7 (the "License"); you may not use this file except in compliance with
 8 the License. You may obtain a copy of the License at
 9 
10 http://www.apache.org/licenses/LICENSE-2.0
11 
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 -->
18 <Context>
19 
20 <resource name="jdbc/ibatis_test" 
21 auth="Container" 
22 type="javax.sql.DataSource" 
23 maxActive="100"
24 maxIdle="30" 
25 maxWait="10000" 
26 username="root"
27 password="root"
28 driverClassName="com.mysql.jdbc.Driver" 
29 url="jdbc:mysql://localhost:3306/ibatis_test?autoReconnect=true"/>
30 
31 <resourceLink global="UserDatabase" name="UserDataBase" type="org.apache.catalina.UserDatabase"/>
32 
33 </Context>

WEB-INF/web.xml内容如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!--
 3  Licensed to the Apache Software Foundation (ASF) under one or more
 4   contributor license agreements.  See the NOTICE file distributed with
 5   this work for additional information regarding copyright ownership.
 6   The ASF licenses this file to You under the Apache License, Version 2.0
 7   (the "License"); you may not use this file except in compliance with
 8   the License.  You may obtain a copy of the License at
 9 
10       http://www.apache.org/licenses/LICENSE-2.0
11 
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17 -->
18 
19 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
20    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
22    version="2.5"> 
23 
24   <display-name>Tomcat Manager Application</display-name>
25   <description>
26     A scriptable management web application for the Tomcat Web Server;
27     Manager lets you view, load/unload/etc particular web applications.
28   </description>
29 
30   <resource-ref>
31       <description>ibatis_test DataSource</description>
32       <res-ref-name>jdbc/ibatis_test</res-ref-name>
33       <res-type>javax.sql.DataSource</res-type>
34       <res-auth>Container</res-auth>
35   </resource-ref>
36 
37 </web-app>

测试用jsp页面代码如下:

 1 <%@page contentType="text/html;charset=utf-8" language="java"%>
 2 <%@page import="javax.naming.*,java.sql.*,javax.sql.*,java.io.*"%>
 3 <html>
 4 <head>
 5 <title></title>
 6 </head>
 7 <body>
 8     <%
 9         Connection conn = null;
10         Statement stmt = null;
11         try {
12             Context context = new InitialContext();
13             DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/ibatis_test");
14             if (ds != null) {
15                 conn = ds.getConnection();
16                 stmt = conn.createStatement();
17                 stmt.executeUpdate("insert into t_user(name,sex) values('sss', 1)");
18                 out.println("insert successful.");
19             } else {
20                 out.println("DataSource is null.");
21             }
22         }finally {
23             try {
24                 if (stmt != null) {
25                     stmt.close();
26                 }
27                 if (conn != null) {
28                     conn.close();
29                 }
30             } catch(Exception e) {
31                 e.printStackTrace();
32             }
33         }
34         
35     %>
36 </body>
37 </html>

报错如下:

 1 exception
 2 
 3 org.apache.jasper.JasperException: javax.servlet.ServletException: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
 4     org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:500)
 5     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:410)
 6     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
 7     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
 8     javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 9 root cause
10 
11 javax.servlet.ServletException: org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
12     org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:865)
13     org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:794)
14     org.apache.jsp.jsp.testJNDI_jsp._jspService(testJNDI_jsp.java:101)
15     org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
16     javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
17     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
18     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
19     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
20     javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
21 root cause
22 
23 org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
24     org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
25     org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
26     org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
27     org.apache.jsp.jsp.testJNDI_jsp._jspService(testJNDI_jsp.java:72)
28     org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
29     javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
30     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
31     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
32     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
33     javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
34 root cause
35 
36 java.lang.NullPointerException
37     sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(Unknown Source)
38     sun.jdbc.odbc.JdbcOdbcDriver.knownURL(Unknown Source)
39     sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(Unknown Source)
40     java.sql.DriverManager.getDriver(Unknown Source)
41     org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
42     org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
43     org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
44     org.apache.jsp.jsp.testJNDI_jsp._jspService(testJNDI_jsp.java:72)
45     org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
46     javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
47     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
48     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
49     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
50     javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

网上看了很多资料都是这样配置可以的啊!不知道问题在哪里?纠结...

数据库用mysql,驱动包在tomcat的lib里和应用的WEB-INF/lib里都放了。

转载于:https://www.cnblogs.com/dingshuai/archive/2012/11/01/2748982.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值