改正程序。 con = DriverManager.getConnection(url, user, pwd); sql = con.createStatement(); String sqlStatement; sqlStatement = "SELECT * FROM student"; PreparedStatement pstm = con.prepareStatement(sqlStatement); rs = sql.executeQuery(sqlStatement); int count =0; while(rs.next()) { count++; } rs = sql.executeQuery(sqlStatement); 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-02-29 14:52:49 浏览: 76
sql-server驱动包
改正后的程序如下:
```
con = DriverManager.getConnection(url, user, pwd);
String sqlStatement;
sqlStatement = "SELECT * FROM student";
PreparedStatement pstm = con.prepareStatement(sqlStatement);
rs = pstm.executeQuery();
int count = 0;
while(rs.next()) {
count++;
}
rs.beforeFirst();
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++;
}
```
修改的内容如下:
1. 将原来的 `sql` 对象改为 `pstm` 对象,并使用 `executeQuery()` 方法执行查询语句。
2. 在第一个 `while` 循环中统计查询结果的行数,并使用 `beforeFirst()` 方法将结果集指针返回到第一行。
3. 在第二个 `while` 循环中遍历查询结果,并将每行数据存储到 `info` 数组中。
阅读全文