gpt4 book ai didi

java - 不完整的结果集数据

转载 作者:太空宇宙 更新时间:2023-11-03 11:07:54 25 4
gpt4 key购买 nike

    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;
}

上面代码的问题是它只返回结果集的第一个结果。

当我删除 WHILE(RS.NEXT) 子句中的两个 IF 子句时,它返回了所有需要的结果但不完整的信息,因为我还需要在 if 语句中查询。

如果你们知道确切的问题,请提供帮助,如果你们需要更多信息,请告诉我。谢谢!

最佳答案

这里,问题似乎在

pstmtLName.setInt(j++, rs.getInt("leaderid"));
pstmtMName.setInt(j++, rs.getInt("memberid"));

j 的值会随着每个真条件的增加而增加,直到循环迭代。因此,它会增加您准备好的语句的parameterIndex

应该是

pstmtLName.setInt(1, rs.getInt("leaderid"));
pstmtMName.setInt(1, rs.getInt("memberid"));

关于java - 不完整的结果集数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10614918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com