dbcp连接池
需要的架包1.commons-dbcp.jar
2.commons-pool.jar
3.mysql-connector-java-5.0.8-bin.jar
DBCP相关配置:DBCP configuration
说明:
defaultAutoCommit: 对于事务是否 autoCommit, 默认值为 true
defaultReadOnly: 对于数据库是否只能读取, 默认值为 false
driverClassName:连接数据库所用的 JDBC Driver Class,
maxActive: 连接池的最大数据库连接数。设为0表示无限制,默认为8
maxIdle: 最大等待连接中的数量,设 0 为没有限制 (对象池中对象最大个数)
minIdle:对象池中对象最小个数
maxWait: 最大等待秒数, 单位为 ms, 超过时间会丟出错误信息
password: 登陆数据库所用的密码
url: 连接数据库的 URL
username: 登陆数据库所用的帐号
validationQuery: 验证连接是否成功,该选项用来验证数据库连接的有效性, SQL SELECT 指令至少要返回一行
removeAbandoned: 是否自我中断, 默认是 false
removeAbandonedTimeout: 几秒后会自我中断, removeAbandoned 必须为 true
logAbandoned: 是否记录中断事件, 默认为 false
minEvictableIdleTimeMillis:大于0 ,进行连接空闲时间判断,或为0,对空闲的连接不进行验证;默认30分钟
timeBetweenEvictionRunsMillis:失效检查线程运行时间间隔,如果小于等于0,不会启动检查线程,默认-1
testOnBorrow:取得对象时是否进行验证,检查对象是否有效,默认为false
testOnReturn:返回对象时是否进行验证,检查对象是否有效,默认为false
testWhileIdle:空闲时是否进行验证,检查对象是否有效,默认为false
initialSize:初始化线程数
一。通过直接配置数据库属性连接
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.DataSourceConnectionFactory; class dbcputils { private static dbcputils dbcp=null; private BasicDataSource dbs=null; private DataSourceConnectionFactory dscf=null; public dbcputils(){ if(dbs==null){ dbs=new BasicDataSource(); dbs.setUsername("root"); dbs.setPassword("jarrem"); dbs.setUrl("jdbc:mysql://127.0.0.1:3306/tang"); dbs.setDriverClassName("com.mysql.jdbc.Driver"); dbs.setMaxActive(11); dbs.setInitialSize(10); dbs.setMaxIdle(5); dbs.setMinIdle(3); dscf=new DataSourceConnectionFactory(dbs); System.out.println("初始化实例了"); } } public dbcputils getInstance(){ if(dbcp==null){ dbcp=new dbcputils(); System.out.println("getInstance初始化实例了"); } return dbcp; } public Connection getConnection(){ Connection connection=null; try { connection = dscf.createConnection(); } catch (SQLException e) { // TODO Auto-generated catch block System.out.println("获取Connection失败!"); e.printStackTrace(); } return connection; } public static void main(String[] args){ dbcputils dp=new dbcputils(); long begin=System.currentTimeMillis(); List list=new ArrayList(); Connection connection = dp.getInstance().getConnection(); try { PreparedStatement ps = connection.prepareStatement("select * from user;"); ResultSet result = ps.executeQuery(); while(result.next()){ list.add(result); System.out.println(result.getString(1)); } for(Object o:list){ System.out.println(list.toString()); } connection.close(); } catch (SQLException e1) { e1.printStackTrace(); } long end=System.currentTimeMillis(); System.out.println(end-begin); } }
二.通过properties文件配置数据库
import java.io.FileInputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; import org.apache.commons.dbcp.DataSourceConnectionFactory; public class dbcpPool { private BasicDataSource bds=null; private DataSourceConnectionFactory dscf=null; private Properties properties=null; private DataSource DataSource=null; private static Connection connection=null; public dbcpPool(){ if(bds==null){ bds=new BasicDataSource(); properties=new Properties(); FileInputStream in; try { //我这里使用的是绝对路径,根据自己文件位置修改 in = new FileInputStream("C:\Users\lenovo\Workspaces\MyEclipse Professional 2014\DatabaseConnectionPool\src\com\tang\dbpcPool2\link.properties"); properties.load(in); DataSource = BasicDataSourceFactory.createDataSource(properties); connection = DataSource.getConnection(); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String args[]){ try { new dbcpPool(); PreparedStatement statement = connection.prepareStatement("select * from user;"); ResultSet result = statement.executeQuery(); while(result.next()){ System.out.println(result.getString(1)+" "+result.getString(2)); } } catch (SQLException e) { e.printStackTrace(); } } }