JDBC update prepared语句未显示更新

时间:2013-08-17 22:02:09

标签: jdbc prepared-statement

我在程序中有两个JDBC SQL预处理语句来UPDATE表(程序的不同区域)。当我运行语句时,我可以在查询表时看到MySQL中的更新。所以我知道更新正在进行中。但是,我有一个表连接到程序中的DB,除非我停止运行程序并重新启动它,否则不会显示更新的结果。然后更新显示在表中。我有其他准备好的语句使用INSERT INTO,这些更改立即显示在表中(我不必停止重启我的程序)。我不知道问题是在UPDATE,还是在我的代码(通常的罪魁祸首),或者我的错误设置。我想我可能在方法调用之间传递错误。这是它的作用的一部分:我有一个收入表,用户创建一个新的收入列表,该列表的一部分被发送到FUNDS表(使用UPDATE语句),另一部分被发送到INCOME表(带有INSERT INTO声明)。这是代码,感谢您的帮助和建议!

     //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;
 }

0 个答案:

没有答案