我正在尝试使用数据库中的结果集填充列表。事实是,列表只有在离开while()循环时才显示从db恢复的最后结果。代码:
public List<String[]> some_function(){
List<String[]> trs = new ArrayList<String[]>();
Connection conn = conectar_post("","","","");
Statement st = null;
ResultSet rs = null;
ResultSetMetaData rmd = null;
String sqls = "";
sqls = "SELECT col1, col2 FROM table1 LIMIT 4";
try{
st = conn.createStatement();
rs = st.executeQuery(sqls);
rmd = rs.getMetaData();
int i=0, ncol = 0, w=0;
ncol = rmd.getColumnCount()+1;
String[] line = new String[ncol];
while(rs.next()){
for(i=1;i<ncol;i++){
line[i] = rs.getString(i);
}
trs.add(line);
System.out.println(trs.get(w)[i-2]);
w++;
}
System.out.println("> "+trs.get(1)[1]);
System.out.println("> "+trs.get(2)[1]);
}
catch (SQLException e) { e.printStackTrace(); }
return trs;
}
输出:
244447
244448
244449
352712
> 352712
> 352712
有些东西我不见了?
答案 0 :(得分:0)
String[] line = new String[ncol];
应位于while(rs.next())