Java sql中的count函数 账户id当前余额期初日期 1 100 2012-03-01 2 100 2012-4-01 3 100 2013-03-1

Java sql中的count函数 账户id当前余额期初日期 1 100 2012-03-01 2 100 2012-4-01 3 100 2013-03-1,java,sql,Java,Sql,现在,当我在sql工作台上运行查询时,一切正常 account_id current_balance opening_date 1 100 2012-03-01 2 100 2012-4-01 3 100 2013-03-1 选择计数(会计科目\u id) 来自每日账户acc 其中acc.开业日期

现在,当我在sql工作台上运行查询时,一切正常

account_id current_balance opening_date 1 100 2012-03-01 2 100 2012-4-01 3 100 2013-03-1
选择计数(会计科目\u id)
来自每日账户acc
其中acc.开业日期<'2013-03-01'
但当我在NetBeans中运行这个时,它并没有给出正确的输出

select count(acc.account_id) 
from daily_account acc 
where acc.opening_date < '2013-03-01'
选择计数(会计科目\u id)
来自每日账户acc
其中acc.opening_date<'“+新日期((Integer.parseInt(FromYearComboBox.getSelectedItem().toString())-1900),FromMonthComboBox.getSelectedIndex(),Integer.parseInt(FromDateComboBox.getSelectedItem().toString()).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()
rs=st.executeQuery(“从每日账户中选择计数(acc.account\u id
acc,其中acc.开业日期<'“+新
日期((Integer.parseInt(FromYearComboBox.getSelectedItem().toString())-1900),FromMonthComboBox.getSelectedIndex(),(Integer.parseInt(FromDateComboBox.getSelectedItem().toString()).toString()+“;”;
rs.next();
tabledata[0][2]=rs.getString(1);
编辑:: 它给了我错误的答案…它正在计算所有帐户id


最后你似乎有一个额外的右大括号
,即
toString())

从每日账户acc中选择计数(acc.account\U id),其中acc.OPTING\U date<'+新日期((Integer.parseInt(FromYearComboBox.getSelectedItem().toString())-1900),FromMonthComboBox.getSelectedIndex(),Integer.parseInt(FromDateComboBox.getSelectedItem().toString()).toString()+“

一个注意事项这确实使查询字符串的维护变得复杂。尝试先构造日期字符串,然后将其追加到查询中

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

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);
String dateStr=
String.valueOf(Integer.parseInt(
FromYearComboBox.getSelectedItem().toString())-1900)
+FromMonthComboBox.getSelectedIndex()中
+FromDateComboBox.getSelectedItem().toString();
String queryStr=“从每日账户账户中选择账户(账户id)”+
“其中acc.开业日期<”+dateStr+“”;

您必须显示在生成该查询文本的Netbeans中使用的代码。我将避免使用sql中的java代码。首先把它放入一个变量中,看看你得到的值是否有效。它在日期处给了我正确的输出。你能发布结果查询字符串吗?如果你不想以后遇到麻烦,你真的应该考虑使用预先准备好的语句。使用预先准备好的语句而不是简单的语句。很抱歉……这是正确的,谢谢你的帮助suggestion@Ashishsingh:当我尝试在eclipse中查看字符串时,它会显示一个额外的大括号。另外,请检查更新的答案。我添加了另一个部分。@Ashishsingh您可以登录/sysout
queryStr
并查看形成了什么查询字符串吗?还有,为什么要从年份中减去1900?我得到了我的答案我没有导入JavaGood的SQl日期类,你得到了吗。。但是看看您的sql示例,确实不可能知道您想要实现什么以及sql日期如何解决您的问题。
 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 +"'";