簡體   English   中英

計算值顯示在apache-tomcat上

[英]count the value is display on apache-tomcat

我已經嘗試開發計數值,該值顯示在我的apache-tomcat控制台窗口中。 這里使用以下代碼:

public class RetailerWs {
 public int data(){
int count=0;

count++;

try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pro","root","");

    PreparedStatement statement =  con.prepareStatement("select * from orders where status='Q' AND date=CURDATE()");
     ResultSet result = statement.executeQuery();


}

      catch(Exception exc){
      System.out.println(exc.getMessage());
      }


    return count;
       }

        }

我的另一堂課是:

  public class Demo {
  public static void main(String[] args){
    RetailerWs obj = new RetailerWs();
    System.out.println(obj.data());
   }

  }

在我的數據庫中,有2個值與上面的query匹配。但是輸出始終顯示1.為什么這里會發生dis錯誤。請幫助我這里使用什么循環條件。給我一些解決方案。

您實際上並不是在查看查詢結果。 由於代碼中的count++ count為1。

您需要類似:

while(result.next()) {
    // Do something with the row returned.
    // e.g. count += result.getInt[1]; if the first col is a count.
}

在執行查詢后添加此行。

count = 0;
while ( result.next() ) 
count++;

暫無
暫無

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

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