建立CONN太多的解决方法--shopping

  1. publicstaticvoidgetCategories(List<Category>list,intid)
  2. {
  3. Connectionconn=null;
  4. try
  5. {
  6. conn=DB.getConn();
  7. getCategories(conn,list,id);
  8. }finally
  9. {
  10. DB.closeConn(conn);
  11. }
  12. }
  13. privatestaticvoidgetCategories(Connectionconn,List<Category>list,intid)
  14. {
  15. ResultSetrs=null;
  16. try
  17. {
  18. Stringsql="select*fromcategorywherepid="+id;
  19. rs=DB.executeQuery(conn,sql);
  20. while(rs.next())
  21. {
  22. Categoryc=newCategory();
  23. c.setId(rs.getInt("id"));
  24. c.setName(rs.getString("name"));
  25. c.setDescr(rs.getString("descr"));
  26. c.setLeaf(rs.getBoolean("isleaf")?true:false);
  27. c.setPid(rs.getInt("pid"));
  28. c.setGrade(rs.getInt("grade"));
  29. list.add(c);
  30. if(c.isLeaf())
  31. {
  32. getCategories(conn,list,c.getId());
  33. }
  34. }
  35. }catch(SQLExceptione)
  36. {
  37. e.printStackTrace();
  38. }finally
  39. {
  40. DB.closeRs(rs);
  41. }
  42. }
  43. 把CONN也作为其中的一个参数传过去就可以避免建立CONN太多的问题了,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值