在遍历我的数据库记录并将其存储在哈希映射或字符串数组中时,我只能检索一行

问题描述 投票:0回答:0

这只返回最后一行数据:

ResultSetMetaData md = rs.getMetaData();

while (rs.next()) {            
    for (int i = 1; i <= md.getColumnCount(); i++) {
        data_map.put(md.getColumnName(i), rs.getString(i));
    }
    System.out.println(data_map);
    con.close();  
    return data_map;    
         
}  

我尝试使用下面的代码,这只返回第一行数据。返回所有数据行我做错了什么?

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(sql);
rs.last();
        
int rows=rs.getRow();
System.out.println("Number of rows"+rows);
ResultSetMetaData md=rs.getMetaData();
int cols=md.getColumnCount();
System.out.println("Number of rows"+cols);
        
String data[][]= new String[rows][cols];
        
int i=0;
rs.beforeFirst();
while(rs.next()) {
    for(int j=0; j<=cols; j++) {
        data[i][j]=rs.getString(j+1);
        System.out.println(data[i][j]);
    }
    i++;
}
return data;
java jdbc
© www.soinside.com 2019 - 2024. All rights reserved.