//数据库表:如何调用自动递增的MySQL的主键Java变量
| 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 | | | |
插入值
String ch_sql =
"INSERT INTO Payment(Ino , pno , cno, Pamount, Pdate, Ptype) VALUES('" +
(paymentIno) + "','" +
(cashPno) + "','" +
(cashCno) + "','" +
(cashAmount) + "','" +
(cashDate) + "','" +
(cashType) + "')";
ch.executeUpdate(ch_sql);
现在我已经得到了自动将MySQL值递增为一个Java变量,以便我可以将该pid插入到下表中。
另一张表,我应该插入pid:
pid | int(11)| NO | PRI | 0
所有事情都发生在一次点击事件中。
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());
}
}
我们应该理解的是什么? –
你想获得最后生成的密钥吗? –
不清楚你在这里问什么,因为你的问题的格式化使得它不可读,并且因为你似乎没有提出问题。请通读[我如何问一个好问题?](http://stackoverflow.com/help/how-to-ask),然后回来并相应地编辑您的问题。 – DaveyDaveDave