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

SQLワークベンチでクエリを実行しているときは問題ありません

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())))。たとえば、1つ少なくする必要があります

acc.opening_date < '"+ new Date((Integer.parseInt(FromYearComboBox.getSelectedItem().toString())-1900),FromMonthComboBox.getSelectedIndex(),Integer.parseInt( FromDateComboBox.getSelectedItem().toString())).toString()+"'";

1 つの注意事項これにより、クエリ文字列の管理が非常に複雑になります。事前に日付文字列を作成してから、クエリに追加してみてください。

別の注意: 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 に答える