c3p0数据源

来源:互联网 发布:mac用的vpn 编辑:程序博客网 时间:2024/06/06 01:02

需要读取的配置文件c3p0-cinfig.xml

<c3p0-config><!-- c3p0-config.xml的默认配置,在classpath下--><!-- 如果要使用自己的配置,需要该name属性 --><default-config><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property><property name="user">root</property><property name="password">mysqladmin</property><property name="initialPoolSize">10</property><property name="maxIdleTime">30</property><property name="maxPoolSize">20</property><property name="minPoolSize">5</property><property name="maxStatements">200</property></default-config><!-- 自己的配置 --><named-config name="mysql"><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property><property name="user">root</property><property name="password">mysqladmin</property><property name="acquireIncrement">50</property><property name="initialPoolSize">100</property><property name="minPoolSize">50</property><property name="maxPoolSize">1000</property><!-- intergalactoApp adopts a different approach to configuring statement caching --><property name="maxStatements">0</property><property name="maxStatementsPerConnection">5</property></named-config><named-config name="oracle"><property name="acquireIncrement">50</property><property name="initialPoolSize">100</property><property name="minPoolSize">50</property><property name="maxPoolSize">1000</property><!-- intergalactoApp adopts a different approach to configuring statement caching --><property name="maxStatements">0</property><property name="maxStatementsPerConnection">5</property></named-config></c3p0-config>
操作数据库的工具类JdbcUtils.java
import com.mchange.v2.c3p0.ComboPooledDataSource;public class JdbcUtils{private static ComboPooledDataSource ds = null;static{try{//c3p0-config.xml配置文件在classpath下ds = new ComboPooledDataSource("mysql");//使用c3p0-config.xml中的mysql的配置,//ds = new ComboPooledDataSource();//使用c3p0-config.xml的默认配置//如果不使用c3p0-cinfig.xml配置文件,自己写配置//ds.setDriverClass("com.mysql.jdbc.Driver");//ds.setJdbcUrl("jdbc:mysql://localhost:3306/test");//ds.setUser("root");//ds.setPassword("mysqladmin");//ds.setMaxPoolSize(30); //最大允许30个链接//ds.setMinPoolSize(5);//最小允许5个链接//ds.setInitialPoolSize(10);//初始化链接个数}catch (Exception e) {throw new ExceptionInInitializerError(e);}}public static Connection getConnection() throws SQLException{return ds.getConnection();//从c3p0数据源中获得数据库链接}public static void release(Connection conn,Statement st,ResultSet rs){if(rs != null){try{rs.close();}catch(Exception e){}}if(st != null){try{st.close();}catch(Exception e){}}if(conn != null){try{conn.close();//从c3p0数据源中获得数据库链接的close方法被增强,将数据库链接归还给连接池}catch(Exception e){}}}}
test.java

public class test {public static void main(String[] args) {Connection conn = null;PreparedStatement st= null;ResultSet rs = null;try{conn = JdbcUtils.getConnection();// 从c3p0数据源中获得链接System.out.println(conn.getClass().getName());}catch (Exception e) {// TODO: handle exception}JdbcUtils.release(conn, st, rs);//释放链接,将链接归还到连接池}}



0 0