Skip to content

Instantly share code, notes, and snippets.

@steinarb
Created October 26, 2019 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steinarb/0e4c2c5e5ec35abdbf4d74f333e054f4 to your computer and use it in GitHub Desktop.
Save steinarb/0e4c2c5e5ec35abdbf4d74f333e054f4 to your computer and use it in GitHub Desktop.
@Component(service=UkelonnService.class, immediate=true)
public class UkelonnServiceProvider extends UkelonnServiceBase {
...
@Override
public Account getAccount(String username) {
try(Connection connection = database.getConnection()) {
try(PreparedStatement statement = connection.prepareStatement("select * from accounts_view where username=?")) {
statement.setString(1, username);
try(ResultSet resultset = statement.executeQuery()) {
if (resultset.next())
{
return mapAccount(resultset);
}
throw new UkelonnException(String.format("Got an empty ResultSet while fetching account from the database for user \\\"%s\\\"", username));
}
}
} catch (SQLException e) {
throw new UkelonnException(String.format("Caught SQLException while fetching account from the database for user \"%s\"", username), e);
}
}
public Account mapAccount(ResultSet results) throws SQLException {
String username = results.getString(UkelonnServiceProvider.USERNAME);
no.priv.bang.osgiservice.users.User user = useradmin.getUser(username);
return new Account(
results.getInt("account_id"),
username,
user.getFirstname(),
user.getLastname(),
results.getDouble("balance"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment