zoukankan      html  css  js  c++  java
  • 如何获取jdbc获取的ArrayList集合

    多个字段数据可用Map

    if(dept!=null && !"".equals(dept)){
            List list = this.getDeptNameByDeptCode(dept);
            if(list!=null && !list.isEmpty()){
                Map m=(Map)list.get(0);
                deptName =  (String) m.get("deptName");
                /*deptName = (String) list.get(0);*/
            }
        }
        public List getDeptNameByDeptCode(String deptCode){
            Connection conn = JdbcManager.getInstance().openCurConnection();
            PreparedStatement pstmt = null;
            ResultSet res = null;
            List list = new ArrayList();
            try {
                StringBuffer hql = new StringBuffer();
                hql.append("select d.dept_name as deptName from T_SYSTEM_DEPT d where 1=1");
                if (deptCode != null && !deptCode.equals("")){
                    hql.append(" and d.dept_code ='"+deptCode+"'");
                }
                pstmt = conn.prepareStatement(hql.toString());
                res = pstmt.executeQuery();
                while (res.next()) {
                    Map map = new HashMap();
                    map.put("deptName", res.getString("deptName"));
                    list.add(map);
                    
                    /*String deptName = res.getString("deptName");
                    list.add(deptName);*/
                }
            } catch (Exception e) {
                throw new RuntimeException("获取T_SYSTEM_DEPT错误!!", e);
            } finally {
                JdbcManager.closeQuietly(res);
                JdbcManager.closeQuietly(pstmt);
                JdbcManager.closeQuietly(conn);
            }
            return list;
        }

    单个字段数据可以直接获取

    if(dept!=null && !"".equals(dept)){
       List list = this.getDeptNameByDeptCode(dept);
       if(list!=null && !list.isEmpty()){
       /*Map m=(Map)list.get(0);
       deptName =  (String) m.get("deptName");*/
       deptName = (String) list.get(0);
      }
    }
    public List getDeptNameByDeptCode(String deptCode){
            Connection conn = JdbcManager.getInstance().openCurConnection();
            PreparedStatement pstmt = null;
            ResultSet res = null;
            List list = new ArrayList();
            try {
                StringBuffer hql = new StringBuffer();
                hql.append("select d.dept_name as deptName from T_SYSTEM_DEPT d where 1=1");
                if (deptCode != null && !deptCode.equals("")){
                    hql.append(" and d.dept_code ='"+deptCode+"'");
                }
                pstmt = conn.prepareStatement(hql.toString());
                res = pstmt.executeQuery();
                while (res.next()) {
                    /*Map map = new HashMap();
                    map.put("deptName", res.getString("deptName"));
                    list.add(map);*/
                    
                    String deptName = res.getString("deptName");
                    list.add(deptName);
                }
            } catch (Exception e) {
                throw new RuntimeException("获取T_SYSTEM_DEPT错误!!", e);
            } finally {
                JdbcManager.closeQuietly(res);
                JdbcManager.closeQuietly(pstmt);
                JdbcManager.closeQuietly(conn);
            }
            return list;
        }
  • 相关阅读:
    C语言探索之旅 | 第二部分第十一课:练习题和习作
    C语言探索之旅 | 第二部分第十课: 实战"悬挂小人"游戏答案
    C语言探索之旅 | 第二部分第九课: 实战"悬挂小人"游戏
    C语言探索之旅 | 第二部分第八课:动态分配
    C语言探索之旅 | 第二部分第七课:文件读写
    最近迫切应学的编程语言
    C语言探索之旅 | 第二部分第五课:预处理
    封装axios方法之一
    react前置路由守卫
    React Router 4.0 实现路由守卫
  • 原文地址:https://www.cnblogs.com/Lemon-ZYJ/p/13489092.html
Copyright © 2011-2022 走看看