Created
October 26, 2019 08:40
-
-
Save steinarb/0e4c2c5e5ec35abdbf4d74f333e054f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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