我對編程非常陌生。我正在處理一個應用程序,它有6個字段供用戶輸入輸入,並根據輸入的輸入,我必須從數據庫查詢行。根據用戶輸入設置查詢數據庫 - JAVA
的形象和我有輸入以下組合:
- A-號碼
- B-號碼
- A-號碼,B-號碼
- A-號碼,開始日期/時間
- 號碼,Bnumber,開始日期/時間
- B-號碼,開始日期/時間
- A-號碼,開始日期/時間,結束日期/時間
- B-號碼,開始日期/時間,結束日期/時間
- A-號碼,B-號碼,開始日期/時間,結束日期/時間
用戶可以按照上述所有方式輸入輸入。我必須根據用戶輸入查詢數據庫。
比方說,用戶輸入A-號碼,然後我的查詢將
Select * from table_name where Anumber = 'input'
如果用戶輸入兩個aNumber的和B-號碼,然後我的查詢將
Select * from table_name where Anumber = 'input' AND bnumber = 'input2'
章等.. 。
我在分享一些我的代碼來認識別人我究竟在做什麼和什麼可以做得更好。
if (TextAnumber.isEmpty() && TextBnumber.isEmpty() && StrUserStartDate.isEmpty() && StrUserStartTime.isEmpty()
&& StrUserEndDate.isEmpty() && StrUserEndTime.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please Enter At Least One Input");
}
if (StrUserStartDate.isEmpty() && !StrUserStartTime.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please Enter Start Date");
}
if (!StrUserStartDate.isEmpty() && StrUserStartTime.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please Enter Start Time");
}
if (StrUserEndDate.isEmpty() && !StrUserEndTime.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please Enter End Date");
}
if (!StrUserEndDate.isEmpty() && StrUserEndTime.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please Enter End Time");
}
if (!StrUserEndDate.isEmpty() && !StrUserEndTime.isEmpty() && StrUserStartDate.isEmpty()
&& StrUserStartTime.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please Enter Start Date/Time");
}
if (!TextAnumber.isEmpty() && TextBnumber.isEmpty() && StrUserStartDate.isEmpty() && StrUserEndDate.isEmpty()) {
ResultSet rs = stmt.executeQuery(
"select anumber,bnumber FROM CDR WHERE ANUMBER = '" + TextAnumber + "' ORDER BY ANUMBER");
while (rs.next()) {
graphNodeA = rs.getString("ANUMBER");
graphNodeB = rs.getString("BNUMBER");
graph.addNode(graphNodeA);
graph.addNode(graphNodeB);
i++;
graph.addEdge("string" + i, graphNodeA, graphNodeB);
}
}
if (TextAnumber.isEmpty() && !TextBnumber.isEmpty() && StrUserStartDate.isEmpty() && StrUserEndDate.isEmpty()) {
ResultSet rs = stmt.executeQuery(
"select anumber,bnumber FROM CDR WHERE ANUMBER = '" + TextBnumber + "' ORDER BY BNUMBER");
while (rs.next()) {
graphNodeA = rs.getString("BNUMBER");
graphNodeB = rs.getString("ANUMBER");
graph.addNode(graphNodeA);
graph.addNode(graphNodeB);
i++;
graph.addEdge("string" + i, graphNodeA, graphNodeB);
}
}
if (!TextAnumber.isEmpty() && !TextBnumber.isEmpty() && StrUserStartDate.isEmpty()
&& StrUserEndDate.isEmpty()) {
ResultSet rs = stmt.executeQuery("select anumber,bnumber FROM CDR WHERE ANUMBER = '" + TextAnumber
+ "' AND BNUMBER = '" + TextBnumber + "' ORDER BY ANUMBER");
while (rs.next()) {
graphNodeA = rs.getString("ANUMBER");
graphNodeB = rs.getString("BNUMBER");
graph.addNode(graphNodeA);
graph.addNode(graphNodeB);
i++;
graph.addEdge("string" + i, graphNodeA, graphNodeB);
}
}
對於這一點,我使用If/Else
這在我看來很複雜,也代碼看起來很粗糙。
我在這裏想要的是,如果這種問題有更好的解決方案嗎? 我希望我在這裏清除我的問題:)
有什麼問題嗎?這可能更適合[Code Review](https://codereview.stackexchange.com/)。 – DCON