0

这是从数据库获取当前日期信息的代码。它正在成功运行。

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 是我的数据库:

在此处输入图像描述

在这里我希望输出是如果我运行上述应用程序意味着它显示计数值。如果我希望输出是当前月份的详细信息。例如:我获取当前日期信息意味着检查查询并显示计数值为 2 ..它工作成功。在这里我写的查询是

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

它成功地工作了。同样的事情是如何为当前月份的 status=Q 编写查询...所以现在我希望显示输出为 2.但我无法执行 dis.所以请帮助我。怎么样to do.how 我怎样才能改变我的代码。

4

2 回答 2

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'

于 2012-08-02T08:54:36.553 回答
1

在这里尝试MONTH()mysql的 功能

于 2012-08-02T08:55:51.047 回答