我是编程新手。我正在开发一个应用程序,它有6个字段供用户输入输入,根据输入的输入,我必须从数据库中查询行。
我有以下组合输入:
用户可以通过我上面描述的所有方式输入输入。我必须根据用户输入查询数据库。
假设用户输入Anumber,那么我的查询将是
Select * from table_name where Anumber = 'input'
如果用户同时输入aumber和bnumber,那么我的查询将是
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
对我来说似乎非常复杂,而且代码看起来非常粗糙。
我想要的是,如果有更好的解决方案可以解决这类问题吗? 我希望我能在这里解决我的问题:)
答案 0 :(得分:0)
为了改进代码,我的建议是你使用Hibernate标准。它不仅可以改善您的代码,还可以防止您的应用程序问题,如SQL注入。例如,假设您的表的hibernate类是CDR.java,您可以处理查询,如下所示:
Criteria cr = session.createCriteria(CDR.class);
if (!TextAnumber.isEmpty()) {
cr.add(Restrictions.eq("ANUMBER", TextAnumber));
}
if (!TextBnumber.isEmpty()) {
cr.add(Restrictions.eq("BNUMBER", TextBnumber));
}
//make sure to format your date to the right format
if (!StrUserStartDate.isEmpty().isEmpty()) {
cr.add(Restrictions.gt("StartDate",StrUserStartDate));
}
if (!StrUserEndDate.isEmpty().isEmpty()) {
cr.add(Restrictions.lt("EndDate",StrUserEndDate));
}
List results = cr.list();
另一项改进是为重复代码创建新方法,以提高可读性和修复错误。
有关hibernate的更多信息,请参阅此处的文档: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html
这是一个很棒的教程: https://www.tutorialspoint.com/hibernate/hibernate_criteria_queries.htm