Comment comment;
ArrayList<Comment> commentList = null;
try{
ConnectionFactory myFactory = ConnectionFactory.getFactory();
Connection conn = myFactory.getConnection();
int i = 1; int j = 1; int k = 1;
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM COMMENT WHERE deletestatus = 0 and workid = ?");
PreparedStatement pstmtLName = conn.prepareStatement("SELECT * FROM LEADER WHERE leaderid = ?");
PreparedStatement pstmtMName = conn.prepareStatement("SELECT * FROM MEMBER WHERE memberid = ?");
pstmt.setInt(i++, Integer.parseInt(projid));
ResultSet rs = pstmt.executeQuery();
System.out.print(rs);
commentList = new ArrayList<Comment>();
while(rs.next())
{
comment = new Comment();
comment.setDetails(rs.getString("details"));
comment.setDateadded(rs.getTimestamp("dateadded"));
comment.setUrgency(rs.getInt("urgency"));
if(rs.getInt("leaderid") != 0){
comment.setLeaderid(rs.getInt("leaderid"));
pstmtLName.setInt(j++, rs.getInt("leaderid"));
ResultSet rs2 = pstmtLName.executeQuery();
if(rs2.next()){
comment.setFullname(rs2.getString("firstname") +" " + rs2.getString("lastname"));
}
}
if(rs.getInt("memberid") != 0) {
comment.setMemberid(rs.getInt("memberid"));
System.out.print("in the loop");
pstmtMName.setInt(j++, rs.getInt("memberid"));
ResultSet rs3 = pstmtMName.executeQuery();
if(rs2.next()){
comment.setFullname(rs3.getString("firstname") +" " + rs3.getString("lastname"));
}
}
comment.setProjid(Integer.parseInt(projid));
commentList.add(comment);
}
return commentList;
}
上面代码的问题是它只返回结果集的第一个结果。
当我删除IF
子句中的两个WHILE(RS.NEXT)
子句时,它返回了所有需要的结果,但信息不完整,因为我还需要 if 语句中的查询。
如果你们知道确切的问题,请帮忙,如果你们需要更多信息,请告诉我。谢谢!