简体   繁体   中英

How to get count from database(table) and link to jsp page?

I have created 3 boxes represents "Booked", "Sold", and "Cancelled". Now I want to get how many people that have been booked, sold, and cancelled order and put the number inside the boxes. Here is my jsp code:

 <table class = "btn1" align = "center"> 
<tr>
<th><button class = "book" onclick ="filterSelection('Booked')">Booked</button><% %></th>
<th><button class = "sold" onclick ="filterSelection('Sold')"> Sold </button></th>
<th><button class = "cancel" onclick ="filterSelection('Cancelled')"> Cancelled </button></th>
</tr>
</table>
</div>

And mysql is:

public String getStatus2 (String co_no, String projCde, String solcCde, String phaseNum, Date cur_date) throws SQLException {
System.out.println("/*** Lot Status **");
String status1="";
     String sql5= "SELECT\r\n" + 
            "    COUNT(CASE WHEN smu_sts_cde = 'B' THEN 1 END) AS booked_cnt,\r\n" + 
            "    COUNT(CASE WHEN smu_sts_cde = 'S' THEN 1 END) AS sold_cnt,\r\n" + 
            "    COUNT(CASE WHEN smu_sts_cde = 'C' THEN 1 END) AS cancelled_cnt\r\n" + 
            "FROM smpurl\r\n" 
            +"WHERE\r\n" 
            +"    smu__book_dte <=  '"+cur_date+"'"  
            +"    smu_proj_cde = '"+projCde+"'" 
            +"    smu_phase_num = '"+phaseNum+"'" 
            +"    smu_buyr_solc_cde = '"+solcCde+"'";
     String lotCde = super.execStr(sql5,false);
     System.out.println("Execute = " +sql5);
     System.out.println("Execute = " +lotCde);
     if(lotCde != "") {
         String sql6 = "SELECT count(*) FROM smpurl"
                    + " WHERE smu_sts_cde = '"+lotCde+"'"; 
         System.out.println("Execute = " + sql6);
         String lotNme = super.execStr(sql6,false);
         status1 = lotNme;
     }else {
         status1 = "BOOKED";
        }

How can I get the number inside my jsp page?

For a partial answer, covering your MySQL requirement, you should be using conditional aggregation here:

SELECT
    COUNT(CASE WHEN smu_sts_cde = 'B' THEN 1 END) AS booked_cnt,
    COUNT(CASE WHEN smu_sts_cde = 'S' THEN 1 END) AS sold_cnt,
    COUNT(CASE WHEN smu_sts_cde = 'C' THEN 1 END) AS cancelled_cnt
FROM smpurl
WHERE
    smu__book_dte <= ? AND
    smu_proj_cde = ?   AND
    smu_phase_num = ?  AND
    smu_buyr_solc_cde = ?;

The ? placeholders are what represent the various dynamic values you want to use in the WHERE clause. Ideally, you would be using some sort of prepared statement. The various counts would be available using the aliases I have defined for them.

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