我的tomcat5.0下的server.xml文件配置如下:(我数据库名为:jspdev,工程名为:Test)
<Context path="/Test" docBase="Test" debug="0" reloadable="true" crossContext="true">
<Resoure name="jdbc/bn" auth="Container" type="javax.sql.DataSource"/>
<ResoureParams 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>bn</value>
</parameter>
<parameter>
<name>password</name>
<value>bn</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>
</ResoureParams>
</Context>我的测试程序如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="javax.naming.Context" %> 
<%@ page import="javax.sql.DataSource"%> 
<%@ page import="javax.naming.InitialContext"%> 
<%@ page import="java.sql.*"%>  <% 
   DataSource ds = null; 
   try{ 
     Context initCtx = new InitialContext(); 
     System.out.print("aaa");
     Context envCtx = (Context) initCtx.lookup("java:comp/env"); 
    //从Context中lookup数据源。
    System.out.print("bbb");
     ds = (DataSource)envCtx.lookup("jdbc/bn"); 
     System.out.print("ccc");
if(ds!=null) 
{
out.println("已经获得DataSource!"); 
out.println("<br>");
Connection conn = ds.getConnection();
Statement stmt=conn.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 ne)
{
 out.println(ne);

 %> 我的问题是:测试程序没有正常运行,我添加的三个打印语句中aaabbb输出了,ccc没输出。抛出的异常是:javax.naming.NameNotFoundException: Name jdbc is not bound in this Context。
请高手指教,到底是怎么了?郁闷中。。

解决方案 »

  1.   

    web.xml中必须添加连接池定义具体例子随便找点资料就可以看到了
      

  2.   

    1、检查下Tomcat5\common\lib目录下是否有Msbase.jar、Msutil.jar、Mssqlserver.jar,另外CLASSPATH里也要添加这三个文件的安装路径。
    2、WEB-INF目录下的web.xml文件里在</web-app>之前加上
    <resource-ref>
             <description>Sql Server Datasource</description>
             <res-ref-name>jdbc/bn</res-ref-name>
             <res-type>javax.sql.DataSource</res-type>
             <res-auth>Container</res-auth>
    </resource-ref>
      

  3.   

    you database username password is bn?
      

  4.   

    tocmat5 conf\Catalina\localhost目录下Test.xml文件里加上
    <Context>
    <ResourceLink global="jdbc/bn" name="jdbc/bn" type="javax.sql.DataSource"/>
    </Context>
      

  5.   

    先改上边的问题。然后,楼主用的是sql server 数据库,如果要用jdbc来进行操作,必须对其再打上sp3的补丁。要不总是会报scoket错误!
      

  6.   

    多谢上面几位,特别是chense(MultiThread),按照你的已经有点进展了。可是运行测试程序还有问题,问题在Connection conn = ds.getConnection();这句话。它是不是找不到数据源?怎么回事?
    我确定我的sql server 2000已经启动,tomcat也重起了。数据库是jspdev 访问名是bn  密码是bn
    都没错啊。难道还要在server.xml里面再设置点什么吗?在线等。大哥们支招 啊。
      

  7.   

    在连接池初始化时,要定义一个数据库连接池的别名。conn = ds.getConnection( 数据库别名 ) ; 来得到
      

  8.   

    InitialContext ctx=new InitialContext();ds=(DataSource)ctx.lookup("java:comp/env/jdbc/bn");//这就是根据名字来查找数据源Connection conn = ds.getConnection();
      

  9.   

    找到%TOMCAT_HOME%\conf,打开web.xml,在</web-app>的前面添加以下内容:      <resource-ref>    <description>DB Connection</description>    <res-ref-name>jdbc/bn</res-ref-name>    <res-type>javax.sql.DataSource</res-type>    <res-auth>Container</res-auth>      </resource-ref>注意res-ref-name填写的内容要与在上文提到的JNDI Name名称一致。 找到%TOMCAT_HOME%\conf\Catalina\localhost下,找到你的web应用对应的.xml文件,如    Test.xml,并在此文件的下添入代码:<ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSourcer"/>
    查找数据源就用:InitialContext ctx=new InitialContext();ds=(DataSource)ctx.lookup("java:comp/env/jdbc/bn");//这就是根据名字来查找数据源Connection conn = ds.getConnection();
      

  10.   

    <Resoure         <Resource    
    <ResoureParams   <ResourceParams 
    放到我自己的Tomcat测试,才发现你是这么不细心。
      

  11.   

    感谢大家!回 OnlyFor_love(【光在哪里,荣耀就在哪里】) :
    <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/bn</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>
    我已经加到web.xml里面了。我在%TOMCAT_HOME%\conf\Catalina\localhost下,没有找到我的Test.xml文件,只有三个文件admin.xml  balancer.xml  manager.xml 。怎么回事啊?我用的是sql server2000,下面这句话是不是要改点什么?
    <ResourceLink name="jdbc/mysql" global="jdbc/mysql" type="javax.sql.DataSourcer"/>
      

  12.   

    %TOMCAT_HOME%\conf\Catalina\localhost下没有Test.xml文件也可以的,因为你的Context定义在Servlet.xml中了,这样也是可以的.    下面的这个是要加到网站项目的web.xml中的.
     <resource-ref>    <description>DB Connection</description>    <res-ref-name>jdbc/bn</res-ref-name>    <res-type>javax.sql.DataSource</res-type>    <res-auth>Container</res-auth>      </resource-ref>
      

  13.   

    问题已解决 !我非常感动!没有大家的帮助我没这么快解决得了问题。
    回:wsk_228(qing_feng) 
    谢谢你的批评,我这个人其实还是比较仔细的,你指出了我这个程序至命的错误,我改了之后能成功运行了。在此表示感谢!
      

  14.   

    我的怎么还是出错在
    getConnection()这一行上呀!?