proxool是什么呢?简单的说就是高效+便捷的数据库访问技术。引用百度百科弄来的原话:
Proxool是一种Java数据库连接池技术。是sourceforge下的一个开源项目,这个项目提供一个健壮、易用的连接池,最为关键的是这个连接池提供监控的功能,方便易用,便于发现连接泄漏的情况。
目前是和DBCP以及C3P0一起,最为常见的三种JDBC连接池技术。
日前,Hibernate官方宣布由于Bug太多不再支持DBCP,而推荐使用 Proxool或C3P0。
使用proxool一共需要2个基本包+1个数据库驱动包
commons-logging-1.1.1.jar
proxool-0.9.0RC2.jar
mysql驱动包:mysql-connector-java-5.0.6-bin.jar
oracle10g驱动包:ojdbc14_g.jar
连接配置文件datasource.properties
注意:后面千万别有多余的空格,我曾经连oracle的时候驱动名后面有空格,一直提示找不到驱动,弄了5个小时才发现原因。
jdbc-0.usepool=true jdbc-0.proxool.alias=mysql jdbc-0.proxool.driver-class=com.mysql.jdbc.Driver jdbc-0.proxool.driver-url=jdbc:mysql://localhost:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8 jdbc-0.user=root jdbc-0.password=root jdbc-0.proxool.maximum-connection-count=10 jdbc-0.proxool.minimum-connection-count=1 jdbc-0.proxool.house-keeping-test-sql=select current_timestamp #jdbc-1.usepool=true #jdbc-1.proxool.alias=oracle #jdbc-1.proxool.driver-class=oracle.jdbc.driver.OracleDriver #jdbc-1.proxool.driver-url=dbc:oracle:thin:@localhost:xe #jdbc-1.user=root #jdbc-1.password=root #jdbc-1.proxool.maximum-connection-count=10 #jdbc-1.proxool.minimum-connection-count=1 #jdbc-1.proxool.house-keeping-test-sql=select sysdate from dual
数据库访问服务类DataService.java
package mainCenter; import java.sql.*; /** * Created by IntelliJ IDEA. * User: wnj * Date: 2010-11-26 * Time: 13:48:45 * To change this template use File | Settings | File Templates. */ public class DataService { private static final ThreadLocal<Connection> _conn = new ThreadLocal<Connection>(); public static int save(String msg){ Connection con=null; PreparedStatement pst=null; try{ con=getConnection(); pst=con.prepareStatement("insert into LOG4J(`message`) values (?)"); int index=1; pst.setString(index++, msg == null? "":msg); return pst.executeUpdate(); }catch(Throwable th){ th.printStackTrace(); }finally{ try{ if(pst!=null){ pst.close(); } clearAll(); }catch(Throwable th){ th.printStackTrace(); } } return -1; } public static String getCityId(String message){ Connection con=null; PreparedStatement pst=null; ResultSet set = null; try{ con=getConnection(); pst=con.prepareStatement("select message from LOG4J where message=?"); int index=1; pst.setString(index++, message); set = pst.executeQuery(); set.next(); return set.getString("message"); }catch(Throwable th){ th.printStackTrace(); }finally{ try{ if(set!=null){ set.close(); } if(pst!=null){ pst.close(); } clearAll(); }catch(Throwable th){ th.printStackTrace(); } } return null; } private static Connection getConnection() throws SQLException { Connection conn = _conn.get(); if(conn == null){ conn = DriverManager.getConnection("proxool.mysql");//这句是重点,这样就能获取到连接所需的connect对象了。 _conn.set(conn); } return conn; } private static void clearAll() throws SQLException{ closeConn(); } private static void closeConn() throws SQLException{ Connection conn = _conn.get(); _conn.set(null); if (conn != null) { if (!conn.isClosed()) { conn.close(); } } } }
程序入口MainCenter.java
package mainCenter; import org.logicalcobwebs.proxool.configuration.PropertyConfigurator; import java.util.Properties; /** * Created by IntelliJ IDEA. * User: Administrator * Date: 2011-7-11 * Time: 14:11:16 * To change this template use File | Settings | File Templates. */ public class MainCenter { public static void main(String[] args) throws Exception //程序运行之前,先要加载配置文件 Properties dbProps = new Properties(); dbProps.load(MainCenter.class.getResourceAsStream("/datasource.properties")); PropertyConfigurator.configure(dbProps); DataService.save("message1"); DataService.save("message2"); String msg = DataService.getCityId("message1"); System.out.println(msg); } }