我如何简化这些线条。我有数百行这些行在每个表格单元格中设置值。
dailyResult("SELECT COUNT(*) FROM fault WHERE Type = 'E-MAIL FAULT' AND No_Fault_Number <> 'No Fault Number' AND CallTypeInt = 'TRI' AND DC_Month = " + month + " AND DC_Day = " + day + " AND DC_Year = " + year , 77, 1);
dailyResult("SELECT COUNT(*) FROM fault WHERE Type = 'E-MAIL FAULT' AND No_Fault_Number = 'No Fault Number' AND CallTypeInt = 'TRI' AND DC_Month = " + month + " AND DC_Day = " + day + " AND DC_Year = " + year , 78, 1);
dailyResult("SELECT COUNT(*) FROM fault WHERE CMTS = 'SJ1' AND CallTypeInt = 'TRI' AND DC_Month = " + month + " AND DC_Day = " + day + " AND DC_Year = " + year , 80, 1);
dailyResult("SELECT COUNT(*) FROM fault WHERE CMTS = 'SJ2' AND CallTypeInt = 'TRI' AND DC_Month = " + month + " AND DC_Day = " + day + " AND DC_Year = " + year , 81, 1);
dailyResult("SELECT COUNT(*) FROM fault WHERE CMTS = 'SJ3' AND CallTypeInt = 'TRI' AND DC_Month = " + month + " AND DC_Day = " + day + " AND DC_Year = " + year , 82, 1);
dailyResult("SELECT COUNT(*) FROM fault WHERE CMTS = 'ALA1' AND CallTypeInt = 'TRI' AND DC_Month = " + month + " AND DC_Day = " + day + " AND DC_Year = " + year , 83, 1);
dailyResult("SELECT COUNT(*) FROM fault WHERE CMTS = 'ALA2' AND CallTypeInt = 'TRI' AND DC_Month = " + month + " AND DC_Day = " + day + " AND DC_Year = " + year , 84, 1);
dailyResult("SELECT COUNT(*) FROM fault WHERE CMTS = 'FVW' AND CallTypeInt = 'TRI' AND DC_Month = " + month + " AND DC_Day = " + day + " AND DC_Year = " + year , 85, 1);
dailyResult("SELECT COUNT(*) FROM fault WHERE CMTS = 'MND1' AND CallTypeInt = 'TRI' AND DC_Month = " + month + " AND DC_Day = " + day + " AND DC_Year = " + year , 86, 1);
dailyResult("SELECT COUNT(*) FROM fault WHERE Call_Concern = 'Slow Connection' AND CallTypeInt = 'TRI' AND DC_Month = " + month + " AND DC_Day = " + day + " AND DC_Year = " + year , 88, 1);
dailyResult("SELECT COUNT(*) FROM fault WHERE Call_Concern = 'No Connection' AND CallTypeInt = 'TRI' AND DC_Month = " + month + " AND DC_Day = " + day + " AND DC_Year = " + year , 89, 1);
dailyResult("SELECT COUNT(*) FROM fault WHERE Call_Concern = 'Intermittent Connection' AND CallTypeInt = 'TRI' AND DC_Month = " + month + " AND DC_Day = " + day + " AND DC_Year = " + year , 90, 1);
只是看着它让我觉得很糟糕,我需要找出是否有一个循环来简化这个?任何类型的帮助都会很棒。
这是方法代码btw。
public void dailyResult(String q, int x, int y){
try{
Statement stmtDr = (Statement)daily.createStatement();
ResultSet rs = stmtDr.executeQuery(q);
if(rs.next()){
reportTable1.setValueAt(rs.getInt(1), x, y);
}
}catch(Exception e){
JOptionPane.showMessageDialog(rootPane, "Error 106\n\nAn error has occured with the resultset procedure method. Please try again later.", "Error!", JOptionPane.ERROR_MESSAGE);
答案 0 :(得分:1)
您可以在查询中使用group by,替换此
dailyResult("SELECT COUNT(*) FROM fault WHERE CMTS = 'MND1' AND CallTypeInt = 'TRI' AND DC_Month = " + month + " AND DC_Day = " + day + " AND DC_Year = " + year , 86, 1);
与
dailyResult("SELECT CMTS,COUNT(*) FROM fault WHERE CallTypeInt = 'TRI' AND DC_Month = " + month + " AND DC_Day = " + day + " AND DC_Year = " + year +" GROUP BY CMTS", 86, 1);
然后在dailyResult
ResultSet rs = stmtDr.executeQuery(q);
while(rs.next()){ // we are expecting multiple rows now
String faultType = rs.getString(1);//what fault is this?
reportTable1.setValueAt(rs.getInt(2), getXOffset(faultType), y);
}
需要注意的是,如果没有找到任何故障,那么将不会返回任何行,但这与您现在的行为相同,所以我假设您正在为此做好准备。
答案 1 :(得分:1)
对这些查询使用存储过程,并使用 group by 命令减少查询的长度。
实际上我注意到CMTS,Call_Concern等在每个查询中的使用方式不同。也许您可以将一些查询合并为一个,但我不认为所有这些查询都可以按当前格式组合在一起。参数化这些列。他们都是。也许你需要调查业务逻辑来做到这一点。一旦完成,您只需要对整个内容进行一次查询。