如何将自动递增的MySQL主键调用到Java变量

时间:2017-04-16 07:05:04

标签: java mysql

// DATABASE TABLE:

String ch_sql =
    "INSERT INTO Payment(Ino , pno , cno, Pamount, Pdate, Ptype) VALUES('" +
    (paymentIno) + "','" +
    (cashPno) + "','" +
    (cashCno) + "','" +
    (cashAmount) + "','" +
    (cashDate) + "','" +
    (cashType) + "')";
ch.executeUpdate(ch_sql);
  

插入值

private void jButton25ActionPerformed(java.awt.event.ActionEvent evt) {

    try {
        Connection_db cd = new Connection_db();
        Connection con = cd.createconnection();
        Statement ch = con.createStatement();
        Statement cq = con.createStatement();
        Statement dd = con.createStatement();

        String paymentIno = Payment_Invoice_No.getText();
        String depositDate = DD_date.getText();
        String depositBrand = DD_brand.getText();
        String sql_get = "SELECT Pno, Cno FROM invoice WHERE Ino = '" + (paymentIno) + "'";
        ResultSet sql_get_rs = ch.executeQuery(sql_get);
        String cashPno = "";
        String cashCno = "";

        while (sql_get_rs.next()) {
            cashPno = sql_get_rs.getString("Pno");
            cashCno = sql_get_rs.getString("Cno");
        }

        int cashAmount = Integer.parseInt(Cash_Amount.getText());
        String cashDate = Cash_Date.getText();
        String cashType = "";

        if (rb_cash.isSelected()) {
            cashType = "Cash";
        } else if (rb_cheque.isSelected()) {
            cashType = "Cheque";
        } else if (rb_dd.isSelected()) {
            cashType = "Direct Deposit";
        }

        String ch_sql =
            "INSERT INTO Payment(Ino , pno , cno, Pamount, Pdate, Ptype) VALUES('" +
            (paymentIno) + "','" +
            (cashPno) + "','" +
            (cashCno) + "','" +
            (cashAmount) + "','" +
            (cashDate) + "','" +
            (cashType) + "')";
        ch.executeUpdate(ch_sql);
        JOptionPane.showMessageDialog(null, "Sucessfully paid");

        int paymentBalance = Integer.parseInt(Payment_Invoice_Balance.getText());
        paymentBalance = paymentBalance - cashAmount;
        String sql_balance =
            "UPDATE invoice set Ibalance = '" + (paymentBalance) +
            "' WHERE ino = '" + (paymentIno) + "'";
        ch.executeUpdate(sql_balance);
        Payment_Invoice_Balance.setText(String.valueOf(paymentBalance));

        if (rb_cheque.isSelected()) {
            //
        } else if (rb_dd.isSelected()) {
            // ????  String sql_pid = "SELECT pid FROM Payment WHERE pid"

            String sql_directD =
                "INSERT INTO Payment VALUES('" +
                (paymentIno) + "','" +
                (cashPno) + "','" +
                (cashCno) + "','" +
                (cashAmount) + "','" +
                (cashDate) + "','" +
                (cashType) + "')";
        }
    }
    catch (Exception e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
}

现在我必须将自动递增的MySQL值变为Java变量,以便我可以将该pid插入下表中。

我应该插入pid的另一个表:

pid | int(11)|没有| PRI | 0

一切都在一次点击活动中发生。

public class MyAdapter extends RecyclerView.Adapter<VH> {
    private MyInterface mInterface;

    public MyAdapter(MyInterface i) {
         mInterface = i;
    }

    //... some code to create view holders


    //binding
    @Override
    protected void onBindItemViewHolder(ViewHolder viewHolder, final int position, int type) {
        viewHolder.bindView(position, mInterface); //call methods of interface in bindView() methods of your viewHolders
    }

    interface MyInterface {
        void someEvent();
    }
}

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView mText;

    ViewHolder(View root) {
        super(root);
        mText = (TextView)root.findViewById(R.id.text)
    }

    void bindView(int pos, MyAdapter.MyInterface myInterface) {
        mText.setOnClickListener(v -> myInterface.someEvent());
        //...some other code
    }
}

1 个答案:

答案 0 :(得分:0)

 try (ResultSet generatedKeys = ch.getGeneratedKeys()) {
        if (generatedKeys.next()) {
           return generatedKeys.getLong(1);
        }
        else {
            throw new SQLException("Creating user failed, no ID obtained.");
        }
    }