« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
|
统计 |
blog名称:小雨 日志总数:262 评论数量:1273 留言数量:15 访问次数:4678425 建立时间:2005年1月8日 |
| 
|
W3CHINA Blog首页 管理页面 写新日志 退出
[经验杂谈]tomcat5.0 sqlserver2000 使用缓冲池的例子 |
1 下载sqlserver2000 for jdbc sp3
2 classpath=%TOMCAT_HOME%\common\lib\msbase.jar;%TOMCAT_HOME%\common\lib\mssqlserver.jar;%TOMCAT_HOME%\common\lib\msutil.jarJAVA_HOME=C:\j2sdk1.4.2_07
TOMCAT_HOME=C:\Program Files\Apache Software Foundation\Tomcat 5.0
3 将数据库驱动的3个jar文件和 C:\j2sdk1.4.2_07\lib 下的tool.jar 文件拷贝到TOMCAT_HOME下的common\lib 下
4 通过管理界面配置数据池
500)this.width=500'>
url=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=northwind
classname=com.microsoft.jdbc.sqlserver.SQLServerDriver
保存以后一定要提交
3 修改自己工程下的 web.xml文件,加入
<resource-ref> <description>myjdbc</description> <res-ref-name>myjdbc</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> 最后类似于
<?xml version="1.0" encoding="ISO-8859-1"?><!-- Copyright 2004 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.-->
<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">
<display-name>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description>
<!-- JSPC servlet mappings start -->
<resource-ref> <description>myjdbc</description> <res-ref-name>myjdbc</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref><!-- JSPC servlet mappings end -->
</web-app> 接下来,在%tomcat_home%\conf\Catalina\localhost目录下建立一个与你工程名同名的xml文件。例如你的工程名是Myapp,那么你就新建一个Myapp.xml,内容如下:<?xml version="1.0" encoding="utf-8"?><Context docBase="Myapp" path="/Myapp" workDir="work\Catalina\localhost\Myapp"><ResourceLink name="myjdbc" global="myjdbc" type="javax.sql.DataSource" /></Context>
4 重新启动 tomcat
5写jsp测试
<%@ page contentType="text/html;charset=gb2312"%><%@ page import="java.sql.*"%> <%@ page import="javax.sql.*"%> <%@ page import="javax.naming.*"%> <% int i=0;Connection conn = null; Context initCtx = new InitialContext();if (initCtx == null) throw new Exception("不能获取Context!");Context ctx = (Context) initCtx.lookup("java:comp/env");
Object obj = (Object) ctx.lookup("myjdbc");//获取连接池对象
javax.sql.DataSource ds = (javax.sql.DataSource) obj; //类型转换
conn = ds.getConnection();
Statement stmt = conn.createStatement();
PreparedStatement ps=conn.prepareStatement("select * from Orders");
ResultSet rs=ps.executeQuery();
while(rs.next()){
out.println(rs.getString(1)+"<BR>");
i++;
}
rs.close();
stmt.close();
conn.close();
out.println("连接池测试成功"+i); %>
最后记得启动sqlserver ,否则会报无法建立连接池的错误
|
阅读全文(4160) | 回复(0) | 编辑 | 精华 |
|