如何从ResultSet构建此Java集合 [英] How to build this Java Collection from a ResultSet

查看:118
本文介绍了如何从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集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆