Skip to content

Instantly share code, notes, and snippets.

@ilyash-b
Created September 7, 2022 18:18
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 ilyash-b/298cda6f02d86dbdd94ab7af4ded9f0c to your computer and use it in GitHub Desktop.
Save ilyash-b/298cda6f02d86dbdd94ab7af4ded9f0c to your computer and use it in GitHub Desktop.
Java Project refactor
public int getId(String queryId, String queryParameter, String objectName) {
try (PreparedStatement preparedStmt = getConnection().prepareStatement(queryId)) {
preparedStmt.setString(1, queryParameter);
ResultSet u = preparedStmt.executeQuery();
if (u.next()) {
return u.getInt("id");
}
throw new SQLException(objectName + " does not exists");
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public int getUserId2(User user) {
return getId("SELECT id FROM users WHERE userName= ? ", user.getUsername(), "User");
}
public int getGameId2(String gameName) {
return getId("SELECT id FROM games WHERE name= ? ", gameName, "Game");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment