public class MyRow {
private String col1;
private int col2;
...
// the constructor
public MyRow(String s, int i, ...) {
this.col1 = s;
this.col2 = i;
...
}
}
假设将结果集中的int列(如果存在)作为映射的键,则会得到如下结果:
Map<Integer, MyRow> map = new HashMap<Integer, MyRow>(0);
ResultSet rs;
// get your result set from some db query or whatever
while(rs.next()) {
MyRow mr = new MyRow(rs.getString(yourCol1Index), rs.getInt(yourCol2Index), ...);
map.put(rs.getInt(yourKeyColumnIndex), mr);
}
// finaly release the resources
rs.close();
# 1 楼答案
从结果集(被视为主键的内容)中选取一列,并将其用于映射的键属性。然后,您可以创建一个自定义类,将其余的列作为其字段保存(简单POJO),并将其用于value属性。举个例子:
假设将结果集中的int列(如果存在)作为映射的键,则会得到如下结果: