0
account_id current_balance opening_date
1 100 2012-03-01
2 100 2012-4-01
3 100 2013-03-1

现在当我在 sql work bench 中运行查询时很好

select count(acc.account_id) 
from daily_account acc 
where acc.opening_date < '2013-03-01'

但是当我在 NetBeans 中运行它时,它没有给出正确的输出。

select count(acc.account_id) 
from daily_account acc 
where acc.opening_date < '"+ new Date((Integer.parseInt(FromYearComboBox.getSelectedItem().toString())-1900),FromMonthComboBox.getSelectedIndex(),Integer.parseInt(FromDateComboBox.getSelectedItem().toString()))).toString()

谁能帮助我为什么会这样?

编辑 :

rs = st.executeQuery("select count(acc.account_id) from daily_account
acc where  acc.opening_date < '"+ new
Date((Integer.parseInt(FromYearComboBox.getSelectedItem().toString())-1900),FromMonthComboBox.getSelectedIndex(),(Integer.parseInt(FromDateComboBox.getSelectedItem().toString()))).toString()+"';");

rs.next();

tabledata[0][2]=rs.getString(1);

编辑::它给了我错误的答案......它正在计算所有的帐户ID......

4

1 回答 1

1

最后,您似乎有一个额外的右大括号),即toString()))). 它应该少一个,例如

select count(acc.account_id) from daily_account acc where acc.opening_date < '"+ new Date((Integer.parseInt(FromYearComboBox.getSelectedItem().toString())-1900),FromMonthComboBox.getSelectedIndex(),Integer.parseInt( FromDateComboBox.getSelectedItem().toString())).toString()+"'";

注意这确实使您的查询字符串难以维护。尝试事先构造日期字符串,然后附加到查询中。

另一个注意事项: Date不推荐使用带参数的构造函数,而且您似乎真的不需要日期而是字符串。在那种情况下,你为什么不写一些简单的东西:

 String dateStr = 
      String.valueOf(Integer.parseInt(
                              FromYearComboBox.getSelectedItem().toString())-1900)
  + FromMonthComboBox.getSelectedIndex()
  +FromDateComboBox.getSelectedItem().toString();

String queryStr = "select count(acc.account_id) from daily_account acc "+
                  " where acc.opening_date < '"+ dateStr +"'";
于 2012-11-09T15:22:39.063 回答