文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Apache Dbutil的使用

Apache Dbutil的使用

时间:2010-07-27  来源:luochaoboy

      Apache 的Dbutil是一个非常好用的工具,尤其在它可以把JDBC中SELECT语句的结果集rs转化为任何我们想要的数据类型。以下为示例:   public XsModel getByID(String xh)throws SQLException{
 XsModel model=null;
 ResultSet rs=null;
 StringBuffer sql=new StringBuffer();
 try{
  //获取SQL
  sql.append("select * from xsxxb where xh='");
  sql.append(xh);
  sql.append("'");
  
  rs = bean.excutequery(sql.toString());
  if(rs.next())
  {
   
   model=(XsModel) this.getModelBySql(sql.toString(),XsModel.class);//通过Dbutil把r
                                                                       s变                                                                        为Model   
  }
  
 }catch(SQLException e){
  e.printStackTrace();
 
 }finally {
  if(rs!=null)
   rs.close();
  rs=null;
 }
 
 return model;
}
  /*通过Apache dbutil直接得到model
注意参数中的beanClass是你想要转换的类型名
注意query的参数中有个ResultsetHandler,ResultsetHandler是个接口,实现此接口的方法很多(详见commons DbUtils 1.2 API)在调用时用其中一种即可
在以下函数中使用了BeanHandler
在getByID函数中调用了此函数
*/
public Object getModelBySql(String sql,Class beanClass) throws SQLException
{                       
 Object model=null;
 QueryRunner qr = new QueryRunner();
 model=qr.query(bean.getConnection(),sql,new BeanHandler(beanClass));
 //model=qr.query(bean.getConnection(),sql,new ArrayHandler());
 return model; 
}
  程序中全部代码:
本例中用的是oracle数据库,数据库中有表xxssb,所含字段Xmodel.java中都有。
文件: DBdealwith.rar
大小: 2487KB
下载: 下载
 
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载