stmt.setString(2, basicPassword);
stmt.setString(3, digestPassword);
stmt.setString(4, md5Password(name, password));
int nrows = stmt.executeUpdate();
if (nrows != 1) {
throw new SecurityException("Failed to add user " + name);
}
ResultSet rs = stmt.getGeneratedKeys();
if (rs.next()) {
id = rs.getInt(1);
}
else {
throw new SecurityException("Failed to get user id for " + name);
}
rs.close();
stmt.close();
stmt = null;
stmt = conn.prepareStatement(ADD_USER_ROLE_SQL);
stmt.setInt(1, id);
for (String role : roles) {
stmt.setString(2, role);
nrows = stmt.executeUpdate();
if (nrows != 1) {
throw new SecurityException("Failed to add role " + role);
}
}
conn.commit();
}
catch (SQLException ex) {
throw new SecurityException("Error adding user", ex);
}
finally {
cleanup(conn, stmt);
}
return new User(id, name, basicPassword, digestPassword, new ArrayList<>(roles));