无法更新ATM界面中存款交易后的余额

发布于 2025-01-27 09:58:29 字数 1634 浏览 2 评论 0原文

我正在使用Java Swing进行GUI在Java的ATM界面进行项目。在数据库中的存款交易后,我无法更新余额。我找不到我出错的地方。

我有一个名为-tryproject的数据库,其中表为 - CustomerDetails,其中有cardnumcardpin and code> balance> balance /代码>作为列。实际上,我会遇到一个错误,因为参数索引超出了范围1>参数的数量为0。我正在发送我的存款模块守则。

代码:-

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    int depositAmount = Integer.valueOf(jTextField1.getText());
    int cardnum = Integer.valueOf(jTextField2.getText());

      try{
        Class.forName("com.mysql.jdbc.Driver"); 
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/TYProject","root","riddhimore");
        PreparedStatement pst = con.prepareStatement("select * from customerDetails where cardnum=?");
        pst.setInt(1, cardnum);
        ResultSet rs = pst.executeQuery();
        if(rs.next()){
            rs.getInt("balance");
            String sql = "update customerDetails set balance=balance+"+depositAmount+ "where cardnum=?"; 
            PreparedStatement pst1 = con.prepareStatement(sql);
            pst.setInt(1,cardnum);
            if(pst1.executeUpdate() == 1){
                int a = JOptionPane.showConfirmDialog(this, "Are you sure you want to submit ?", "Confirm ?", JOptionPane.YES_NO_CANCEL_OPTION);
                if(a == 0){
                    new atmFrame10().setVisible(true);
                    this.setVisible(false);
                }
            }
        }
      }catch(Exception e){
          JOptionPane.showMessageDialog(null, e);
      }
}

I'm doing a project on ATM interface in Java using Java swing for GUI. I'm not able to update the balance after the deposit transaction in the database. I'm not able to find where I'm going wrong.

I've a database named - TYProject, in it the table is - customerDetails and in it there's cardnum, cardpin and balance as columns. Actually I'm getting an error as parameter index out of range 1> number of parameters which is 0. I'm sending my code of deposit module.

Code:-

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    int depositAmount = Integer.valueOf(jTextField1.getText());
    int cardnum = Integer.valueOf(jTextField2.getText());

      try{
        Class.forName("com.mysql.jdbc.Driver"); 
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/TYProject","root","riddhimore");
        PreparedStatement pst = con.prepareStatement("select * from customerDetails where cardnum=?");
        pst.setInt(1, cardnum);
        ResultSet rs = pst.executeQuery();
        if(rs.next()){
            rs.getInt("balance");
            String sql = "update customerDetails set balance=balance+"+depositAmount+ "where cardnum=?"; 
            PreparedStatement pst1 = con.prepareStatement(sql);
            pst.setInt(1,cardnum);
            if(pst1.executeUpdate() == 1){
                int a = JOptionPane.showConfirmDialog(this, "Are you sure you want to submit ?", "Confirm ?", JOptionPane.YES_NO_CANCEL_OPTION);
                if(a == 0){
                    new atmFrame10().setVisible(true);
                    this.setVisible(false);
                }
            }
        }
      }catch(Exception e){
          JOptionPane.showMessageDialog(null, e);
      }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫罗兰の梦幻 2025-02-03 09:58:29

实际上,我会遇到一个错误,因为参数索引超出了范围1>参数数为0。

为了避免indexoutofBoundSexceptions(或任何所谓的),我养成了使用三元运算符的习惯。这是保险的,以确保您不会从范围中调用索引:

int index = (testNumber < 0 || testNumber > numOfParameters) ? 0 : testNumber;

这将转化为:

if("the index number is outside the range of your Array/Object/etc.") 
    return 0; 
else 
    return testNumber;

编辑以进行澄清

我知道这不是一个完整的解决方案

无法更新余额

,但建议避免一些例外

Actually I'm getting an error as parameter index out of range 1> number of parameters which is 0.

To avoid the IndexOutOfBoundsExceptions (or whatever it is called) I have made a habit of using a ternary operators. This acts as insurance to make sure you aren't calling an index out of bounds:

int index = (testNumber < 0 || testNumber > numOfParameters) ? 0 : testNumber;

This translates to:

if("the index number is outside the range of your Array/Object/etc.") 
    return 0; 
else 
    return testNumber;

EDIT for clarification

I understand that this is not a full solution to

Unable to update balance

but advice to avoid some exceptions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文