0

我正在尝试更新数据库中的一个表,我在该表中接受学生的费用并维护收到的金额、收到的总金额和付款方式等的记录。我的代码如下:

private void pay_saveActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String dbUrl = "jdbc:mysql://localhost/hostel";
    String dbClass = "com.mysql.jdbc.Driver";
    PreparedStatement ps1 = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");

            con = DriverManager.getConnection (dbUrl,"root","17121990");
            System.out.println("connected!");

            String firstname=pay_enter_firstname.getText();
            String lastname=pay_enter_lastname.getText();

            String amt=pay_enter_amt.getText();
            int amount=Integer.parseInt(amt);
            String day=pay_enter_date.getText(); 
            String cheque_no=pay_enter_chequeno.getText();
            String mode=pay_enter_mode.getText();
            int totalamount=10000;               
            int bal_amt=totalamount-amount;
            String remark=pay_enter_remark.getText();          
            int id = Integer.parseInt(pay_enter_id.getText());

            Statement stmt = con.createStatement(
                                  ResultSet.TYPE_SCROLL_INSENSITIVE,
                                  ResultSet.CONCUR_UPDATABLE);
            ResultSet rs = stmt.executeQuery("SELECT id, lastamtreceived FROM payment");





            if(rs.next())
           {
                stmt = con.createStatement(
                                  ResultSet.TYPE_SCROLL_INSENSITIVE,
                                  ResultSet.CONCUR_UPDATABLE);
                rs = stmt.executeQuery("SELECT id, lastamtreceived FROM payment");

              while(rs.next())
                {



              int temp =rs.getInt(1);

              if (temp ==id)
              {
                  int amtrecvd2= rs.getInt(2);
                  bal_amt=totalamount- (amtrecvd2+amount);
                  String updt = "UPDATE payment SET Amountreceivd="+(amtrecvd2+amount)+",lastamtreceived="+amount+",dte='"+day+"', balance_amt ="+bal_amt+" WHERE id ="+temp+";" ;
                  Statement stmt1 = con.createStatement();
                  int result = stmt1.executeUpdate(updt);
              }

                }
           }

             if(!rs.next())
            {
                String str=" INSERT INTO payment(id, firstname,lastname,Amountreceivd,dte,lastamtreceived,Creditcashcheque,"
                   + "cheque_no,balance_amt,totalamount,Remark) VALUES ("+id+",'"+firstname+"','"+lastname+"',"+amount+",'"+day+"',"+amount+",'"+mode+"','"+cheque_no+"',"+ bal_amt+","+totalamount+",'"+remark+"')";
                Statement stmt1=con.createStatement();

                int result=stmt1.executeUpdate(str);
                panel_feesframe.setVisible(false);
            }


               panel_feesframe.setVisible(false);



    con.close();
    }
        catch (ClassNotFoundException | SQLException | NumberFormatException e)
    {
        e.printStackTrace();
    }

}

最初,当我添加新值时,我得到了正确的结果,但是当我尝试更新现有行时,我得到了错误,即我为主键 ID 制作了重复条目。

我应该检查什么条件,以便我知道结果集没有那个特定的 id 值并且新人正在支付费用?

4

3 回答 3

1

此条件: if(!rs.next()) 正在 while 循环之外进行检查。此条件始终为真,即使发生更新,也会尝试插入记录。为避免这种情况,我建议使用标志变量。一旦发生更新,将此标志的值设置为 1。检查它是否已变为 1 而不是if(!rs.next())进入内部。

于 2013-02-27T08:58:10.790 回答
1

你是两个if陈述正在冲突...

// If this is true...
if(rs.next()) {
    // ...
    // Looping till the it's false...
    while(rs.next()) {
        // ....
    }
 }

 // Will mean that this is false...
 if(!rs.next())

你应该使用一个else

if(rs.next()) {
    // ...
    while(rs.next()) {
        // ....
    }
 } else {...}

更新

在与 Aashray 进行了启发性的转换(谢谢)之后,我们得出的结论是你的逻辑被打破了

而不是手动尝试通过匹配 id 手动查找记录,让 SQL 数据库为您完成。

代替....

ResultSet rs = stmt.executeQuery("SELECT id, lastamtreceived FROM payment");

你应该用...

ResultSet rs = stmt.executeQuery("SELECT id, lastamtreceived FROM payment where id = " + id);

这将返回一个ResultSet空的(不匹配)或(希望)一行。

从那里,调用rs.next()现在可以让您在更新或正确插入之间进行分支。

private void pay_saveActionPerformed(java.awt.event.ActionEvent evt) {
    String dbUrl = "jdbc:mysql://localhost/hostel";
    String dbClass = "com.mysql.jdbc.Driver";
    PreparedStatement ps1 = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");

        con = DriverManager.getConnection(dbUrl, "root", "17121990");
        System.out.println("connected!");

        String firstname = pay_enter_firstname.getText();
        String lastname = pay_enter_lastname.getText();

        String amt = pay_enter_amt.getText();
        int amount = Integer.parseInt(amt);
        String day = pay_enter_date.getText();
        String cheque_no = pay_enter_chequeno.getText();
        String mode = pay_enter_mode.getText();
        int totalamount = 10000;
        int bal_amt = totalamount - amount;
        String remark = pay_enter_remark.getText();
        int id = Integer.parseInt(pay_enter_id.getText());

        Statement stmt = con.createStatement(
                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_UPDATABLE);
        ResultSet rs = stmt.executeQuery("SELECT id, lastamtreceived FROM payment where id = " + id);

        if (rs.next()) {
            int amtrecvd2 = rs.getInt(2);
            bal_amt = totalamount - (amtrecvd2 + amount);
            String updt = "UPDATE payment SET Amountreceivd=" + (amtrecvd2 + amount) + ",lastamtreceived=" + amount + ",dte='" + day + "', balance_amt =" + bal_amt + " WHERE id =" + id + ";";
            Statement stmt1 = con.createStatement();
            int result = stmt1.executeUpdate(updt);
        } else {
            String str = " INSERT INTO payment(id, firstname,lastname,Amountreceivd,dte,lastamtreceived,Creditcashcheque,"
                            + "cheque_no,balance_amt,totalamount,Remark) VALUES (" + id + ",'" + firstname + "','" + lastname + "'," + amount + ",'" + day + "'," + amount + ",'" + mode + "','" + cheque_no + "'," + bal_amt + "," + totalamount + ",'" + remark + "')";
            Statement stmt1 = con.createStatement();

            int result = stmt1.executeUpdate(str);
            panel_feesframe.setVisible(false);
        }

        panel_feesframe.setVisible(false);

        con.close();
    } catch (ClassNotFoundException | SQLException | NumberFormatException e) {
        e.printStackTrace();
    }

}
于 2013-02-27T09:00:21.803 回答
0
    I think this may help you

私人无效 pay_saveActionPerformed(java.awt.event.ActionEvent evt) {
String dbUrl = "jdbc:mysql://localhost/hostel"; String dbClass = "com.mysql.jdbc.Driver"; PreparedStatement ps1 = null;

尝试 { Class.forName("com.mysql.jdbc.Driver");

    con = DriverManager.getConnection (dbUrl,"root","17121990");
    System.out.println("connected!");

    String firstname=pay_enter_firstname.getText();
    String lastname=pay_enter_lastname.getText();

    String amt=pay_enter_amt.getText();
    int amount=Integer.parseInt(amt);
    String day=pay_enter_date.getText(); 
    String cheque_no=pay_enter_chequeno.getText();
    String mode=pay_enter_mode.getText();
    int totalamount=10000;               
    int bal_amt=totalamount-amount;
    String remark=pay_enter_remark.getText();          
    int id = Integer.parseInt(pay_enter_id.getText());

    Statement stmt = con.createStatement(
                          ResultSet.TYPE_SCROLL_INSENSITIVE,
                          ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT id, lastamtreceived FROM payment");





    if(rs.next())
   {
        stmt = con.createStatement(
                          ResultSet.TYPE_SCROLL_INSENSITIVE,
                          ResultSet.CONCUR_UPDATABLE);
        rs = stmt.executeQuery("SELECT id, lastamtreceived FROM payment");

      while(rs.next())
        {



      int temp =rs.getInt(1);

      if (temp ==id)
      {
          int amtrecvd2= rs.getInt(2);
          bal_amt=totalamount- (amtrecvd2+amount);
          String updt = "UPDATE payment SET Amountreceivd="+(amtrecvd2+amount)+",lastamtreceived="+amount+",dte='"+day+"', balance_amt ="+bal_amt+" WHERE id ="+temp+";" ;
          Statement stmt1 = con.createStatement();
          int result = stmt1.executeUpdate(updt);
      }

        }
   }
    else
    {
        String str=" INSERT INTO payment(id, firstname,lastname,Amountreceivd,dte,lastamtreceived,Creditcashcheque,"
           + "cheque_no,balance_amt,totalamount,Remark) VALUES ("+id+",'"+firstname+"','"+lastname+"',"+amount+",'"+day+"',"+amount+",'"+mode+"','"+cheque_no+"',"+ bal_amt+","+totalamount+",'"+remark+"')";
        Statement stmt1=con.createStatement();

        int result=stmt1.executeUpdate(str);
        panel_feesframe.setVisible(false);
    }


       panel_feesframe.setVisible(false);

con.close(); } catch (ClassNotFoundException | SQLException | NumberFormatException e) { e.printStackTrace(); }

于 2013-02-27T09:06:44.987 回答