连接池问题,高手们快帮我看看,谢谢!(tomcat5.5.23)

cimybb 2008-05-23 12:08:01

javax.servlet.ServletException: Name jdbc is not bound in this Context
上传到空间后,没有办法修改tmcat_home下的server.xml文件,所以在我自己目录下增加META-INF/context.xml文件,和WEB-INF/web.xml来配置。
Context.xml
<?xml version="1.0" encoding="GBK"?>

<!--Context path="/" docBase="" debug="5" reloadable="true" crossContext="true"-->
<Context>
<Resource
auth="Container"
description="test"
name="jdbc/connectDB"
type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxIdle="2"
maxWait="5000"
username="sa"
password="pwd"
url="jdbc:microsoft:sqlserver://(主机IP):1433;DatabaseName=db"
maxActive="4"/>
</Context>

Web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

<description>
Welcome to Tomcat
</description>
<resource-ref>
<description>connectDB</description>
<res-ref-name>jdbc/connectDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>

</resource-ref>
<servlet>
<servlet-name>regservlet</servlet-name>
<display-name>regservlet</display-name>
<description>regservlet</description>
<servlet-class>cn.regservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>regservlet</servlet-name>
<url-pattern>/servlet/regservlet</url-pattern>
</servlet-mapping>
</web-app>

测试JSP文件
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.naming.*"%>
<html >
<body >
<%
Context initctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)initctx.lookup("java:comp/env/jdbc/connectDB");
Connection conn = ds.getConnection();
Statement stmt=conn.createStatement();
String sql="select * from AdminInfo";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
out.print("数据库操作成功,恭喜你");
<%}
rs.close();
stmt.close();
conn.close();
%>
</body >
</html >
运行JSP文件,出错。
...全文
85 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lblxiaoyu 2008-05-23
  • 打赏
  • 举报
回复
困了 就不看你什么错了 建议用proxool baidu一下这个,开源的连接池,很方便
cimybb 2008-05-23
  • 打赏
  • 举报
回复
我把一楼的重贴一遍,这样方便看,为帮我的好心人省点时间和精力。javax.servlet.ServletException: Name jdbc is not bound in this Context
上传到空间后,没有办法修改tmcat_home下的server.xml文件,所以在我自己目录下增加META-INF/context.xml文件,和WEB-INF/web.xml来配置。
Context.xml[color=#FF00FF][/color]
<?xml version="1.0" encoding="GBK"?>

<!--Context path="/" docBase="" debug="5" reloadable="true" crossContext="true"-->
<Context>
<Resource
auth="Container"
description="test"
name="jdbc/connectDB"
type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxIdle="2"
maxWait="5000"
username="sa"
password="pwd"
url="jdbc:microsoft:sqlserver://(主机IP):1433;DatabaseName=db"
maxActive="4"/>
</Context>

Web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

<description>
Welcome to Tomcat
</description>
<resource-ref>
<description>connectDB</description>
<res-ref-name>jdbc/connectDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>

</resource-ref>

<servlet>
<servlet-name>regservlet</servlet-name>
<display-name>regservlet</display-name>
<description>regservlet</description>
<servlet-class>cn.regservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>regservlet</servlet-name>
<url-pattern>/servlet/regservlet</url-pattern>
</servlet-mapping>
</web-app>

测试JSP文件
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.naming.*"%>
<html >
<body >
<%
Context initctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)initctx.lookup("java:comp/env/jdbc/connectDB");
Connection conn = ds.getConnection();

Statement stmt=conn.createStatement();
String sql="select * from AdminInfo";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
out.print("数据库操作成功,恭喜你");
<%}
rs.close();
stmt.close();
conn.close();
%>
</body >
</html >
运行JSP文件,出错。
cimybb 2008-05-23
  • 打赏
  • 举报
回复
错误信息
exception
org.apache.jasper.JasperException: Exception in JSP: /2.jsp:21

18: //Object obj = (Object)ctx.lookup("jdbc/connectDB");
19: //out.print(obj);
20: //DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/connectDB");
21: javax.sql.DataSource ds = (javax.sql.DataSource)initctx.lookup("java:comp/env/jdbc/connectDB");
22: //out.print(ds);
23: Connection conn = ds.getConnection();
24: //out.print(conn);


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:467)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:121)
root cause
javax.servlet.ServletException: Name jdbc is not bound in this Context
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:846)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:779)
org.apache.jsp._2_jsp._jspService(_2_jsp.java:116)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:121)
root cause
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
org.apache.naming.NamingContext.lookup(NamingContext.java:770)
org.apache.naming.NamingContext.lookup(NamingContext.java:140)
org.apache.naming.NamingContext.lookup(NamingContext.java:781)
org.apache.naming.NamingContext.lookup(NamingContext.java:140)
org.apache.naming.NamingContext.lookup(NamingContext.java:781)
org.apache.naming.NamingContext.lookup(NamingContext.java:153)
org.apache.naming.SelectorContext.lookup(SelectorContext.java:137)
javax.naming.InitialContext.lookup(Unknown Source)
org.apache.jsp._2_jsp._jspService(_2_jsp.java:64)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:121)
Sendler_Lee 2008-05-23
  • 打赏
  • 举报
回复
没有进行数据库连接
在<context></context>中添加
<ResourceLink global="数据源名称" name="映射后的名称" type="javax.sql.DataSource"/>,

<ResourceLink global="jdbc/connectDB" name="jdbc/connectDB" type="javax.sql.DataSource"/>,
M_song 2008-05-23
  • 打赏
  • 举报
回复
你再看一篇文章:
http://www.knowsky.com/344253.html
M_song 2008-05-23
  • 打赏
  • 举报
回复
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

JDBC没有绑定到上下文!

一般是因为配置有问题!

web工程下的,WEB-INF下的web.xml添加这一段:

<resource-ref>
<description>DB Connection</description>
<res-ref-name>JDBC/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

相对应的jsp或者java代码应该为:

DataSource ds = (DataSource) ctx.lookup("java:comp/env/JDBC/TestDB");

或者

Context envCtx = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup("JDBC/TestDB");

不配置,或者配置错误,就会出现:Name jdbc is not bound in this Context,如果配置成 <res-ref-name>JDBC/Test222DB</res-ref-name>,则会报Name TestDB is not bound in this Context。

注意:<res-ref-name>JDBC/TestDB</res-ref-name> 该配置是大小写敏感的。大小写一定要完全匹配

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧