DBCP连接池测试用例(8月修正版) HibernateJDBCSQL ServerSQLXML
程序员文章站
2022-07-12 16:31:23
...
WIN2000>>APACHE TOMCAT5.0.28(要求5.0及以上版本)>>SQL SERVER 2000
系统用的是SQL SERVER 库中的Northwind。采用第四类驱动,驱动类放到D:\testpool\WEB-INF\lib中。
保证TOMCAT和SQL SERVER正常运行。
[color=red][1]在%TOMCAT_HOME%\conf\Catalina\localhost\目录下建一个testPool.xml文件:
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="D:/testpool" path="/testpool" privileged="true" workDir="work\Catalina\localhost\testpool">
<Resource type="javax.sql.DataSource" auth="Container" name="jdbc/northwind"/>
<ResourceParams name="jdbc/northwind">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>password</name>
<value>12345</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://10.0.0.168:1433;databaseName=Northwind</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
</ResourceParams>
</Context>
[2]在D:\testpool\WEB-INF\下面建立一个web.xml文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<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">
<!--ConnectionPool-->
<resource-ref>
<res-ref-name>jdbc/northwind</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
[3]在D:\testpool\下面建立测试文件index.jsp
<%@ page contentType="text/html;charset=GB2312"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page session="false" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>tetst connection pool</title>
<%
out.println("我的测试开始");
DataSource ds = null;
try{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/northwind");
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = " select * from Categories";
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.println(rs.getString(1));
}
out.println("我的测试结束");
}
catch(Exception ex){
out.print("出现例外,信息是:"+ex.getMessage());
ex.printStackTrace();
}
%>
</head>
<body>
</body>
</html>
[4]补充:做配置时大体要搞清楚类似的几个问题,就是考虑WWW原则,要建或改什么文件(WHO),在那里做(WHERE),做什么(WHAT).与之对应的是:
WHO WHERE WHAT
testPool.xml %TOMCAT_HOME%\conf\Catalina\localhost\ 见第一步
web.xml(名字固定) D:\testpool\WEB-INF\web.xml 见第二步
index.jsp D:\testpool\index.jsp 见第三步
[5]如果使用hibernate,除作上面的外还需要更改hibernate.cfg.xml文件:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<hibernate-configuration>
<session-factory>
<!-- properties
<property name="connection.username">sa</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://10.0.0.168:1433;DataBaseName=cw_scene
</property>
<property name="dialect">
net.sf.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.password">12345</property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="hibernate.jdbc.fetch_size">50</property>
<property name="hibernate.jdbc.batch_size">25</property>
-->
<!-- properties -->
<property name="connection.datasource">
java:comp/env/jdbc/testpool
</property>
<property name="dialect">
net.sf.hibernate.dialect.SQLServerDialect
</property>
<property name="hibernate.connection.provider_class">
net.sf.hibernate.connection.DatasourceConnectionProvider
</property>
<property name="hibernate.jdbc.fetch_size">50</property>
<property name="hibernate.jdbc.batch_size">25</property>
<!-- mapping files -->
<mapping resource="com/scenechina/table/Imgpos.hbm.xml" />
</session-factory>
</hibernate-configuration>