我的系統在檢索數據庫中的某些值時遇到問題。我想獲得特定領域的值的總結。這是我的檢索值的代碼:如何檢索COUNT()SUM()AVG()MIN()MAX()語句的結果集?
public void insertToClientSummary(){
sql = "SELECT COUNT(genClientID), SUM(principal), SUM(interest), SUM(totalPayment), SUM(totalBal) FROM client_info";
try {
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
String titles[] = new String [4];
titles[0] = "Total Clients";
titles[1] = "Total Loan Book";
titles[2] = "Total Interests";
titles[3] = "Total Payment";
titles[4] = "Total Balance";
rs.next();
for (int ctr=0;ctr<=5;ctr++){
String sql2 = "INSERT INTO client_summary (title, sumValues) VALUES ('"+titles[ctr]+"','')";
PreparedStatement stmt2 = conn.prepareStatement(sql2);
stmt2.executeUpdate();
ctr++;
sql = "UPDATE client_summary SET sumValues = '"+String.valueOf(rs.getDouble(ctr))+"'";
stmt = conn.prepareStatement(sql);
stmt.executeUpdate();
ctr--;
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null, "Client Summary Query Exception");
}
}
順便說一句,我也包括我的代碼上說client_summary
表中插入新值。但我認爲我在第一個查詢中遇到了異常。任何關於正確檢索數據的想法?
什麼是您正在使用的數據庫? – shazin
mysql5和phpMyAdmin是我的DBMS –