2012-08-02 27 views
0

這是從數據庫獲取當前日期信息的代碼。它正在成功運作。GetCurrent月從yyyy-mm-dd

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(); 

     while(result.next()) { 
     // Do something with the row returned. 
     count++; //if the first col is a count. 
     }  
    } 
    catch (Exception exc) { 
     System.out.println(exc.getMessage()); 
    } 


    return count; 
    } 
} 

我的疑問是如何從數據庫中獲取當前月份的詳細信息。 ..如何做...請幫助我。

DIS是我的數據庫:

enter image description here

在這裏,我想需要的輸出是,如果我運行上述應用意味着它顯示計數值。如果我想需要的輸出是當前月份的詳細資料。對於如:我知道現在的日期信息是指檢查查詢並顯示計數值2..it進行加工successfully.here我寫的查詢是

select * from orders where status='Q' AND date=CURDATE()". 

它成功是怎麼worked.same事編寫查詢狀態=本月的Q ...所以現在我希望顯示輸出是2.但我無法做dis.so請幫助我。如何做,如何更改我的代碼。

回答

1

試功能的MySQL here

+0

dis鏈接無法打開me.its顯示問題加載error.please發送給我那個代碼 – 2012-08-02 09:01:46

+0

@krishnaveni其工作正常,http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_month – 2012-08-02 09:02:49

+0

請閱讀我的更新問題,並給我解決方案 – 2012-08-02 09:27:25

1

試試這個:

select month(CURDATE()) 

select date_format(now(),'%M')as Month_name 

得到月份數

select date_format('2012-07-26','%m')as Month_number 

回答您的問題更新:

select count(*) from orders where status='Q' AND date=CURDATE()" 

就在您的查詢添加COUNT(*)會給你造成 '2'

+0

給我其他的解決辦法 – 2012-08-02 09:00:38

+0

@krishnaveni的MONTH():你如何需要你放出來? – 2012-08-02 09:02:10

+0

在我的數據庫中有一個字段yyyy-mm-dd format.i從dis格式獲取信息以獲取僅月值。 – 2012-08-02 09:03:52