1 ##数据库连接池 2 3 >1. 数据库的连接对象创建工作,比较消耗性能。 4 5 >2.一开始现在内存中开辟一块空间(集合) , 一开先往池子里面放置 多个连接对象。 后面需要连接的话,直接从池子里面去。不要去自己创建连接了。 使用完毕, 要记得归还连接。确保连接对象能循环利用。 6 7 ![icon](img/img11.png) 8 9 10 ###自定义数据库连接池 11 12 13 * 代码实现 14 15 * 出现的问题: 16 17 1. 需要额外记住 addBack方法 18 19 2. 单例。 20 21 3. 无法面向接口编程。 22 23 UserDao dao = new UserDaoImpl(); 24 dao.insert(); 25 26 27 DataSource dataSource = new MyDataSource(); 28 29 因为接口里面没有定义addBack方法。 30 31 4. 怎么解决? 以addBack 为切入点。 32 33 ###解决自定义数据库连接池出现的问题。 34 35 > 由于多了一个addBack 方法,所以使用这个连接池的地方,需要额外记住这个方法,并且还不能面向接口编程。 36 37 > 我们打算修改接口中的那个close方法。 原来的Connection对象的close方法,是真的关闭连接。 38 > 打算修改这个close方法,以后在调用close, 并不是真的关闭,而是归还连接对象。 39 40 41 ###如何扩展某一个方法? 42 43 > 原有的方法逻辑,不是我们想要的。 想修改自己的逻辑 44 45 1. 直接改源码 无法实现。 46 47 2. 继承, 必须得知道这个接口的具体实现是谁。 48 49 3. 使用装饰者模式。 50 51 52 53 ##开源连接池 54 55 #### DBCP 56 57 58 1. 导入jar文件 59 60 2. 不使用配置文件: 61 62 63 public void testDBCP01(){ 64 65 66 Connection conn = null; 67 PreparedStatement ps = null; 68 try { 69 70 //1. 构建数据源对象 71 BasicDataSource dataSource = new BasicDataSource(); 72 //连的是什么类型的数据库, 访问的是哪个数据库 , 用户名, 密码。。 73 //jdbc:mysql://localhost/bank 主协议:子协议 ://本地/数据库 74 dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 75 dataSource.setUrl("jdbc:mysql://localhost/bank"); 76 dataSource.setUsername("root"); 77 dataSource.setPassword("root"); 78 79 80 //2. 得到连接对象 81 conn = dataSource.getConnection(); 82 String sql = "insert into account values(null , ? , ?)"; 83 ps = conn.prepareStatement(sql); 84 ps.setString(1, "admin"); 85 ps.setInt(2, 1000); 86 87 ps.executeUpdate(); 88 89 } catch (SQLException e) { 90 e.printStackTrace(); 91 }finally { 92 JDBCUtil.release(conn, ps); 93 } 94 95 } 96 97 2. 使用配置文件方式: 98 99 100 Connection conn = null; 101 PreparedStatement ps = null; 102 try { 103 BasicDataSourceFactory factory = new BasicDataSourceFactory(); 104 Properties properties = new Properties(); 105 InputStream is = new FileInputStream("src//dbcpconfig.properties"); 106 properties.load(is); 107 DataSource dataSource = factory.createDataSource(properties); 108 109 //2. 得到连接对象 110 conn = dataSource.getConnection(); 111 String sql = "insert into account values(null , ? , ?)"; 112 ps = conn.prepareStatement(sql); 113 ps.setString(1, "liangchaowei"); 114 ps.setInt(2, 100); 115 116 ps.executeUpdate(); 117 118 } catch (Exception e) { 119 e.printStackTrace(); 120 }finally { 121 JDBCUtil.release(conn, ps); 122 } 123 124 125 126 127 * C3P0 128 129 > 拷贝jar文件 到 lib目录 130 131 ###不使用配置文件方式 132 133 134 Connection conn = null; 135 PreparedStatement ps = null; 136 try { 137 //1. 创建datasource 138 ComboPooledDataSource dataSource = new ComboPooledDataSource(); 139 //2. 设置连接数据的信息 140 dataSource.setDriverClass("com.mysql.jdbc.Driver"); 141 142 //忘记了---> 去以前的代码 ---> jdbc的文档 143 dataSource.setJdbcUrl("jdbc:mysql://localhost/bank"); 144 dataSource.setUser("root"); 145 dataSource.setPassword("root"); 146 147 //2. 得到连接对象 148 conn = dataSource.getConnection(); 149 String sql = "insert into account values(null , ? , ?)"; 150 ps = conn.prepareStatement(sql); 151 ps.setString(1, "admi234n"); 152 ps.setInt(2, 103200); 153 154 ps.executeUpdate(); 155 156 } catch (Exception e) { 157 e.printStackTrace(); 158 }finally { 159 JDBCUtil.release(conn, ps); 160 } 161 162 163 ###使用配置文件方式 164 165 166 //默认会找 xml 中的 default-config 分支。 167 ComboPooledDataSource dataSource = new ComboPooledDataSource(); 168 //2. 设置连接数据的信息 169 dataSource.setDriverClass("com.mysql.jdbc.Driver"); 170 171 172 //忘记了---> 去以前的代码 ---> jdbc的文档 173 dataSource.setJdbcUrl("jdbc:mysql://localhost/bank"); 174 dataSource.setUser("root"); 175 dataSource.setPassword("root"); 176 177 //2. 得到连接对象 178 conn = dataSource.getConnection(); 179 String sql = "insert into account values(null , ? , ?)"; 180 ps = conn.prepareStatement(sql); 181 ps.setString(1, "admi234n"); 182 ps.setInt(2, 103200); 183 184 185 186 ##DBUtils 187 188 ###增删改 189 190 191 //dbutils 只是帮我们简化了CRUD 的代码, 但是连接的创建以及获取工作。 不在他的考虑范围 192 QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource()); 193 194 195 //增加 196 //queryRunner.update("insert into account values (null , ? , ? )", "aa" ,1000); 197 198 //删除 199 //queryRunner.update("delete from account where id = ?", 5); 200 201 //更新 202 //queryRunner.update("update account set money = ? where id = ?", 10000000 , 6); 203 204 ###查询 205 206 1. 直接new接口的匿名实现类 207 208 209 QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource()); 210 211 212 Account account = queryRunner.query("select * from account where id = ?", new ResultSetHandler<Account>(){ 213 214 @Override 215 public Account handle(ResultSet rs) throws SQLException { 216 Account account = new Account(); 217 while(rs.next()){ 218 String name = rs.getString("name"); 219 int money = rs.getInt("money"); 220 221 account.setName(name); 222 account.setMoney(money); 223 } 224 return account; 225 } 226 227 }, 6); 228 229 System.out.println(account.toString()); 230 231 2. 直接使用框架已经写好的实现类。 232 233 234 * 查询单个对象 235 236 QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource()); 237 //查询单个对象 238 Account account = queryRunner.query("select * from account where id = ?", 239 new BeanHandler<Account>(Account.class), 8); 240 241 242 * 查询多个对象 243 244 QueryRunner queryRunner = new QueryRunner(new ComboPooledDataSource()); 245 List<Account> list = queryRunner.query("select * from account ", 246 new BeanListHandler<Account>(Account.class)); 247 248 ###ResultSetHandler 常用的实现类 249 以下两个是使用频率最高的 250 251 BeanHandler, 查询到的单个数据封装成一个对象 252 BeanListHandler, 查询到的多个数据封装 成一个List<对象> 253 254 ------------------------------------------ 255 256 ArrayHandler, 查询到的单个数据封装成一个数组 257 ArrayListHandler, 查询到的多个数据封装成一个集合 ,集合里面的元素是数组。 258 259 260 261 MapHandler, 查询到的单个数据封装成一个map 262 MapListHandler,查询到的多个数据封装成一个集合 ,集合里面的元素是map。 263 264 265 266 267 ColumnListHandler 268 KeyedHandler 269 ScalarHandler
事务数据库连接池的几种用法
猜你喜欢
转载自blog.csdn.net/qq_37706228/article/details/81294755
今日推荐
周排行