Tomcat5+Mssql server 2000数据库连接池配置之旅

2019-05-27 08:06:23丽君

     DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");

     if (ds != null) {
       Connection conn = ds.getConnection();

       if(conn != null)  {
           foo = "Got Connection "+conn.toString();
           Statement stmt = conn.createStatement();
           ResultSet rst =
               stmt.executeQuery("select * from orders");
           if(rst.next()) {
              foo=rst.getString("CustomerID");
              bar=rst.getInt("OrderID");
           }
           conn.close();
       }
     }
   }catch(Exception e) {
     e.printStackTrace();
   }
}

public String getFoo() { return foo; }
public int getBar() { return bar;}
}


然后是index.jsp文件

<html>
 <head>
   <title>DB Test</title>
 </head>
 <body>

 <%
   foo.DBTest tst = new foo.DBTest();
   tst.init();
 %>

 <h2>Ms sql server 2000 java search Results</h2>
   Foo <%= tst.getFoo() %><br/>
   Bar <%= tst.getBar() %>

 </body>
</html>
'www.knowsky.com

编译运行,如果不出意外,应该检索到一条记录,

我的ie中显示的是
Ms sql server 2000 java search Results
Foo VINET
Bar 10248

ok,配制成功!

参考文档:
http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html 上面有mysql和oracle8i的连接教程,有兴趣的朋友可以上去看一下。

作者 海仔  email:rautinee@21cn.com  http://www.tryitsoft.com