工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 2255|回复: 7

连接池连接SQL Server2000数据库

[复制链接]
发表于 2008-5-4 23:12 | 显示全部楼层 |阅读模式
我想问一下JBuiler使用连接池连接SQL Server2000数据库javax.naming.NameNotFoundException: Name jdbc is not bound in this Context 这是什么问题啊?
我在server.xml上加上了
<Context path="/DBTest" reloadable="true" docBase="${catalina.home}/webapps/DBTest">
        <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_DBTest_log."
          suffix=".txt" timestamp="true"/>
          <Resource name="jdbc/my_db" auth="Container" type="java.sql.DataSource"/>
          <ResourceParams name="jdbc/my_db">
            <parameter>
              <name>factory</name>
              <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
              </parameter>
             <parameter>
               <name>maxActive</name>
               <value>100</value>
               </parameter>
               <parameter>
                 <name>maxIdle</name>
                 <value>30</value>
                 </parameter>
                 <parameter>
                   <name>maxWait</name>
                   <value>10000</value>
                   </parameter>
                   <parameter>
                     <name>username</name>
                     <value>sa</value>
                     </parameter>
                       <parameter>
                         <name>password</name>
                         <value></value>
                         </parameter>
                         <parameter>
                           <name>driveClassName</name>
                           <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
                           </parameter>
                           <parameter>
                             <name>url</name>
     <value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=my_db;user=sa;password=;</value>
     </parameter>
     </ResourceParams>
     </Context>
 楼主| 发表于 2008-5-4 23:13 | 显示全部楼层
在tomcat/webapps/DBTest下创建了web.xml,内容如下:
    <?xml version="1.0" encoding="ISO-8859-1"?>
<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>SQLServer2000 Test APP</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/my_db</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
回复

使用道具 举报

 楼主| 发表于 2008-5-4 23:14 | 显示全部楼层
源程序代码如下:<%@page contentType="text/html;charset=gb2312" language="java" import="java.sql.*,javax.naming.*,javax.sql.*" errorPage=""%>
<html>
  <body>
    <%
   out.print("使用连接池连接SQL Server2000数据库成功!<br>");
   out.print("<br>");
   Context ctx=null;
   DataSource ds=null;
   Statement stmt=null;
   ResultSet rs=null;
   Connection con=null;
   ResultSetMetaData md=null;
   try{
   ctx=new InitialContext();
   ds=(DataSource)ctx.lookup("java:comp/env/jdbc/my_db");
   con=ds.getConnection();
   stmt=con.createStatement();
   rs=stmt.executeQuery("select * from stu_info");
   md=rs.getMetaData();
   out.print(md.getColumnLabel(1)+"");
   out.print(md.getColumnLabel(2)+"");
   out.print(md.getColumnLabel(3)+"");
   out.print(md.getColumnLabel(4)+"");
   while(rs.next()){
   out.print(rs.getInt(1)+"");
   out.print(rs.getString(2)+"");
    out.print(rs.getString(3)+"");
    out.print(rs.getString(4)+"<br>");
   }
}catch(Exception e){
out.print(e);
}finally{
if(rs!=null)rs.close();
if(stmt!=null)stmt.close();
if(con!=null)con.close();
if(ctx!=null)ctx.close();
}
    %>
    </body>
    </html>
回复

使用道具 举报

 楼主| 发表于 2008-5-4 23:16 | 显示全部楼层
我是按书本照抄的,运行不了,请高手指教一下,感激不尽!
回复

使用道具 举报

发表于 2008-5-4 23:58 | 显示全部楼层
不懂JSP,LZ把运行后出错情况列出来吧?不要让别人也运行一遍。。。
回复

使用道具 举报

发表于 2008-5-6 02:41 | 显示全部楼层
把代码贴出来,楼主自己参考吧
1、在 server.xml 的  <Host></Host> 标签之前增加一个应用

    <Context path="/mynewtestweb" docBase="E:\TestWeb" debug="5" reloadable="true" crossContext="true">
                        <Resource name="jdbc/test_conn" auth="Container" type="javax.sql.DataSource"
                        maxActive="100" maxIdle="30" maxWait="10000"
                        username="sa" password="" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
                        url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Library_manage;User=sa;Password=;"/>
                </Context>

2、 在 E:\TestWeb 这个应用的 web.xml 中增加

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

3、编写连接代码,如下

    <%@page contentType="text/html;charset=gb2312" language="java" import="java.sql.*,javax.naming.*,javax.sql.*"%>
<html>
  <body>
    <%
   Context ctx = new InitialContext();
   DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/test_conn");
   if (ds != null) {
        out.println("连接成功");
   }
    %>
    </body>
    </html>


测试成功
回复

使用道具 举报

发表于 2008-5-6 02:59 | 显示全部楼层
其实上面第二步可以不要,但是别忘了将 JDBC Driver 放到 lib/ 下
回复

使用道具 举报

发表于 2008-5-6 12:05 | 显示全部楼层
楼主会不会没有装JDBC DRIVER?
或者没有加到环境变量中
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2024-5-17 04:10

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表