Tomcat连接池首次配置成功状态
一.环境变量: PATH: C:\j2sdk1.4.2\bin;
JAVA_HOME: C:\j2sdk1.4.2
注:仅此两个.
二.SQL Server 驱动
COPY
Microsoft SQL Server 2000 Driver for JDBC\lib\目录下三个.jar :
msbase.jar
mssqlserver.jar
msutil.jar
To
Tomcat5\common\lib
三.虚拟目录与连接池
假设要在D:\JsgTest建自己的虚拟目录
/myjsp
假设要建连接池jndi 名为aaa
假设连接数据库为pubs
则在
Tomcat5\conf\Catalina\localhost
目录下新建文件:
myjsp.xml
内容如下:
<?xml version='1.0' encoding='utf-8'?>
<Context crossContext="true" displayName="myjsp" docBase="D:/JsgTest" path="/myjsp" reloadable="true">
<Resource name="aaa" type="javax.sql.DataSource"/>
<ResourceParams name="aaa">
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs</value>
</parameter>
<parameter>
<name>password</name>
<value>kingapex</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
</ResourceParams>
</Context>
测试:
在D:\JsgTest目录下建文件:Test.jsp:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%
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("aaa");
//类型转换
javax.sql.DataSource ds = (javax.sql.DataSource) obj;
conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = "select * from [titles]";
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.println(rs.getString(1));
}
rs.close();
stmt.close();
conn.close();
out.println("连接池测试成功");
%>
测试路径:
http://localhost:8080/myjsp/Test.jsp