码迷,mamicode.com
首页 > 数据库 > 详细

如何获取jdbc获取的ArrayList集合

时间:2020-08-12 15:53:52      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:col   rest   resultset   where   exe   exec   list()   pst   enc   

多个字段数据可用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;
    }

 

如何获取jdbc获取的ArrayList集合

标签:col   rest   resultset   where   exe   exec   list()   pst   enc   

原文地址:https://www.cnblogs.com/Lemon-ZYJ/p/13489092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!