java中怎么吧jdbc查询的resultset对象转为json

java 新手
不知道怎么将查询结果转为json对象
使用的是json-simple 库

阅读 10.9k
1 个回答

一条记录 建议先封装成map 对象 多条几率 封装成 list<map<string,object>>

protected List<Map<String, Object>> fullToList(ResultSet rs) {
    ArrayList result = new ArrayList();
    try {
        int iColumn = rs.getMetaData().getColumnCount();
        while (rs.next()) {
            Map<String, Object> map = new LinkedHashMap<String, Object>();
            for (int i = 1; i <= iColumn; i++) {
                String tmpkey = rs.getMetaData().getColumnName(i);// 通过索引找到行的名字

                String[] tmps = tmpkey.split("_");// 分割所有含"_"字符,返回数组
                String key = "";
                for (int j = 0; j < tmps.length; j++) {
                    if (j == 0) {
                        key += tmps[j].toLowerCase();// 转换为小写
                    } else {
                        key += tmps[j].substring(0, 1).toUpperCase()
                                + tmps[j].substring(1).toLowerCase();
                    }
                }

                Object val = null;
                // switch行类型
                switch (rs.getMetaData().getColumnType(i)) {
                case Types.DATE:
                    val = rs.getDate(i);
                    break;
                case Types.DOUBLE:
                    if(rs.getObject(i)==null)
                        val=null;
                    else
                        val = rs.getDouble(i);
                    break;
                case Types.NUMERIC:
                    if(rs.getObject(i)==null)
                        val=null;
                    else
                        val = rs.getDouble(i);
                    break;
                case Types.DECIMAL:
                    if(rs.getObject(i)==null)
                        val=null;
                    else
                        val = rs.getDouble(i);
                    break;
                case Types.INTEGER:
                    if(rs.getObject(i)==null)
                        val=null;
                    else
                        val = rs.getInt(i);
                    break;
                default:
                    val = rs.getString(i);
                    break;
                }

                if (null != val) {
                    // map找找不到指定键便设置个
                    if (!map.containsKey(key)) {
                        map.put(key, val);
                    }
                }
            }
            result.add(map);
        }
    } catch (Exception e) {
        log.error(e.getMessage());
    } finally {
        try {
            if (rs != null)
                rs.close();
            rs = null;
        } catch (SQLException e) {
            log.error(e.getMessage());
        }
    }
    return result;
}

protected Map<String, Object> fullToMap(ResultSet rs) {
    Map<String, Object> map = new TreeMap<String, Object>();
    try {
        int iColumn = rs.getMetaData().getColumnCount();
        while (rs.next()) {
            for (int i = 1; i <= iColumn; i++) {
                String tmpkey = rs.getMetaData().getColumnName(i);

                String[] tmps = tmpkey.split("_");
                String key = "";
                for (int j = 0; j < tmps.length; j++) {
                    if (j == 0) {
                        key += tmps[j].toLowerCase();
                    } else {
                        key += tmps[j].substring(0, 1).toUpperCase()
                                + tmps[j].substring(1).toLowerCase();
                    }
                }

                Object val = null;
                switch (rs.getMetaData().getColumnType(i)) {
                case Types.DATE:
                    val = rs.getDate(i);
                    break;
                default:
                    val = rs.getString(i);
                    break;
                }

                if (null != val) {
                    if (!map.containsKey(key)) {
                        map.put(key, val);
                    }
                }
            }
            break;
        }
    } catch (Exception e) {
        log.error(e.getMessage());
    } finally {
        try {
            if (rs != null)
                rs.close();
            rs = null;
        } catch (SQLException e) {
            log.error(e.getMessage());
        }
    }
    return map;
}

然后再将map 装成json

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
101 新手上路
子站问答
访问
宣传栏