我的系统在检索数据库中的某些值时遇到问题。我想获得特定字段中值的摘要。这是我检索值的代码:
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
表中插入新值的代码。但我认为我在第一次查询时遇到了异常。对正确检索数据有任何想法吗?