// String sql = "insert into user(email,password,name) values('"+email+"','"+ password+"','"+name+"')";
String sql ="insert into user(email,password,name) values(?,?,?)";try(PreparedStatement preparedStatement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)){
preparedStatement.setString(1,email);
preparedStatement.setString(2,password);
preparedStatement.setString(3,name);
i = preparedStatement.executeUpdate();try(ResultSet generatedKeys = preparedStatement.getGeneratedKeys()){if(generatedKeys.next()){
id = generatedKeys.getLong(1);}};}
问题
java.sql.SQLException: Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate(), Statement.executeLargeUpdate() or Connection.prepareStatement().
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.StatementImpl.getGeneratedKeys(StatementImpl.java:1396)
at com.zaxxer.hikari.pool.ProxyStatement.getGeneratedKeys(ProxyStatement.java:230)
Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.execu
问题java.sql.SQLException: Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate(), Statement.executeLargeUpdate() or Connection.prepareStatement(). at com.mysql.cj.jdbc.exceptions.SQLError.createSQLEx