Java 最后插入 ID 不起作用
可能的重复:
如何在 JDBC 中获取插入 ID?
我正在尝试获取最后一个插入 ID,但显然它不起作用,它不断给我带来一堆错误,其中之一是
java.sql.SQLException: Can not issue SELECT via executeUpdate().
我试图获取最后一个插入的 ID,但它不起作用,这是我的代码
public int getLastInsertID(){
try{
Statement statement = conn.createStatement();
ResultSet rs = null;
statement.executeUpdate("SELECT LAST_INSERTED_ID", Statement.RETURN_GENERATED_KEYS);
rs = statement.getGeneratedKeys();
while(rs.next()){
System.out.println(""+rs.getInt(1));
id = rs.getInt(1);
JOptionPane.showMessageDialog(null,id);
}
}catch(Exception e){
e.printStackTrace();
}
return id;
}
}
和第二个错误我
public void addEmployee(Personal p ,Contact c,Employee e) {
Statement statement = null;
String insert0 = "INSERT INTO `finalpayroll`.`users` (`emp_id`, `emp_pass`) VALUES ('2010-010122', '1231922')";
try{
statement = conn.createStatement();
statement.executeUpdate(insert0);
statement.close();
}catch(Exception ex){
ex.printStackTrace();
}
id = getLastInsertID();
String insert1 = "INSERT INTO personal_info (`idpersonal_info`,`First_Name`, `Middle_Initial`, `Last_Name`, `Date_Of_Birth`, `Marital_Status`, `Beneficiaries`) VALUES ("+id+",'"+p.getFirstName()+"', '"+p.getMiddleInitial()+"'" +
" , '"+p.getLastName()+"', '"+p.getDateOfBirth()+"', '"+p.getMaritalStatus()+"', '"+p.getBeneficiaries()+"')";
try{
statement = conn.createStatement();
statement.executeUpdate(insert1);
statement.close();
conn.close();
JOptionPane.showMessageDialog(null, "Employee Added!!");
}catch(Exception ex){
ex.printStackTrace();
}
}
怎么能获取最后插入的id?我的方法有什么问题吗?
Possible Duplicate:
How to get the insert ID in JDBC?
I was trying to get the last insert ID, but apparently it's not working, it keeps on giving me a bunch of errors one of them is this
java.sql.SQLException: Can not issue SELECT via executeUpdate().
I was trying to get the last Inserted it but it doesn't work, here's my code
public int getLastInsertID(){
try{
Statement statement = conn.createStatement();
ResultSet rs = null;
statement.executeUpdate("SELECT LAST_INSERTED_ID", Statement.RETURN_GENERATED_KEYS);
rs = statement.getGeneratedKeys();
while(rs.next()){
System.out.println(""+rs.getInt(1));
id = rs.getInt(1);
JOptionPane.showMessageDialog(null,id);
}
}catch(Exception e){
e.printStackTrace();
}
return id;
}
}
and the second one is
public void addEmployee(Personal p ,Contact c,Employee e) {
Statement statement = null;
String insert0 = "INSERT INTO `finalpayroll`.`users` (`emp_id`, `emp_pass`) VALUES ('2010-010122', '1231922')";
try{
statement = conn.createStatement();
statement.executeUpdate(insert0);
statement.close();
}catch(Exception ex){
ex.printStackTrace();
}
id = getLastInsertID();
String insert1 = "INSERT INTO personal_info (`idpersonal_info`,`First_Name`, `Middle_Initial`, `Last_Name`, `Date_Of_Birth`, `Marital_Status`, `Beneficiaries`) VALUES ("+id+",'"+p.getFirstName()+"', '"+p.getMiddleInitial()+"'" +
" , '"+p.getLastName()+"', '"+p.getDateOfBirth()+"', '"+p.getMaritalStatus()+"', '"+p.getBeneficiaries()+"')";
try{
statement = conn.createStatement();
statement.executeUpdate(insert1);
statement.close();
conn.close();
JOptionPane.showMessageDialog(null, "Employee Added!!");
}catch(Exception ex){
ex.printStackTrace();
}
}
how could I get the last inserted id? what's wrong wit my method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来这可能是某些版本的 mysql 驱动程序的错误。在此源代码中搜索错误消息。他们没有检查 getGenerateIds 条件。
MySQL 自 2005 年以来就有一个类似的“无法修复”bug。提到 JDBC API 并不要求其驱动程序支持该功能。这似乎是一个已知且未解决的问题。
Looks like it may be a bug with certain versions of the mysql driver. Search this source code for the error message. They aren't checking for the getGeneratedIds condition.
MySQL has a similar 'won't fix' bug open since 2005. The discussion mentions that it's not a requirement of the JDBC API that their driver support the feature. Seems likely this is a known and unaddressed issue.