我希望从DB.resulset获取的hashmap中添加值有2行数据。但是它的覆盖首先添加了行值。
这是我的代码。
Statement s1 = con.createStatement();
ResultSet rs1 = s1.executeQuery("SELECT * FROM garantee WHERE loanapplicationid ='" + applicationid + "'");
ResultSetMetaData garanteeMetaData = rs1.getMetaData();
int garanteeColumns = garanteeMetaData.getColumnCount();
HashMap<String, String> garanteeFull = new HashMap<String, String>();
while (rs1.next()) {
int j = 1;
for (int i = 1; i < garanteeColumns; i++) {
garanteeFull.put(garanteeMetaData.getColumnLabel(i) + j, rs1.getString(i));
System.out.println(rs1.getString(i));
}
j++;
}
我使用int j来改变键值。但是它不起作用corectly.please帮助我。如何做到
**抱歉英文不好
答案 0 :(得分:1)
我认为你应该在循环之外使用以下行,因为无论何时它进入循环,j的值每次变为1。
int j = 1;
请尝试使用以下代码替换您的代码..
Statement s1 = con.createStatement();
ResultSet rs1 = s1.executeQuery("SELECT * FROM garantee WHERE loanapplicationid ='" + applicationid + "'");
ResultSetMetaData garanteeMetaData = rs1.getMetaData();
int garanteeColumns = garanteeMetaData.getColumnCount();
HashMap<String, String> garanteeFull = new HashMap<String, String>();
int j = 1;
while (rs1.next()) {
for (int i = 1; i < garanteeColumns; i++) {
garanteeFull.put(garanteeMetaData.getColumnLabel(i) + j, rs1.getString(i));
System.out.println(rs1.getString(i));
}
j++;
}