根据一组用户输入JAVA swing查询数据库
我对编程非常陌生。我正在开发一个应用程序,它有6个字段供用户输入,根据输入的内容,我必须从数据库中查询行
我有以下输入组合:
- 一个月
- B号码
- 一个月,B号
- a月,开始日期/时间
- a编号,b编号,开始日期/时间
- B编号,开始日期/时间
- a编号,开始日期/时间,结束日期/时间
- B编号、开始日期/时间、结束日期/时间
- a编号,b编号,开始日期/时间,结束日期/时间
用户可以用我上面描述的所有方式输入信息。我必须根据用户输入查询数据库
假设用户输入一个数字,那么我的查询将是
Select * from table_name where Anumber = 'input'
如果用户同时输入一个数字和一个数字,那么我的查询将被删除
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
,这对我来说非常复杂,而且代码看起来非常粗糙
我想问的是,是否有更好的方法解决这类问题 我希望我能在这里解决我的问题:)
# 1 楼答案
为了改进代码,我的建议是使用Hibernate标准。它不仅可以改进代码,还可以防止sql注入等应用程序问题。例如,假设表的hibernate类是CDR。java您可以按如下所示处理查询:
另一个改进是为重复代码创建新方法,以提高可读性和修复错误
有关hibernate的更多信息,请参阅以下文档: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html
下面是一个关于它的很棒的教程: https://www.tutorialspoint.com/hibernate/hibernate_criteria_queries.htm