Ashishsingh
Ashishsingh

Reputation: 742

count function in sql

account_id    current_balance  opening_date
1             100              2012-03-01
2             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'

but when I am running this in NetBeans it is not giving proper output.

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

can any one help me why is this happening?

Edit :

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...

Upvotes: 0

Views: 181

Answers (1)

Yogendra Singh
Yogendra Singh

Reputation: 34367

You seem to have an extra closing braces ) in the end i.e toString()))). It should be one less e.g.

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()+"'";

One note This is really making your query string complex to maintain. Try constructing the date string before hand and then append in the query.

Another Note: Date constructor with arguments is deprecated, and it also seems that you really don't need the date but string. In that case, why don't you write something simple as :

 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 +"'";

Upvotes: 1

Related Questions