繁体   English   中英

如何计算从数据库中找到的记录数量并使用struts2在jsp页面中显示?

[英]how to count number of records found from database and display in jsp page using struts2?

        List<CommonDTO> subjectList = new ArrayList<CommonDTO>();
            try {

                pstmt = conn.prepareStatement(SLCMQueryConstant.SEARCH_SUBJECT);
                pstmt.setString(1, commonDTO.getSubjectName());
                pstmt.setString(2, commonDTO.getSubjectCode());

                if(commonDTO.getIsActive()==0){

                    pstmt.setInt(3, commonDTO.IsActive = 1 );

                }
                else{

                    pstmt.setInt(3, commonDTO.IsActive= 0);
                }

                rs = pstmt.executeQuery();
                int count = 0;
                while (rs.next()) {
                    count++;
                    CommonDTO commondto = new CommonDTO();
                    commondto.setSubjectId(rs.getInt("Subject_Id"));
                    commondto.setSubjectCode(rs.getString("Subject_Code"));
                    commondto.setSubjectName(rs.getString("SubjectName"));
                    commondto.setSubjectNameHindi(rs.getString("SubjectName_Hindi"));
                    commondto.setIsActive(rs.getInt("Is_Active"));
                    commondto.setViewcount(count);
                    subjectList.add(commondto);
                }
                System.out.println(count);
            }
        return subjectList;

    public static final String SEARCH_SUBJECT = new StringBuilder("").
    append(" SELECT Subject_Id,Subject_Code,SubjectName,SubjectName_Hindi,Is_Active").
        append(" FROM  M_Subject_Master WHERE   Subject_Id=IFNULL(NULL,Subject_Id) ").
        append(" AND (SubjectName IS NULL OR SubjectName LIKE CONCAT('%',?,'%'))"). 
        append(" AND (Subject_Code IS NULL OR Subject_Code LIKE CONCAT('%',?,'%'))").
        append(" AND Is_Active = ?  ").
        append(" ORDER BY SubjectName").toString();

JSP页面以显示找到的记录数=========================================

<s:iterator value="subjectList" var="quesvar" status="questat">
                    <s:hidden value="%{subjectId}" name="commonDTO.subjectId" id="subjectId"></s:hidden>
                    <td valign="top" class="style11" style="width: 20%;text-align:left">No.Of Records Found<s:property  value="subjectList[#questat.index].viewcount"/></td>
                        <tr class="item">
                        <td valign="top" class="style10" style="width: 10%;text-align:left"><s:property value="#questat.count"/></td>
                            <td valign="top" class="style11" style="width: 20%;text-align:left"><s:property  value="subjectList[#questat.index].subjectCode"/></td>
                            <td valign="top" class="style11" style="width: 20%;text-align:left"><s:property  value="subjectList[#questat.index].subjectName"/> </td>


                            <td valign="top" class="style11" style="width: 20%;"><a href="#" onClick="editSubject(<s:property  value="subjectList[#questat.index].subjectId"/>)" >View/Edit</a>

                        </tr>
                    </s:iterator>

如何计算数据库中记录的数量并在jsp页面上打印? 我不使用另一个prepare语句来计数如何显示从数据库中的jsp中获取的记录数。

在您的JSP文件中,您可以打印出尺寸

<s:property value ="subjectList.size()"/>

您也可以直接从数据库中执行此操作。

选择后包括SQL_CALC_FOUND_ROWS,例如, select SQL_CALC_FOUND_ROWS * from users

然后再次触发Select FOUND_ROWS()

注意:只需使用相同的数据库连接器实例触发两个查询。

当您有很多行并且已应用限制时,这将为您提供帮助。 在这种情况下,您的记录总数将大于限制。

例如,在mysql的任何UI中(如SQLyog或Workbench),当您首次尝试打开表时,它们的自我附加限制为1000条记录,但是在这种情况下,表可能包含超过50000条记录,因此从数组列表中获取计数可能无法提供总数每次记录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM