java连接池中的一种,proxool,c3p0,dbcp
1. http://proxool.sourceforge.net 下载 proxool 的jar
2. 将proxool的jar导入web工程的lib中,导入mysql的jar
3. 于web-info下建立文件 proxool.xml ,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>mysql</alias> <!-- 别名,靠proxool.mysql来获取此连接 -->
<driver-url>jdbc:mysql://127.0.0.1:3306/mydb</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root"/>
<property name="password" value="sk123456"/>
</driver-properties>
<maximum-connection-count>10</maximum-connection-count>
</proxool>
</something-else-entirely>
4.于web-info/web.xml添加2个servlet,内容:
<?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">
<servlet>
<servlet-name>proxoolServletConfigurator</servlet-name>
<servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>
<init-param>
<param-name>xmlFile</param-name>
<param-value>WEB-INF/proxool.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- proxool提供的管理监控工具,可查看当前数据库连接情况。 -->
<servlet>
<servlet-name>Admin</servlet-name>
<servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Admin</servlet-name>
<url-pattern>/admin</url-pattern>
</servlet-mapping>
</web-app>
5.测试jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="org.logicalcobwebs.proxool.ProxoolDriver"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.ResultSet"%>
<html>
<head>
<title>Proxool Test</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//加载 proxool的驱动
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
//获取 注册好的,mysql:Proxool中注册的别名alias
Connection conn = DriverManager.getConnection("proxool.mysql");
out.print(conn);
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("select * from news");
while(rs.next()){
out.print(rs.getString("ids"));
}
rs.close();
st.close();
conn.close();
%>
</body>
</html>