Saturday, 17 August 2013

JDBC update prepared statement not showing updates

JDBC update prepared statement not showing updates

I have two JDBC SQL prepared statements in the program to UPDATE tables
(different areas of program). When I run the statement, I can see the
updates in MySQL when I query the table. So I know the update is
happening. However, I have a table connected to the DB in the program that
does not show the updated results unless I stop running the program and
restart it. Then the updates show in the table. I have other prepared
statements that use INSERT INTO and these changes show up right away in
the tables (I don't have to stop-restart my program). I don't know if the
problem lies in the UPDATE, or in my code (the usual culprit), or some
setting I have wrong. I think I might be passing things wrong between
method calls. This is part of what it does: I have an income table, the
user creates a new income listing, and part of that listing gets sent to a
FUNDS table (with the UPDATE statement) and another part gets sent to an
INCOME table (with the INSERT INTO statement). Here is the code, thanks
for help and suggestions!
//Get user info
Income income = new Income();
Funds fund = new Funds();
income.setIncomeName(jTextField10.getText());
String budgetAmt = jTextField11.getText();
BigDecimal bAmt = new BigDecimal(budgetAmt);
income.setAmount(bAmt);
income.setDescription(jTextArea3.getText());
String iDate = jTextField12.getText();
Date date = new SimpleDateFormat("MM-dd-yy",
Locale.ENGLISH).parse(iDate);
java.util.Date utilDate = date;
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
income.setDateReceived(sqlDate);
s = (String)jList10.getSelectedValue().toString();
income.setFundAllocation(s);
// Send info to be processed in FUND table with UPDATE - start of
problem area
FundsSvcJDBCImpl fundImpl = new FundsSvcJDBCImpl();
Funds calculateFundAmt = fundImpl.calculateFundAmt(fund, income);
// Send info to be process in INCOME table - this part works
IncomeSvcJDBCImpl budgetImpl = new IncomeSvcJDBCImpl();
Income addIncome = budgetImpl.addIncome(income);
//This is the part of the FundsSvcJDBCImpl that uses the UPDATE
statement
public Funds calculateFundAmt (Funds fund, Income income) throws
Exception {
Statement stmt = null;
int max = 0;
BigDecimal fundTotal = new BigDecimal(0);
Connection conn = getConnection();
try{
stmt = conn.createStatement();
String sql1 = "SELECT sum(Amount) As fundTotal FROM funds
WHERE FundsName = ?";
PreparedStatement pstmt1 = conn.prepareStatement(sql1);
pstmt1.setString(1, income.getFundAllocation());
ResultSet rs2 = pstmt1.executeQuery();
while(rs2.next()){
fundTotal = fundTotal.add(rs2.getBigDecimal("fundTotal"));
}
fundTotal = fundTotal.add(income.getAmount());
String sql2 = "UPDATE funds SET Amount = ? WHERE FundsName
= ?";
PreparedStatement pstmt = conn.prepareStatement(sql2);
pstmt.setBigDecimal(1, fundTotal);
pstmt.setString(2, income.getFundAllocation());
pstmt.executeUpdate();
}
catch (Exception e){
throw e;
}
finally {
if (conn != null){
conn.close();
stmt.close();
}
}
return fund;
}

No comments:

Post a Comment