我想在我的表中添加一个空行,并在java中获取自动增量ID。我不知道为什么但我仍然得到userId = 0。 我的java类:
public class UserProfileDao {
private DBConnectionManager connectionManager;
private static final String INSERT_USER = "insert into user_profile values(default)";
private int userId;
public UserProfileDao(DBConnectionManager connectionManager) {
this.connectionManager = connectionManager;
try (Connection connection = connectionManager.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USER)) {
try (ResultSet generatedKey = preparedStatement.getGeneratedKeys()) {
if (generatedKey.next()) {
userId = generatedKey.getInt(1);
}
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public int getUserId() {
return userId;
}
}