且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从JDBC结果集中获取行值的数组

更新时间:2023-02-06 08:33:02

您可以使用地图以所需的格式最初收集结果.

You can use a Map to initially collect the results in the format you want.

Map<Integer, Set<String>> customerTitles = new HashMap<Integer, Set<String>>();
while(resultSet.next()) {
    Integer custId = resultSet.getInt(1);
    Set<String> titles = customerTitles.containsKey(custId) ? 
            customerTitles.get(custId) : new HashSet<String>();
    titles.add(resultSet.getString(2));
    customerTitles.put(custId, titles);
}

一旦以这种方式收集了数据,就可以遍历Map,然后依次将其中的Set转换为JSON

Once you have it collected this way, you can iterate over the Map and in turn the Sets within and convert them to JSON

// Convert to Json array here using your JSON library
for(Integer custId :  customerTitles.keySet()) {
    for(String title : customerTitles.get(custId)) {

    }
}