1.从aspx?familyid=4f8f2f01-1ed7-4c4d-8f7b-3d47969e66ae&displaylang=en#filelist">http://www.microsoft.com/downloads/details.aspx?familyid=4f8f2f01-1ed7-4c4d-8f7b-3d47969e66ae&displaylang=en#filelist下载"microsoft sql server 2000 driver for jdbc",并安装,得到msbase.jar,mssqlserver.jar和msutil.jar三个文件,将三个文件copy到tomcat 4.1下common/lib文件夹中
2.在tomcat 4.1的server.xml中host域中添加如下代码
factory
org.apache.commons.dbcp.basicdatasourcefactory
maxactive
100
maxidle
30
maxwait
10000
username
sa
password
你的密码
driverclassname
com.microsoft.jdbc.sqlserver.sqlserverdriver
url
jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs
3.在webapps下新建文件夹"myweb","myweb/web-inf","myweb/web-inf/classes"
4.在web-inf下新建文件"web.xml",并添加如下内容
"java.sun.com/dtd/web-app_2_3.dtd">http://java.sun.com/dtd/web-app_2_3.dtd">
my web
invoker
/servlet/*
jdbc/mydb
javax.sql.datasource
container
5.编写servlet程序jdbcpoolserv.java
import java.sql.*;
import javax.naming.context;
import javax.sql.datasource;
import javax.naming.initialcontext;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class jdbcpoolserv extends httpservlet
{
public void doget(httpservletrequest request,
httpservletresponse response) throws servletexception,ioexception {
datasource ds=null;
connection con=null;
string val="null",title="jdbc pooling test";
try{
context initctx = new initialcontext();
if(initctx == null )
throw new exception("boom - no context");
ds = (datasource)initctx.lookup(
"java:comp/env/jdbc/mydb");
if (ds != null){
con = ds.getconnection();
if (con != null){
statement stmt=con.createstatement();
resultset rs=stmt.executequery("select * from authors");
rs.next();
val=rs.getstring("au_id");
rs.close();
stmt.close();
}
con.close();
}
}
catch(exception ex){
system.out.println(ex.getmessage());
}
response.setcontenttype("text/html");
printwriter out = response.getwriter();
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("
" + val + "
");
out.println("");
out.println("");
}
}
6.编译jdbcpoolserv.java得到jdbcpoolserv.class(注意加入servlet.jar包),将其copy到"myweb/web-inf/classes"下
7.启动sql server2000
8.启动tomcat
9.浏览http://127.0.0.1:8080/myweb/servlet/jdbcpoolserv
10.在ie中可看到"172-32-1176"
注意:如果无法正常运行请检查以上文件夹名,url和java类名大小写是否一致
新闻热点
疑难解答