当前位置: 代码迷 >> Java Web开发 >> 连接池有关问题,高手们快帮小弟我看看,多谢!(tomcat5.5.23)
  详细解决方案

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

热度:624   发布时间:2016-04-17 12:21:11.0
连接池问题,高手们快帮我看看,谢谢!(tomcat5.5.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
<?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文件,出错。


------解决方案--------------------
困了 就不看你什么错了 建议用proxool baidu一下这个,开源的连接池,很方便