0
    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 语句中的查询。

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

4

2 回答 2

2

在这里,问题似乎在

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"));
于 2012-05-16T09:33:16.853 回答
2

您已定义int k但未使用它。我假设你想用它来设置memberid参数。
改变

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

pstmtMName.setInt( k++, rs.getInt( "memberid" ) );  

它应该可以工作。

我想知道当查询中只看到一个参数标记时,为什么要使用i++,j++并设置语句的参数值。您应该直接使用. 否则,如果获取的记录多于 where and ,它们将导致标记索引增加,例如,这在您的查询案例中是无效参数并抛出 and 。 k++?pstObject.setInt( 1, ...rsleaderid != 0memberid != 0pstObject.setInt( 2, ...SQLException

由于您pstObject在 while 循环中重复使用相同的内容,我建议您使用pstObject.clearParameters()它来清除当前参数值。尽管这是可选的,但在某些情况下,立即释放当前参数值使用的资源很有用。

于 2012-05-16T10:03:43.767 回答