//DATABASE TABLE:
| Pid | int(6) unsigned zerofill | NO | PRI | NULL | auto_increment |
| Ino | varchar(20) | NO | PRI | | |
| pno | int(11) | NO | PRI | 0 | |
| cno | int(11) | NO | PRI | 0 | |
| Pamount | double | NO | | | |
| Pdate | date | NO | | | |
| Ptype | varchar(15) | NO | | | |
INSERTING VALUES
String ch_sql =
"INSERT INTO Payment(Ino , pno , cno, Pamount, Pdate, Ptype) VALUES('" +
(paymentIno) + "','" +
(cashPno) + "','" +
(cashCno) + "','" +
(cashAmount) + "','" +
(cashDate) + "','" +
(cashType) + "')";
ch.executeUpdate(ch_sql);
Now I have to get the auto-incremented MySQL value to a Java variable so I can insert that pid into the table below.
Another table where I should insert the pid:
pid | int(11) | NO | PRI | 0
Everything happens in one click event.
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());
}
}