tomcat6中连接池的使用

本文详细介绍了如何在Tomcat服务器中配置Oracle数据库连接池,并通过编写Java类和JSP页面实现与数据库的交互,包括获取连接、执行查询和更新操作,以及展示查询结果。此外,还说明了在Web工程中引入数据库驱动jar包并启动应用的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、新建一个web工程,在tomcat的server.xml中,将应用和连接池绑定。

<?xml version="1.0" encoding="UTF-8"?>
 <Host appBase="webapps" autoDeploy="true" name="localhost"
 	unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">
 	<Context docBase="connectionPoolTest" path="/connectionPoolTest"
 		reloadable="false"
 		source="org.eclipse.jst.jee.server:connectionPoolTest">
 		<Resource driverClassName="oracle.jdbc.driver.OracleDriver"
 			maxActive="100" maxIdle="100" maxWait="5000" name="testPool"
 			password="oracle" type="javax.sql.DataSource"
 			url="jdbc:oracle:thin:@localhost:1521:orcl" username="system" />
 	</Context>
 </Host>
2、创建2个测试连接的类。
package connection;
 
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
 import javax.sql.DataSource;
 import javax.sql.rowset.CachedRowSet;
 
 import com.sun.rowset.CachedRowSetImpl;
 
 public class DBUtil {
 	/**
 	 * 根据数据源名称获取数据源
 	 * 
 	 * @param dsName
 	 * @return
 	 * @throws NamingException
 	 */
 	public static DataSource getDataSource(String dsName) {
 		DataSource ds = null;
 		try {
 			ds = (DataSource) new InitialContext().lookup("java:comp/env/"
 					+ dsName);
 			System.out.println("tomcat datasource");
 		} catch (NamingException e) {
 			try {
 				System.out.println("weblogic datasource");
 				ds = (DataSource) new InitialContext().lookup(dsName);
 			} catch (NamingException e1) {
 				e1.printStackTrace();
 			}
 		}
 		return ds;
 	}
 
 	/**
 	 * 获取一个默认的数据连接
 	 * 
 	 * @return
 	 * @throws SQLException
 	 * @throws NamingException
 	 */
 	public static Connection getDefaultConnection() throws SQLException,
 			NamingException {
 		return DBUtil.getDataSource("HXPT").getConnection();
 	}
 
 	/**
 	 * 简单查询获取数据集,不包含条件。
 	 * 
 	 * @param sql
 	 * @return
 	 * @throws SQLException
 	 * @throws NamingException
 	 */
 	public static CachedRowSet execSimpleQuery(String sql) throws SQLException,
 			NamingException {
 		CachedRowSet rs = new CachedRowSetImpl();
 		Connection conn = getDefaultConnection();
 		Statement stmt = conn.createStatement();
 		ResultSet resultset = stmt.executeQuery(sql);
 		rs.populate(resultset);
 		resultset.close();
 		stmt.close();
 		conn.close();
 		return rs;
 	}
 
 	/**
 	 * 查询获取数据集,包含条件。
 	 * 
 	 * @param sql
 	 * @return
 	 * @throws SQLException
 	 * @throws NamingException
 	 */
 	public static CachedRowSet execQuery(String sql, Object[] params)
 			throws SQLException, NamingException {
 		CachedRowSet rs = new CachedRowSetImpl();
 		Connection conn = getDefaultConnection();
 		PreparedStatement psmt = conn.prepareStatement(sql);
 		for (int i = 0; i < params.length; i++) {
 			psmt.setObject(i + 1, params[i]);
 		}
 		ResultSet resultset = psmt.executeQuery();
 		rs.populate(resultset);
 		resultset.close();
 		psmt.close();
 		conn.close();
 		return rs;
 	}
 
 	/**
 	 * 执行DML语句。
 	 * 
 	 * @param sql
 	 * @return
 	 * @throws NamingException
 	 * @throws SQLException
 	 * @throws SQLException
 	 * @throws NamingException
 	 */
 	public static void execUpdate(String sql, Object[] params)
 			throws SQLException, NamingException {
 		Connection conn = getDefaultConnection();
 		PreparedStatement psmt = conn.prepareStatement(sql);
 		for (int i = 0; i < params.length; i++) {
 			psmt.setObject(i + 1, params[i]);
 		}
 		psmt.executeUpdate();
 		psmt.close();
 		conn.close();
 	}
 }
package connection;
 
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Date;
 import java.text.SimpleDateFormat;
 import javax.naming.NamingException;
 import javax.sql.rowset.CachedRowSet;
 
 public class TestConnection {
 	public String welcome() throws NamingException, SQLException {
 		// 获取连接
 		Date result = null;
 		SimpleDateFormat sdt = null;
 		Connection conn = DBUtil.getDefaultConnection();
 		ResultSet rs = conn.createStatement().executeQuery(
 				"select sysdate from dual");
 		while (rs.next()) {
 			result = rs.getTimestamp("sysdate");
 			sdt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
 			result = new Date(result.getTime());
 		}
 		// 测试连接是否成功
 		return "当前时间:" + "</br>" + sdt.format(result) + "</br>";
 	}
 
 	public String testRs() throws SQLException, NamingException {
 		StringBuffer sb = new StringBuffer("tableList:").append("</br>");
 		CachedRowSet rs = DBUtil
 				.execQuery(
 						"select table_name from user_tables where table_name like ? and rownum<?",
 						new Object[] { "%", 100 });
 		while (rs.next()) {
 			sb.append(rs.getString("table_name")).append("</br>");
 		}
 		return sb.toString();
 	}
 }
 

3、新建一个测试连接的jsp文件。
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <%@page import="connection.TestConnection;"%>
 <%@ page language="java" contentType="text/html; charset=utf-8"
 	pageEncoding="utf-8"%>
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <title>welcome</title>
 </head>
 <body>
 <%
 	TestConnection tc = new TestConnection();
 	out.print(tc.welcome());
 	out.print(tc.testRs());
 %>
 </body>
 </html>
4、将ojdbc14.jar放入该工程的\WEB-INF\lib中,启动应用。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值