dbutils的update方法与query方法的内部实现
来源:互联网 发布:javascript 电子书 编辑:程序博客网 时间:2024/06/08 14:24
首先假设有一个连接池 comboPooledDataSource
Class Dbutils{ private DataSource dataSoure; public Dbutils(DataSource dataSource) { this.dataSource=dataSource; } private void InitParams(PreparedStatement preparedStatement,Object[] params) throws SQLException { for(int i=0;i<params.length;i++) { preparedStatement.setObject(i+1, params[i]); } } public int update(String sql,Object[] params) { Connection con = null; PreparedStatement pre = null; try{ con=dataSource.getConnection(); pre=con.preparedStatement(); InitParams(pre,params); return preparedStatement.executeUpdate(); } catch(Exception e) { // TODO: handle exception } finally { try { if(preparedStatement!=null)preparedStatement.close(); if(connection!=null) connection.close(); } catch (Exception e) { throw new RuntimeException(e); } return -1; } public <T> T querry(String sql,RsHandler<T> rh,Object[] params) { Connection con = null; PreparedStatement pre = null; ResultSet rs=null; try{ con=dataSource.getConnection(); pre=con.preparedStatement(); InitParams(pre,params); rs=pre.executeQuery(); if(rs.next()) return rh.handler(rs); } catch(Exception e) { // TODO: handle exception } finally { try { if(rs!=null) rs.closer(); if(preparedStatement!=null)preparedStatement.close(); if(connection!=null) connection.close(); } catch (Exception e) { throw new RuntimeException(e); } return null; }interface RsHandler<T> { public T Handle(ResultSet resultSet) throws SQLException; }}
/** 调用query方法*/public void fun1() throws SQLException { Dbutils dbutils=new Dbutils(comboPooledDataSource); String sql="select * from stu where sid=?"; Object[] params={1002}; RsHandler<Stu> rh = new RsHandler<Stu>(){ public Stu Handle(ResultSet resultSet) throws SQLException { Stu stu = new Stu(); //从resultSet里获取值 .............. return stu; }; Stu stu = dbutils.querry(sql,rh,params) System.out.println(stu);}
在真正的dbutils其实有更简单的实现,就是调用BeanHandler这个实现类
public void fun2() throws SQLException{ QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource()); String sql="select * from stu where sid = ?"; Object[] param={1002}; Stu stu=queryRunner.query(sql, new BeanHandler<Stu>(Stu.class), param); System.out.println(stu);}
阅读全文
0 0
- dbutils的update方法与query方法的内部实现
- dbutils的下载方法
- OC中setter方法与getter方法的内部实现
- ContentResolver的四个常用方法:delete().query(),update(),insert()
- SQLiteDatabase类的query(),insert(),update(),delete()方法参数说明
- SSH的update实现方法
- xUtils在开发过程中DbUtils的使用(save,update方法)
- Hibernate中的query.list()与query.iterator()方法的区别
- sqlalchmey的query方法
- b3log的query方法
- hibernate-Query的list方法与iterator方法的区别
- xUtils中的DbUtils内部常用方法分类
- update的三种实现方法
- ios如何实现内部购买的方法
- String类的一些内部实现方法
- String的subString()方法实现内部机制
- 实现抽象类内部的抽象方法
- String的substring()方法内部实现
- 窗口与控件关联的方式:
- JVM(三)—垃圾回收算法及垃圾收集器
- Robberies
- ListView封装
- H
- dbutils的update方法与query方法的内部实现
- Moving Averages(7):Conclusions
- Java学习笔记之JavaWeb-EL表达式
- 持续集成主流工具介绍
- Server Tomcat v7.0 Server at localhost failed to start.错误可能原因
- MFC Socket网络编程之TCP服务器端
- 写一个函数判断机子大小端的问题
- HashTable-----138. Copy List with Random Pointer
- Word中如何去掉换行符