`

Tomcat5.5配置连接池

 
阅读更多
1.配置驱动包
将如下三个sql2000的驱动jar包复制到tomcat-root/common/lib目录下
msbase.jar
mssqlserver.jar
msutil.jar
(注:tomcat-root:tomcat的根目录)
2.配置连接池
在tomcat-root/conf/server.xml里添加以下红色代码片段:

<!-- 上面省略 -->
<Host name="localhost" appBase="webapps" unpackWARs="true"
autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context path="/connPoll" docBase="connPoll" debug="5"
reloadable="true" crossContext="true">
<Resource name="jdbc/EmployeeDB" auth="Container"
type="javax.sql.DataSource" username="sa" password=""
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"
maxActive="8" maxIdle="4" />
</Context>
</Host>
<!-- 下面省略 -->

3.在web.xml里添加JNDI资源的引用,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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">
<resource-ref>
<description>
Resource reference to a factory for java.sql.Connection
instances that may be used for talking to a particular
database that is configured in the server.xml file.
</description>
<res-ref-name>jdbc/EmployeeDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

4.页面测试
test.jsp

<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.*,javax.sql.*,javax.naming.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Tomcat5.5连接池测试</title>
</head>
<body>
<%
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB");
Connection conn = ds.getConnection();
Statement sta = conn.createStatement();
ResultSet rs = sta.executeQuery("select * from authors");
while (rs.next()) {
out.println(rs.getString("au_lname") + "<br>");
}
conn.close();
%>
</body>
</html>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics