我正在根据某些条件打印两个结果集。第一个结果集包含日期大于 1 天的数据。第二个结果集打印日期大于 2 天的表。但在我的第二个表中,第一个表的数据包含表。如何从第一个表中打印没有重复行的第二个表。(日期是表的列之一)
这两个表都是由同一个查询生成的。我必须用 java 写这个,我不能改变我的查询。使用java代码过滤重复数据我需要做什么?
您可以使用 HashMap 来避免重复键。
ResultSet rs1 = ... // here goes your first result set
ResultSet rs2 = ... // here goes your second result set
// TableRow is a class you can create which contains the fields of your result set
// including the primary key
HashMap<String, TableRow> hm = new HashMap<String, TableRow>();
// First you insert the rows from Result Set 1
while (rs1.next()) {
// Obtain primary key
String pk = rs1.getString("pkey");
// Obtain fields
String field1 = rs1.getString("field1");
String field2 = rs1.getString("field2");
// ...up to fieldN
TableRow tr = new TableRow(pk, field1, field2, ...);
hm.put(pk, TableRow);
}
// Then you insert the rows from Result Set 2
while (rs2.next()) {
// Obtain primary key
String pk = rs2.getString("pkey");
// Obtain fields
String field1 = rs2.getString("field1");
String field2 = rs2.getString("field2");
// ...up to fieldN
TableRow tr = new TableRow(pk, field1, field2, ...);
hm.put(pk, TableRow);
}
HashMap 将避免重复键。如果每一行的日期都是唯一的,那么你也可以使用日期作为 HashMap 的键。