如何获取jdbc获取的ArrayList集合

Posted 码代码的柯小二

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何获取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;
    }

 

以上是关于如何获取jdbc获取的ArrayList集合的主要内容,如果未能解决你的问题,请参考以下文章

使用反射机制,获取 ArrayList 的容量大小

Java基础练习题10--[使用ArrayList获取10个随机数;String类型的ArrayList集合,去除ArrayList中的重复元素,模拟注册,登录存入集合]

如何从 Firestore 文档中获取自定义对象的 ArrayList? [复制]

怎么获取数组里对象的某一项属性值

获取嵌套集合里的值

jsp页面前台用jdbc如何获取sql数据库里的数据。