你用的是if,没有执行循环,应该用 while
public ListgetAllMsgList() {
ListmsgList = new ArrayList ();
DBConnection db = new DBConnection();
Connection conn = db.getConn();
String sql = "select * from msg";
try {
Statement pstmt = conn.createStatement();
ResultSet rs = pstmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt(1);
String content = rs.getString(2);
String author = rs.getString(3);
String publishiTime = rs.getString(4);
Msg msg = new Msg(id, content, author, publishiTime);
msgList.add(msg);
}
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return msgList;
if (rs.next()) {
int id = rs.getInt(1);
String content = rs.getString(2);
String author = rs.getString(3);
String publishiTime = rs.getString(4);
Msg msg = new Msg(id, content, author, publishiTime);
msgList.add(msg);
}
//把 if 改成 while 代码如下:
while (rs.next()) {
int id = rs.getInt(1);
String content = rs.getString(2);
String author = rs.getString(3);
String publishiTime = rs.getString(4);
Msg msg = new Msg(id, content, author, publishiTime);
msgList.add(msg);
}
//祝你好运
要用循环语句