proxool连接池的优点:
1.透明度:透明地将连接池添加到现有的JDBC驱动程序。
2.开源:我们的许可证允许您灵活地将其用于商业和其他开源产品。
3.标准:符合J2SE API,使您有信心开发标准。
4.控制:您可以监视数据库连接的性能并监听连接事件
5.易于使用:您可以轻松地使用JDBC API,XML或Java属性文件进行配置。
这里有用到两种数据库,一个是mysql,一个是hsqldb,需要用到的包一共有8个,分别是:
mysql驱动包:mysql-connector-java-5.1.43-bin.jar
hsql驱动包:hsqldb.jar
proxool连接池的包:proxool-0.9.1.jar,proxool-cglib.jar,cglib-nodep-2.1_3.jar,jarjar-0.6.jar
还有:commons-logging-1.0.4.jar,commons-logging-api-1.1.jar
用到的包有上面几个(有点多,去官网下载就好了
//类配
try {
Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
Connection conn=DriverManager.getConnection("proxool.example:com.mysql.jdbc.Driver:jdbc:mysql://localhost:3306/test","root","123456"); System.out.println(conn); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //xml配置 (无需配置web.xml要注意DriverManager.getConnection("proxool.ljg-xml")里面的参数必须是proxool.别名) try { JAXPConfigurator.configure(new InputSource(DBConfig.class.getResourceAsStream("/db.xml")), false); Connection conn = DriverManager.getConnection("proxool.ljg-xml"); System.out.println(conn); } catch (ProxoolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
/*
db.xml必须在src目录下(不在任何一个包中)
代码如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- the proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored. -->
<something-else-entirely>
<proxool>
<alias>ljg-xml</alias>
<driver-url>jdbc:mysql://localhost:3306/test</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root"/>
<property name="password" value="2311664"/>
</driver-properties>
<maximum-connection-count>10</maximum-connection-count>
<house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
</proxool>
</something-else-entirely>
*/ //这是连接hsqldb的
//启动hsqldb需要运行hsqldb.jar包下的两个文件:
//1.运行org.hsqldb.server.Server
//2.运行org.hsqldb.util.DatabaseManagerSwing
//properties配置(无需配置web.xml要注意DriverManager.getConnection("proxool.ljg-properties")里面的参数必须是proxool.别名) Properties pro=new Properties(); try { pro.load(DBConfig.class.getResourceAsStream("/hsqldb.properties")); PropertyConfigurator.configure(pro); Connection conn=DriverManager.getConnection("proxool.hsqlljg-properties"); Statement st=conn.createStatement(); ResultSet rs=st.executeQuery("SELECT * FROM "PUBLIC"."USERINFO""); System.out.println(conn); while(rs.next()) { System.out.println(rs.getString(1)); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ProxoolException e) { // TODO Auto-generated catch block e.printStackTrace(); }
/*
同上hsqldb.properties也要在src目录下
代码如下:
jdbc-0.proxool.alias=hsqlljg-properties
jdbc-0.proxool.driver-url=jdbc:hsqldb:hsql://localhost/
jdbc-0.proxool.driver-class=org.hsqldb.jdbcDriver
jdbc-0.user=SA
jdbc-0.password=
jdbc-0.proxool.maximum-connection-count=10
jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE
url和驱动根据实际而定
*/ //这是mysql的,只需要把配置的驱动和url修改即可 Properties pro=new Properties(); try { pro.load(DBConfig.class.getResourceAsStream("/db.properties")); PropertyConfigurator.configure(pro); Connection conn=DriverManager.getConnection("proxool.ljg-properties"); System.out.println(conn); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ProxoolException e) { // TODO Auto-generated catch block e.printStackTrace(); }
/*
db.properties配置文件信息如下:
jdbc-0.proxool.alias=ljg-properties
jdbc-0.proxool.driver-url=jdbc:mysql://localhost:3306/test
jdbc-0.proxool.driver-class=com.mysql.jdbc.Driver
jdbc-0.user=root
jdbc-0.password=2311664
jdbc-0.proxool.maximum-connection-count=10
jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE
*/ //在类里写properties文件配置 try { Properties info = new Properties(); info.setProperty("proxool.maximum-connection-count", "10"); info.setProperty("proxool.house-keeping-test-sql", "select CURRENT_DATE"); info.setProperty("user", "root"); info.setProperty("password", "2311664"); String alias = "ljg-properties"; String driverClass = "com.mysql.jdbc.Driver"; String driverUrl = "jdbc:mysql://localhost:3306/test"; String url = "proxool." + alias + ":" + driverClass + ":" + driverUrl; ProxoolFacade.registerConnectionPool(url, info); Connection conn=DriverManager.getConnection("proxool.ljg-properties"); System.out.println(conn); } catch (ProxoolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
配置成功运行会输出像这么一句代码:
如果要配置管理页面只需在web.xml中添加如下代码:
<servlet> <servlet-name>proxool</servlet-name> <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>proxool</servlet-name> <url-pattern>/admin</url-pattern> </servlet-mapping> <servlet> <servlet-name>ServletConfigurator</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>
在lib下创建proxool.xml文件,代码如下:
<?xml version="1.0" encoding="ISO-8859-1"?> <!-- the proxool configuration can be embedded within your own application's. Anything outside the "proxool" tag is ignored. --> <something-else-entirely> <proxool> <alias>ljg-xml</alias> <driver-url>jdbc:mysql://localhost:3306/test</driver-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <driver-properties> <property name="user" value="root"/> <property name="password" value="2311664"/> </driver-properties> <maximum-connection-count>10</maximum-connection-count> <house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql> </proxool> </something-else-entirely>
配置完后在浏览器打开localhost:8080/myapp/admin 即可访问