<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page session="false" %>
<%@page contentType="text/html;charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Test of SQL Server connection pool</title>
</head>
<body>
<%
out.print("Start");
try{
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("zcwy");
javax.sql.DataSource ds = (javax.sql.DataSource)obj;
Connection conn = ds.getConnection();
out.print("SQL Server connection pool runs perfectly!");%>
<%
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
String sql="select * from wl_user"; 
//titles为SQLSERVER2000的默认数据库pubs中的默认表 
ResultSet rs=stmt.executeQuery(sql); 
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%> 
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%> 
<%out.print("数据库操作成功,恭喜你");%> 
<%rs.close(); 
stmt.close(); conn.close();
}
catch(Exception ex){
out.print(ex.getMessage());
ex.printStackTrace();
}
%>
</body>
</html>
--------------------------
运行报错如下
javax.servlet.ServletException: Name jdbc is not bound in this 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> 
<resource-ref> 
     <description>MySQL Connection Pool</description> 
     <res-ref-name>JDBC for MySQL</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref></web-app>

解决方案 »

  1.   

    你用的什么数据库,怎么没看到数据库的驱动啊
    Class.forName(驱动);
      

  2.   

    你用的是什么服务器?
    如果是tomcat,在tomcat的配置文件中还要配置。具体的到google搜索。我记得JR上有一篇专门讲DataSoure配置的文章。www.javaresearch.org
      

  3.   

    我在server.xml里面配了,google搜索了2天了,没解决问题
      

  4.   

    你把server.xml中的那部分Resource代码贴出来
      

  5.   


    <GlobalNamingResources>
        <Environment name="simpleValue" type="java.lang.Integer" value="30"/>
        <Resource auth="Container" description="User database that can be updated and saved" name="UserDatabase" type="org.apache.catalina.UserDatabase"/>
        <Resource name="JDBC for MySQL" type="javax.sql.DataSource"/>
        <ResourceParams name="UserDatabase">
          <parameter>
            <name>factory</name>
            <value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
          </parameter>
          <parameter>
            <name>pathname</name>
            <value>conf/tomcat-users.xml</value>
          </parameter>
        </ResourceParams>
        <ResourceParams name="JDBC for MySQL">
          <parameter>
            <name>maxWait</name>
            <value>5000</value>
          </parameter>
          <parameter>
            <name>maxActive</name>
            <value>4</value>
          </parameter>
          <parameter>
            <name>password</name>
            <value>sa</value>
          </parameter>
          <parameter>
            <name>url</name>
            <value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=zcwy</value>
          </parameter>
          <parameter>
            <name>driverClassName</name>
            <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
          </parameter>
          <parameter>
            <name>maxIdle</name>
            <value>2</value>
          </parameter>
          <parameter>
            <name>username</name>
            <value>sa</value>
          </parameter>
        </ResourceParams>
      </GlobalNamingResources>
      

  6.   

    你可以在代码里面直接得到conn
            try
    {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    }
    catch (InstantiationException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch (IllegalAccessException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch (ClassNotFoundException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
            String url="jdbc:oracle:thin:@localhost:1521:DBTEST";
            String user="jlrc"; 
            String password="jlrc"; 
            Connection conn = null;
    try
    {
    conn = DriverManager.getConnection(url, user, password);
    }
    catch (SQLException e1)
    {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
      

  7.   

    <Resource name="jdbc/bn" auth="Container" type="javax.sql.DataSource"/>   
         <ResourceParams name="jdbc/bn">
    jsp中:
           Context initCtx=new javax.naming.InitialContext();
           Context envCtx=(Context)initCtx.lookup("java:comp/env");
           DataSource ds=(DataSource)envCtx.lookup("jdbc/bn");
      

  8.   

    你用的是数据源啊?我建议你用另一个数据源,叫proxool的数据源,在网上搜一下,把Jar包down下来,配一下WEB.XML就得
      

  9.   

    你怎么不把数据库连接写在Bean里面?