简体   繁体   中英

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 Page to show number of records found =========================================

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

How can I count number of records from database and print on jsp page ? I don't use another prepare statement for count how can i display number of records fetch in jsp from database.

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

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

You can also do it Directly from the database.

Include SQL_CALC_FOUND_ROWS after select like select SQL_CALC_FOUND_ROWS * from users

And then again fire Select FOUND_ROWS()

Note : just fire both query with same instance of database connector.

This will help you when you have lots of rows and you have applied limit. In that case you will have total no of records which may be greater then limit.

For example, in any UI for mysql Like SQLyog or workbench when you first try to open a table they their self append limit of 1000 records but table may contain more then 50000 records in that case getting count from array list may not give you total number of records every time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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