我有以下代码
public int addMail(String mail){
String query = "";
BD bd = new BD();
int id = 0;
try{
query = "INSERT INTO Mail(dir) VALUES ('" + "?" + "');";
PreparedStatement sp = bd.conect().prepareStatement(query);
sp.setString(1, mail);
int result = sp.executeUpdate();
// ResultSet rs = sp.getGeneratedKeys();
// id = rs.getInt("idMail");
if (result > 0) {
System.out.println("Mail created");
} else {
System.out.println("Mail not created");
}
sp.close();
bd.conection.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return id;
}
我需要从INSERT查询中获取ID(它的IDENDITY),然后将其返回给Main。 我尝试使用getGeneratedKeys和SCOPE_IDENDTITY,但是无法正常工作(我是SQL新手)