网站的风格设计,产品宣传册,品牌营销包括哪些内容,网站页面制作视频/** ####################################数据库的连接池学习################################# * * * #####数据库连接池
1. 数据库的连接对象创建工作#xff0c;比较消耗性能。 2.一开始现在内存中开辟一块空间#xff08;集合#xff09; #xff0c; 一开… /** ####################################数据库的连接池学习################################# * * * #####数据库连接池
1. 数据库的连接对象创建工作比较消耗性能。 2.一开始现在内存中开辟一块空间集合 一开先往池子里面放置 多个连接对象。
后面需要连接的话直接从池子里面去。不要去自己创建连接了。
使用完毕 要记得归还连接。确保连接对象能循环利用。
###自定义数据库连接池 * 代码实现* 出现的问题1. 需要额外记住 addBack方法2. 单例。3. 无法面向接口编程。 UserDao dao new UserDaoImpl();dao.insert();DataSource dataSource new MyDataSource();因为接口里面没有定义addBack方法。 4. 怎么解决? 以addBack 为切入点。###解决自定义数据库连接池出现的问题。 由于多了一个addBack 方法所以使用这个连接池的地方需要额外记住这个方法并且还不能面向接口编程。 我们打算修改接口中的那个close方法。 原来的Connection对象的close方法是真的关闭连接。 打算修改这个close方法以后在调用close 并不是真的关闭而是归还连接对象。* * * ####开源连接池#### DBCP
1. 导入jar文件
2. 不使用配置文件* public void testDBCP01(){Connection conn null;PreparedStatement ps null;try {//1. 构建数据源对象BasicDataSource dataSource new BasicDataSource();//连的是什么类型的数据库 访问的是哪个数据库 用户名 密码。。//jdbc:mysql://localhost/bank 主协议子协议 ://本地/数据库dataSource.setDriverClassName(com.mysql.jdbc.Driver);dataSource.setUrl(jdbc:mysql://localhost/bank);dataSource.setUsername(root);dataSource.setPassword(root);//2. 得到连接对象conn dataSource.getConnection();String sql insert into account values(null , ? , ?);ps conn.prepareStatement(sql);ps.setString(1, admin);ps.setInt(2, 1000);ps.executeUpdate();} catch (SQLException e) {e.printStackTrace();}finally {JDBCUtil.release(conn, ps);}}* * * ####使用配置文件方式Connection conn null;PreparedStatement ps null;try {BasicDataSourceFactory factory new BasicDataSourceFactory();Properties properties new Properties();InputStream is new FileInputStream(src//dbcpconfig.properties);properties.load(is);DataSource dataSource factory.createDataSource(properties);//2. 得到连接对象conn dataSource.getConnection();String sql insert into account values(null , ? , ?);ps conn.prepareStatement(sql);ps.setString(1, liangchaowei);ps.setInt(2, 100);ps.executeUpdate();} catch (Exception e) {e.printStackTrace();}finally {JDBCUtil.release(conn, ps);}* *###################配置文件模板##################################################*#连接设置
driverClassNamecom.mysql.jdbc.Driver
urljdbc:mysql://localhost:3306/bank
usernameroot
passwordroot#!-- 初始化连接 --
initialSize10#最大连接数量
maxActive50#!-- 最大空闲连接 --
maxIdle20#!-- 最小空闲连接 --
minIdle5#!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 --
maxWait60000#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样[属性名property;]
#注意user 与 password 两个属性会被明确地传递因此这里不需要包含他们。
connectionPropertiesuseUnicodetrue;characterEncodinggbk#指定由连接池所创建的连接的自动提交auto-commit状态。
defaultAutoCommittrue#driver default 指定由连接池所创建的连接的事务级别TransactionIsolation。
#可用值为下列之一详情可见javadoc。NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolationREAD_UNCOMMITTED########################################################################################* * ####################C3P0知识点##################################################################C3P0拷贝jar文件 到 lib目录###不使用配置文件方式Connection conn null;PreparedStatement ps null;try {//1. 创建datasourceComboPooledDataSource dataSource new ComboPooledDataSource();//2. 设置连接数据的信息dataSource.setDriverClass(com.mysql.jdbc.Driver);//忘记了--- 去以前的代码 --- jdbc的文档dataSource.setJdbcUrl(jdbc:mysql://localhost/bank);dataSource.setUser(root);dataSource.setPassword(root);//2. 得到连接对象conn dataSource.getConnection();String sql insert into account values(null , ? , ?);ps conn.prepareStatement(sql);ps.setString(1, admi234n);ps.setInt(2, 103200);ps.executeUpdate();} catch (Exception e) {e.printStackTrace();}finally {JDBCUtil.release(conn, ps);}######使用配置文件方式#####c3p0-config配置文件xml文件。名字不可以改变?xml version1.0 encodingUTF-8?
c3p0-config!-- default-config 默认的配置 --default-configproperty namedriverClasscom.mysql.jdbc.Driver/propertyproperty namejdbcUrljdbc:mysql://localhost/bank/propertyproperty nameuserroot/propertyproperty namepasswordroot/propertyproperty nameinitialPoolSize10/propertyproperty namemaxIdleTime30/propertyproperty namemaxPoolSize100/propertyproperty nameminPoolSize10/propertyproperty namemaxStatements200/property/default-config!-- This app is massive! --named-config nameoracle property nameacquireIncrement50/propertyproperty nameinitialPoolSize100/propertyproperty nameminPoolSize50/propertyproperty namemaxPoolSize1000/property!-- intergalactoApp adopts a different approach to configuring statement caching --property namemaxStatements0/property property namemaxStatementsPerConnection5/property!-- hes important, but theres only one of him --user-overrides usermaster-of-the-universe property nameacquireIncrement1/propertyproperty nameinitialPoolSize1/propertyproperty nameminPoolSize1/propertyproperty namemaxPoolSize5/propertyproperty namemaxStatementsPerConnection50/property/user-overrides/named-config/c3p0-config#####代码部分//默认会找 xml 中的 default-config 分支。
public class C3P0Demo02 {Testpublic void testC3P0(){Connection conn null;PreparedStatement ps null;try {//就new了一个对象。在这种情况下c3p0会直接找到c3p0-config.xml文件//并且在c3p0-config.xml文件中默认的找到 default-config配置ComboPooledDataSource dataSource new ComboPooledDataSource();//ComboPooledDataSource dataSource new ComboPooledDataSource(oracle);//找到c3p0-config.xml文件中默认的找到named-config nameoracle的配置//2. 得到连接对象conn dataSource.getConnection();String sql insert into account values(null , ? , ?);ps conn.prepareStatement(sql);ps.setString(1, wangwu2);ps.setInt(2, 2600);ps.executeUpdate();} catch (Exception e) {e.printStackTrace();}finally {JDBCUtil.release(conn, ps);}}
}###########################################################################################* ###########DBUtils###增删改
//dbutils 只是帮我们简化了CRUD 的代码 但是连接的创建以及获取工作。 不在他的考虑范围QueryRunner主要是这个类QueryRunner queryRunner new QueryRunner(new ComboPooledDataSource());//增加
//queryRunner.update(insert into account values (null , ? , ? ), aa ,1000);//删除
//queryRunner.update(delete from account where id ?, 5);//更新
//queryRunner.update(update account set money ? where id ?, 10000000 , 6);* * * * * * ######查询
1. 直接new接口的匿名实现类QueryRunner queryRunner new QueryRunner(new ComboPooledDataSource());Account account queryRunner.query(select * from account where id ?, new ResultSetHandlerAccount(){Overridepublic Account handle(ResultSet rs) throws SQLException {Account account new Account();while(rs.next()){String name rs.getString(name);int money rs.getInt(money);account.setName(name);account.setMoney(money);}return account;}}, 6);System.out.println(account.toString());2. 直接使用框架已经写好的实现类。
* 查询单个对象QueryRunner queryRunner new QueryRunner(new ComboPooledDataSource());//查询单个对象Account account queryRunner.query(select * from account where id ?, new BeanHandlerAccount(Account.class), 8);* 查询多个对象QueryRunner queryRunner new QueryRunner(new ComboPooledDataSource());ListAccount list queryRunner.query(select * from account ,new BeanListHandlerAccount(Account.class));######ResultSetHandler 常用的实现类(重点)
以下两个是使用频率最高的BeanHandler, 查询到的单个数据封装成一个对象BeanListHandler, 查询到的多个数据封装 成一个List对象------------------------------------------ArrayHandler, 查询到的单个数据封装成一个数组ArrayListHandler, 查询到的多个数据封装成一个集合 集合里面的元素是数组。 MapHandler, 查询到的单个数据封装成一个mapMapListHandler,查询到的多个数据封装成一个集合 集合里面的元素是map。 ColumnListHandler
KeyedHandler
ScalarHandler##数据连接池* DBCP不使用配置使用配置* C3P0不使用配置使用配置 必须掌握* 自定义连接池 装饰者模式##DBUtils 简化了我们的CRUD 里面定义了通用的CRUD方法。 queryRunner.update();queryRunner.query* * * * * */ 转载于:https://www.cnblogs.com/byczyz/p/11343575.html