gpt4 book ai didi

java - sql中的计数函数

转载 作者:行者123 更新时间:2023-12-01 15:03:25 26 4
gpt4 key购买 nike

account_id    current_balance  opening_date1             100              2012-03-012             100              2012-4-01 3             100              2013-03-1

now when I am running query in sql work bench it's fine

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

Edit :: It is giving me wrong answer ...it is counting all the account id...

最佳答案

最后似乎有一个额外的右大括号),即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 +"'";

关于java - sql中的计数函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13310685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com