DBUtil 用法

 DbUtils类

ResultSetHandler 接口 

MapListHandler 类  (实现ResultSetHandler 接口)把从数据库中查询出的记录 都 放到List  集合当中, List集合中每一个对象都是Map类型,可以根据这条记录的字段名读出相对应的值

BeanListHandler 类  (实现ResultSetHandler 接口)把从数据库中的记录 放到List集合中 ,List集合中每一个对象都是一个JavaBean类型的对象,可以根据get 方法得到值

QreryRunner类   值行SQL语名,其中一个参数为以上两种类型的对象

 

 

 

 

Java代码   收藏代码
  1. import java.sql.Connection;  
  2. import java.sql.SQLException;  
  3. import java.util.List;  
  4. import javax.naming.Context;  
  5. import javax.naming.InitialContext;  
  6. import javax.naming.NamingException;  
  7. import javax.sql.DataSource;  
  8. import org.apache.commons.dbutils.QueryRunner;  
  9. import org.apache.commons.dbutils.ResultSetHandler;  
  10. /** 
  11.  * 该类主要完成对数据库的操作 
  12.  *  
  13.  */  
  14. public class SqlUtil {  
  15.     private static String datasoucename ="java:comp/env/jdbc/exam";  
  16.     private  DataSource da;  
  17.     private static sqlUtil sqlutil = null;  
  18.   
  19.     public static sqlUtil newInstance()  
  20.     {  
  21.         if(sqlutil == null) {  
  22.             sqlutil = new sqlUtil();  
  23.         }  
  24.         return sqlutil;  
  25.     }  
  26.       
  27.       
  28.     public Connection getConnection() throws SQLException  
  29.     {  
  30.         synchronized (da) {  
  31.             return da.getConnection();  
  32.         }  
  33.     }  
  34.       
  35.     /** 
  36.      * 构造方法,设置数据源 
  37.      * @param datasoucename 
  38.      */  
  39.     private sqlUtil() {  
  40.         init();  
  41.     }  
  42.   
  43.       
  44.     /** 
  45.      * 初始化数据源的方法 
  46.      */  
  47.     private void init() {  
  48.         try {  
  49.             Context ct = new InitialContext();  
  50.             this.da = (DataSource) ct.lookup(datasoucename);  
  51.             ct.close();  
  52.         } catch (NamingException e) {  
  53.             e.printStackTrace();  
  54.         }  
  55.   
  56.     }  
  57.   
  58.     /** 
  59.      * 对数据增删改的方法,需要一个object的数组,数据是sql语句的占位符 
  60.      * @param sql 
  61.      * @param pring 
  62.      * @return 
  63.      */  
  64.     public boolean update(String sql, Object pring[]) {  
  65.         QueryRunner qu = new QueryRunner();  
  66.         int i = 0;  
  67.         boolean flag = false;  
  68.         try {  
  69.             i = qu.update(getConnection(),sql, pring);  
  70.             if(i>=0){  
  71.                 flag = true;  
  72.             }  
  73.         } catch (SQLException e) {  
  74.             e.printStackTrace();  
  75.         }  
  76.   
  77.         return flag;  
  78.     }  
  79.   
  80.     /** 
  81.      * 对数据查找的方法,需要一个objece的数组,数据是sql语句的占位符,ResultSetHandler参数是你要把查询出的数据转换的类型 
  82.      * 可以是一个javabean   (如:new BeanListHandler (JavaBean.class)或:new MapListHandler ) 
  83.      * @param sql 
  84.      * @param pring  参数数组可以为空 
  85.      * @param rsh 
  86.      * @return 
  87.      */  
  88.     public List query(String sql, Object pring[], ResultSetHandler rsh) {  
  89.         QueryRunner qu = new QueryRunner();  
  90.         List result = null;  
  91.         try {  
  92.             result = (List) qu.query(getConnection(),sql, pring, rsh);  
  93.         } catch (SQLException e) {  
  94.             // TODO Auto-generated catch block  
  95.             e.printStackTrace();  
  96.         }  
  97.         return result;  
  98.     }  
  99. }  

 

 

调用

 

 

Java代码   收藏代码
  1. /** 
  2.      * 删除 
  3.      * @return boolean 
  4.      * @param ItemPool的Id 
  5.      */  
  6.     public boolean deteItemPool(String ipId) {  
  7.         String sql1 = "delete from ItemPool where ipId =? ";  
  8. //指定所需要的参数 数组  
  9.         Object[] pring = {ipId};  
  10. //执行更新  
  11.         boolean result = sql.update(sql1, pring);  
  12.         return result;  
  13.     }  

 

Java代码   收藏代码
  1. /** 
  2.      * 根据题库Id查找题库的方法 
  3.      */  
  4.     public ItemPool searchItemPoolById(String ipId) {  
  5.         String sql1 = "select * from ItemPool where ipId=?";  
  6. //指定参数数组  
  7.         Object[] pring = {ipId};  
  8.   
  9.           
  10.   
  11.   
  12.   
  13.   
  14.   
  15. List list = sql.query(sql1, pring, new BeanListHandler(ItemPool.class));  
  16.         ItemPool itempool=(ItemPool)list.get(0);  
  17.         return itempool;  
  18.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值