我已经阅读了在java中使用jdbc在MySQL中获取最后一个插入ID的主要文章,但我似乎无法正确使用。
int identity;
public void insertdata(){
Connection con = Connect();
Statement statement;
statement = (Statement) con.createStatement();
String sql = "INSERT INTO table1 (Surname, FirstName, MiddleName, Birthday)" + " VALUES('"+surname+"', '"+fName+"', '"+mName+"', '"+bDay+"');";
statement.executeUpdate(sql,Statement.RETURN_GENERATED_KEYS);
ResultSet rs = statement.getGeneratedKeys();
rs.next();
identity=rs.getInt(1);
statement.close();
con.close();
}