javax.naming.NameNotFoundException: Name jdbc is not bound in this Context 这是文件运行后出现的问题,我没怎么学过JAVA,所以不大清楚问题出在哪,请哪位知道的帮我看一下,谢了

解决方案 »

  1.   

    java里面连接是这样的
    你可以写成
    <% 
    //java代码
      Connection conn;
         try {
            Class.forName("oracle.jdbc.driver.OracleDriver");//连接的数据源
            System.out.println("JDBC-ODBC注册成功");
          }
          catch (Exception e) {
            System.out.println("JDBC-ODBC驱动程序注册不成功");
          }      try {
            conn = DriverManager.getConnection(
                "连接数据库的地址", "数据库用户名", "密码");
            System.out.println("JDBC-ODBC注册成功-----------------------");
          }
          catch (Exception e) {
            System.out.println("JDBC-ODBC驱动程序注册不成功--------------------------");
          }
    %>
      

  2.   

    顺便说一句,检查你的context.xml是否配置正确……
      

  3.   

    server.xml的部份配置如下:
    <Context  path="/ch13" docBase="ch13" debug="0" reloadable="true" crossContext="true" >
            <Resource name="jdbc/bn" auth="Container" type="javax.sql.DataSource"/>   
             <ResourceParams name="jdbc/bn">
             <parameter>
           <name>factory</name>
            <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
        </parameter>
      <parameter>
         <name>driverClassName</name>
         <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
      </parameter>
      <parameter>
        <name>url</name>
           <value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev</value>  
          </parameter>
     <parameter>     
        <name>username</name>
          <value>sa</value>   
          </parameter>
     <parameter>     
      <name>password</name>     
      <value>""</value>
          </parameter>
    <parameter>     
        <name>maxActive</name>     
        <value>20</value>   
    </parameter>
      <parameter>            
          <name>maxIdle</name>    
           <value>10</value>   
          </parameter>
          <parameter>
             <name>maxWait</name>     
             <value>-1</value>   
            </parameter>
            </ResourceParams> 
      
           </Context>所写的测试jsp文件如下:
    <%@ page contentType="text/html; charset=gb2312" language="java"  errorPage=""%>
    <%@ page import="javax.naming.Context" %>
    <%@ page import= "java.sql.*" %>
    <%@ page import = "javax.sql.DataSource"%>
    <%@ page import = "javax.naming.InitialContext"%>
    <%@ page import= "java.sql.Statement" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'conpool.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
      </head>
      
      <body>
        <%
        DataSource ds = null;
        try
        {
         Context initCtx = new InitialContext();
         Context envCtx = (Context)initCtx.lookup("java:comp/env");
         ds = (DataSource)envCtx.lookup("jdbc/bn");
         if(ds!=null)
         {
         out.println("已经获得DataSource!");
         out.println("<br>");
         Connection con = ds.getConnection();
         Statement stmt = con.createStatement();
         ResultSet rst = stmt.executeQuery("select * from book");
         out.println("以下是从数据库中读取出来的数据");
         while(rst.next())
         {
         out.println("bookName:" + rst.getString("bookname"));
         out.println("<br>");
         }
         }
         else
         {
         out.println("数据库连接失败");
         }
        }
        catch(Exception e)
        {
         out.println(e);
        }
        
        %>
      </body>
    </html>
      

  4.   

    Context envCtx = (Context)initCtx.lookup("java:comp/env");
         ds = (DataSource)envCtx.lookup("jdbc/bn");把这两行改成ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/bn");
      

  5.   

    在TOMCAT的官方文档中有标准写法
    http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html
      

  6.   

    你放oracle的jar包到工程了吗,一定没有
      

  7.   

    连接池的配置要写在你工程的context.xml文件里.比如在tomcat5.x里你的工程叫myweb,那么你的context.xml文件就是Tomcat_home\conf\Catalina\localhost\myweb.xml