[英]How to create a search statment in java with multiple optional where clauses?
以下是常規搜索的代碼:
int row = jTable_accounts.getSelectedRow();
String Table = (jTable_accounts.getModel().getValueAt(row, 0).toString());
String sql ="select c.customer_id as 'Customer No.', customer_name as 'Name',a.account_id as 'Account No.',DIA,a.balance as 'Current Balance',a.outstanding_amt as'Outstanding Amt.' from customer c inner join customer_details cd on cd.customer_id=c.customer_id inner join account a on a.customer_id=c.customer_id where a.account_id= '"+Table+"' ";
因此,結果將顯示在一個jtable中,但是,我想添加4或5個文本框,這些文本框應根據客戶名稱,未償還金額或其他內容過濾結果。這些文本框應為可選條目,如果用戶未輸入在應用程序應顯示常規輸出的任何文本框中輸入任何值(上面的代碼)。
我認為應該在select語句中添加一些where子句,但是我不確定它應該是可選的。
以下是三個表,以獲取更多信息:
顧客:
customer_id DIA customer_name birth_date
-------------- ------ ------------------------ -------------
1 32 Ahmad mohammad bin afif (null)
2 10 mohammad ahmad bin afif (null)
顧客信息:
customer_id phone_no1 phone_no2 address_line1 address_line2
-------------- ------------ ------------ ---------------- ----------------
1 0111231415 019123443 bukit (null)
2 01345532 (null) kl serdang
帳戶:
account_id balance outstanding_amt customer_id
------------- ---------- ------------------ --------------
1 12530.23 1821.3 1
2 2040.13 125.3 1
3 213455 1234.3 2
您應該檢查文本框修剪的值是否不為空,如果不是,則可以在查詢中構建額外的過濾器。
例如
if (textField1.getValue().trim().length > 0) {
query += " and COLUMN1 = " + textField1.getValue().trim() + " ";
}
if (textField2.getValue().trim().length > 0) {
query += " and COLUMN2 = " + textField2.getValue().trim() + " ";
}
。 。 。
因為您有一個在其中聲明“ where”語句的基本查詢,所以只需要檢查文本字段是否為空即可添加“ and”語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.