簡體   English   中英

如何從數據庫(表)中獲取計數並鏈接到jsp頁面?

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

我創建了 3 個盒子,分別代表“已預訂”、“已售出”和“已取消”。 現在我想知道有多少人已被預訂、售出和取消訂單,並將數字放入框中。 這是我的jsp代碼:

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

而 mysql 是:

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

如何在我的jsp頁面中獲取數字?

對於部分答案,涵蓋您的 MySQL 要求,您應該在此處使用條件聚合:

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 = ?;

? 占位符代表您要在WHERE子句中使用的各種動態值。 理想情況下,您將使用某種准備好的語句。 使用我為它們定義的別名可以使用各種計數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM