如何从ResultSet构建此Java集合

编程入门 行业动态 更新时间:2024-10-20 08:54:11
本文介绍了如何从ResultSet构建此Java集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个ResultSet的数据,它包含三个字段:DataType,Data,DataValue。我想要构建一个Java集合,然后构建JSON编码。

这是输出当我尝试打印:

System.out.println(resultSet.getString(DATATYPE)+ - >+ resultSet.getString(DATA)+ - + resultSet.getString(DATAVALUE));

输出:

code>地址 - >测试 - > JHGJHGJKG 地址 - >城市 - >一些城市地址 - >状态 - >一些状态地址 - >市中心 - >一些市中心地址 - >电话 - > 547455654 地址 - >街 - >一些街道客户信息 - >市中心 - >一些其他市中心客户信息 - >作业 - >程序员客户信息 - >识别卡 - > ID32443 客户信息 - >邮政编码 - > 74584 客户信息 - >种类 - >常规客户信息 - >生日 - > 10 01 1980 客户信息 - >电话 - > 99832498 其他 - >上次访问 - > 10 01 1980

我使用Guava Google Collections这样做:

Map< String,String> data = new HashMap< String,String>(); HashMultimap< String,Map> multiMap = HashMultimap。< String,Map>创建(); ... //迭代resultSet .. data.put(resultSet.getString(DATA),resultSet.getString(DATAVALUE )); multiMap.put(resultSet.getString(DATATYPE),data); .. // resultSet ends

生成的Collection / Multimap be:

{ [地址= { Test = somevalue, City = SomeCity, ... },客户信息= { Job =程序员,识别卡= ID34234, ... },其他{ Lastvisit = 10-02-1990 } ] } pre>

我试过,但没有成功的结果。

ResultSet的所有结果都是动态的类型,数据和值。

解决方案

我不能使用定义类,例如对于Address数据,根据输入数据不能显示。你可以有一个类似这样的通用域对象:

Dataobject { dataType; data; dataValue; 设置< Dataobject> descendents; }

这将给你无限的链接深度。 p>

在迭代结果集时,您可以决定是要在同一级别创建还是增加深度。

毕竟这样做,使用XStream()将它编组为一个xml字符串。

I have data from a ResultSet that contains three fields: DataType, Data, DataValue. I want to build a Java Collection, and next build the JSON encode.

This is the output when I try to print:

System.out.println(resultSet.getString("DATATYPE") + " --> " + resultSet.getString("DATA") + " --> " + resultSet.getString("DATAVALUE"));

Output:

Address --> Test --> JHGJHGJKG Address --> City--> Some city Address --> State --> Some state Address --> Downtown --> Some downtown Address --> Telephone --> 547455654 Address --> Street --> Some street Customer Information --> Downtown --> some other downtown Customer Information --> Job --> Programmer Customer Information --> Identification card --> ID32443 Customer Information --> ZIP Code --> 74584 Customer Information --> Kind of person --> Regular Customer Information --> Birthday --> 10 01 1980 Customer Information --> Telephone --> 99832498 Other --> Last visit --> 10 01 1980

I attemped with Guava Google Collections like this:

Map<String, String> data = new HashMap<String,String>(); HashMultimap<String, Map> multiMap = HashMultimap.<String, Map> create(); ... //iterating the resultSet .. data.put(resultSet.getString("DATA"), resultSet.getString("DATAVALUE")); multiMap.put(resultSet.getString("DATATYPE"), data); .. // resultSet ends

The the resulting Collection/Multimap would be:

{ [ Address = { Test = somevalue, City = SomeCity, ... }, Customer Information={ Job = programer, Identificationcard = ID34234, ... }, Other{ Lastvisit = 10-02-1990 } ] }

I've tried, but no successful results.

All the results from ResultSet are dynamically, dynamic type, data and value. I can't use definided classes, for example for an Address data, based on the input data could not be shown.

解决方案

You can have a generic domain object like this:

Dataobject { dataType; data; dataValue; Set<Dataobject>descendents; }

That will give you the endless depth of chaining.

while you iterate the resultset, you can decide whether you want to create them at the same level or increasing depth.

after all that is done, use XStream() to marshall it into an xml string.

更多推荐

如何从ResultSet构建此Java集合

本文发布于:2023-10-18 11:00:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504043.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ResultSet   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!