2012-08-02 49 views
0

我試圖開發計數值顯示在我的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.but顯示輸出始終1.爲什麼DIS錯誤是匹配發生here.please幫助我在這裏使用什麼循環條件給我一些解決方案。

回答

1

您實際上並未查看查詢結果。由於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. 
} 
+0

Thanks..thank u.it是非常有幫助的我,我有我的答案 – 2012-08-02 05:48:34

+0

我得到了我的output.because日期字段保存在DATE(YYYY-MM -dd)format.but現在我的數據庫有日期filed.it被保存在timestamp(int)format.now如何開發this.please幫助我 – 2012-08-08 08:45:49

+0

你應該開始另一個問題,但Date.getTime()是你的地方需要開始尋找。 – John3136 2012-08-08 23:46:43

0

執行查詢後添加這行。

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