改正代码。 while (rs.next()) { count++; } Object[][] info = new Object[count][6]; String[] title = { "学号", "姓名", "出生日期", "性别", "专业", "宿舍楼" }; count = 0; while (rs.next()) { info[count][0] = rs.getString("sno"); info[count][1] = rs.getString("sname"); info[count][2] = rs.getString("sbirth"); info[count][3] = rs.getString("ssex"); info[count][4] = rs.getString("sclass"); info[count][5] = rs.getString("saddress"); count++; }
时间: 2024-03-01 21:54:06 浏览: 88
C语言课程设计
可以将第二个 while 循环中的代码合并到第一个 while 循环中,如下所示:
```
while (rs.next()) {
count++;
}
Object[][] info = new Object[count][6];
String[] title = { "学号", "姓名", "出生日期", "性别", "专业", "宿舍楼" };
count = 0;
rs.beforeFirst(); // 将结果集的指针重置到第一行之前
while (rs.next()) {
info[count][0] = rs.getString("sno");
info[count][1] = rs.getString("sname");
info[count][2] = rs.getString("sbirth");
info[count][3] = rs.getString("ssex");
info[count][4] = rs.getString("sclass");
info[count][5] = rs.getString("saddress");
count++;
}
```
这样就可以正确地将 ResultSet 中的数据保存到 info 数组中了。需要注意的是,第二个 while 循环中的代码执行之前,需要将 ResultSet 的指针重置到第一行之前,否则第二个 while 循环也无法遍历 ResultSet 中的数据。
阅读全文