package com.mysql.demo; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; //运用开源数据库连接池DBCP //引用dbcp.jar和pool.jar,读取dbcp的配置文件。引用的时候直接getConnection或getDataSource public class sqlpooling { private static DataSource ds=null; static{ try{ Properties p=new Properties(); InputStream in=sqlpooling.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"); p.load(in); BasicDataSourceFactory factory=new BasicDataSourceFactory(); ds=factory.createDataSource(p);// } catch(Exception e) { e.printStackTrace(); } } public static DataSource getDataSource() { return ds; } public static Connection getConnection() throws SQLException { return ds.getConnection(); //dbcp的Connction() } }
时间: 2024-10-12 05:09:05