为什么我读数据库信息只能读到最后一条数据?

来源:百度知道 编辑:UC知道 时间:2024/06/22 09:12:16
/**
* 查询
*/
public void select(UserFrame userFrame){
String strsql = "select * from userInfo";
String str = " ";

try {
connection();
ResultSet rs = stmt.executeQuery(strsql);
while(rs.next()){
if (!rs.next()) {
JOptionPane.showMessageDialog(null, "没找到任何信息");
} else {
str = rs.getString(2) + "\t" + rs.getString(3) + "\t" +
rs.getString(4) + "\t" + rs.getString(5) + "\n";
}
}
userFrame.jEditorPane1.setText(str);
rs.close();
stmt.close();
con.close();
} catch (SQLException ex) {
}
}

为什么??搞C#太久了,JAVA都快忘完了

.next() 一次游标就移到下一条记录了
如果while(rs.next()){
if (!rs.next()) {
这样写向下移动2次,中间就会有一条数据被跳过了

public void select(UserFrame userFrame){
String strsql = "select * from userInfo";
String str = " ";

try {
connection();
ResultSet rs = stmt.executeQuery(strsql);
while(rs.next()){
str += rs.getString(2) + "\t" + rs.getString(3) + "\t" +
rs.getString(4) + "\t" + rs.getString(5) + "\n";
}
if (str=="") {
JOptionPane.showMessageDialog(null, "没找到任何信息");
} else {
userFrame.jEditorPane1.setText(str);
}
rs.close();
stmt.close();
con.close();
} catch (SQLException ex) {
}
}