如何为Java变量调用自动递增的MySQL主键
//数据库表:如何为Java变量调用自动递增的MySQL主键,java,mysql,Java,Mysql,//数据库表: | Pid | int(6) unsigned zerofill | NO | PRI | NULL | auto_increment | | Ino | varchar(20) | NO | PRI | | | | pno | int(11) | NO | PRI | 0 | | |
| 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 | | | |
插入值
现在,我必须将自动递增的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());
}
}
我们应该理解什么?您想获取最后生成的密钥吗?这里不清楚您在问什么,因为您的问题的格式使其无法阅读,而且您似乎没有提出问题。请通读一遍,然后回来相应地编辑你的问题。我是新来的,所以我不太明白你在这里做了什么。你能告诉我们整个方法吗,这样我们可以帮助你更多!我用这种方法编辑了这篇文章好吧我更新了我的答案
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());
}
}
try (ResultSet generatedKeys = ch.getGeneratedKeys()) {
if (generatedKeys.next()) {
return generatedKeys.getLong(1);
}
else {
throw new SQLException("Creating user failed, no ID obtained.");
}
}