我开始学习Java,但是练习练习中遇到了问题。
这是整个代码的片段:
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.util.ArrayList;
import java.sql.PreparedStatement;
private static final int MAX_LINES = 44;
private static final int REPORT_COL1 = 30;
private static final int REPORT_COL = 15;
private ArrayList<String> errors = new ArrayList<String>();
private ArrayList<String> detailReport = new ArrayList<String>();
private ArrayList<String> summaryReport= new ArrayList<String>();
String firstSortCode= (String) parameters.get("01");
if(firstSortCode==null || firstSortCode.trim().equals("")) {
errors.add("Missing required parameter 01");
invalidParameters= true;
}
else {
for(int i=0; i<SORT_CODES.length; i++) {
if(firstSortCode.equals(SORT_CODES[i][0])) {
pSort1 = SORT_CODES[i][1];
sort1Attr = SORT_CODES[i][2];
sort1GetDescTable = SORT_CODES[i][3];
sort1GetDescCode = SORT_CODES[i][4];
sort1GetDescValue = SORT_CODES[i][5];
val1 = firstSortCode + " - " + pSort1;
break;
}
}
if(pSort1.equals("")) {
errors.add("Invalid value '"+ firstSortCode + "' for parameter 01");
invalidParameters= true;
}
}
String mainSelectSQL ="SELECT shrdgmr_pidm, " +
" shrdgmr_levl_code, "+
" shrdgmr_grst_code, "
" NVL("+sort1Attr+", 'Not Reported') ";
mainSelectSQL =mainSelectSQL +
"FROM shrdgmr " +
"WHERE shrdgmr_pidm is not null " +
//Appends the ORDER BY clause
mainSelectSQL+= "ORDER BY "+sort1Attr;
//Executes the query and obtains the ResultSet
ResultSet rs= sqlStatement.executeQuery();
String sort1Desc= "***";
String Sort1Prev= "*";
//Arrays to hold the student counts for each of the reported sort values
int sort1Count= 0;
int grandTotal = 0;
while(rs.next()) {
String Value1 = rs.getString(2);
if(!Value1.equals(prevSort1Value)) {
String sort1Record= Library.lPad(sort1Count, REPORT_COL, ' ');
if(!sort1Desc.equals("***")) {
if(lineCount[0]+4>MAX_LINES) {
startNewPage(detailReport, pageCount, lineCount, currentDate,
databaseName, pGradTerm, pInitiator, sort1Desc, pSort1, false);
}
detailReport.add(Library.rPad(sort1Desc, REPORT_COL1, ' ')+sort1Record);
summaryReport.add(Library.rPad(sort1Desc, REPORT_COL1, ' ')+sort1Record);
lineCount[0]++;
}
//Resets the counter
sort1Count =0;
sort1Desc= getSortDescription(connection, pSort1, sort1GetDescValue, sort1GetDescTable,
sort1GetDescCode, Value1, errors);
}
sort1Count++;
我的问题是如何合并重复项并汇总其数量?
例如,我现在的代码仅在摘要报告中打印以下内容,
建筑40
工程56
牙科66
建筑16
计算机科学10
工程11
建筑5
输出应仅为:
体系结构61
工程学67
牙科66
计算机科学10
建筑5
我只是坚持如何做到这一点。我正在考虑使用hashmap或hashset,但不确定如何使用。感谢您的帮助,谢谢!
答案 0 :(得分:1)
如果您真的不想在sql中使用聚合,则可以使用HashMap<String, Integer>
。像这样:
Map<String, Integer> aggResult = new HashMap<>();
while (rs.next()) {
String name = rs.getString(NAME_INDEX); //NAME_INDEX - name column index
int value = rs.getInt(VALUE_INDEX); //VALUE_INDEX - value column index
aggResult.merge(name, value, Integer::sum);
//if you dont have java8 use this "if":
/*
if (aggResult.containsKey(name)) {
Integer sum = aggResult.get(name);
aggResult.put(name, sum + value);
} else {
aggResult.put(name, value);
}
*/
}
//test output:
aggResult.forEach((key, value) -> System.out.println(key + ": " + value));
也许您将需要TreeMap
(带有或不带有比较器)而不是HashMap
进行订购,对ResultSet中的值进行空检查或其他。
答案 1 :(得分:0)
必须包含聚合函数,查询中显示“没有聚合的聚合”就没有魅力
使用以下查询进行必要的操作。我用sum是因为您的o / p实际上看起来并不像计数数据,而是已经计数的记录的总和
SELECT NAME, SUM(*)
FROM TABLE GROUP BY
NAME