我想查询一个表中的两个字段值A,B,然后存储在键值对HashMap中,如何写合适一些?自定义SQL?还是把所有取出来,然后遍历?
4 回复
public HashMap<String,String> queryUniqValues(Dao dao, String fieldname1, String fieldname2 ,String table) {
Sql sql= Sqls.create("select $fieldname1,$fieldname2 from $table");
sql.vars().set("fieldname1", fieldname1).set("fieldname2", fieldname2).set("table",table);
sql.setCallback(new SqlCallback() {
public Object invoke(java.sql.Connection conn, ResultSet rs, Sql sql) throws SQLException {
HashMap<String,String> list = new HashMap<String,String>();
while (rs.next()) {
String value1= rs.getString(fieldname1);
String value2= rs.getString(fieldname2);
list.put(value1,value2);
}
return list;
}
});
dao.execute(sql);
return sql.getResult();
}
最后一句是 return sql.getResult();这么写吗?还是说我的函数返回值应该改写成Object?
添加回复
请先登陆