When doing insert MySQL database by JDBC. The executeUpdate API default will return row count only. See Java Doc:
Returns:
either (1) the row count for SQL Data Manipulation Language (DML) statements
or (2) 0 for SQL statements that return nothing
But actually, you can configure it to return the insert record ID. Just change your executeUpdate from:
int count = stmt.executeUpdate(strInsert);
to
stmt.executeUpdate(strInsert, Statement.RETURN_GENERATED_KEYS);
ResultSet rs = stmt.getGeneratedKeys();
if (rs != null && rs.next()) {
long id = rs.getLong(1);
}
No comments:
Post a Comment