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

I have a sql statement that brings back the following rows:

3844d1575bd25d991b3a8c934a8195b1.png

I would like to obtain these values from a JDBC result set as two objects. One for the customer with customerNo 1 and the other for customer 2. I would like the two objects to have another array value with the related Titles with the object values.

The structure would ultimately look like this (in JSON):

{customerNo : 1, ["Object 1", "Object 2", "Object 3"]}, {customerNo : 2, ["Object 4", "Object 5"]}

How can I accomplish this with JDBC?

解决方案

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

Map> customerTitles = new HashMap>();

while(resultSet.next()) {

Integer custId = resultSet.getInt(1);

Set titles = customerTitles.containsKey(custId) ?

customerTitles.get(custId) : new HashSet();

titles.add(resultSet.getString(2));

customerTitles.put(custId, titles);

}

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)) {

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值