爱不爱由你 发表于 2008-5-4 23:12

连接池连接SQL Server2000数据库

我想问一下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

我是按书本照抄的,运行不了,请高手指教一下,感激不尽!

onttpi 发表于 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?
或者没有加到环境变量中
页: [1]
查看完整版本: 连接池连接SQL Server2000数据库