TomCat6.0配置连接池连接SQL Server 2005数据库
1、在你的项目里的META-INF里建一个context.xml文件,内容如下。
<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true" crossContext="true">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<Resource
name="jdbc/NorthWind"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DataBaseName=NorthWind"
username="sa"
password="sa"
maxActive="100"
maxIdle="30"
maxWait="1000"/>
</Context>
2、打开WEB-INF下的Web.xml文件,加入如下代码:
<resource-ref>
<description>NorthWind</description>
<res-ref-name>jdbc/NorthWind</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3、把SQl Server2005JDBC驱动拷贝到WEB-INF下的Lib里。
4、测试代码:
<%@ page language="java" contentType="text/html;charset=utf-8" imp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>JDBC 连接池</title></head>
<body>
<%
DataSource ds = null;
InitialContext ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env/jdbc/NorthWind");//这里
java:comp/env/ 后面的值就是context.xml 文件里 Resource 项下 name 的值
"jdbc/pool_mssql"
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = "SELECT * FROM Employees"; //找个能运行的 SQL 语句试试即可
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.print(rs.getString(2)+"<br />");
}
%>
</body>
</html>