0

我尝试开发一个 java 应用程序。这里正在从 mysql 数据库获取信息。我的代码是:

public class RetailerWs {
  public int data(){
    int count=0;
    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 MONTH(date) = MONTH(CURDATE()) AND YEAR(date) = YEAR(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;
  }
}

另一类是:

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

}

在这里我必须检查查询并显示计数值是否成功。但我怀疑查询匹配项为零表示输出显示空白。但我希望它是显示 0.不显示空白屏幕..所以请帮忙我该怎么办。

4

2 回答 2

1

创建类 RetailerWs 的对象。

RetailerWs  obj =new RetailerWs ();
System.out.println(obj.data());
于 2012-08-02T11:20:20.237 回答
0

该声明

System.out.println(new RetailerWs().data());

将始终无条件地打印一个整数值,即使该值为 0。除非异常阻止它执行,否则。

于 2012-08-02T11:21:32.320 回答