Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. **
  2. * Query a command to get its value in an async task
  3. *
  4. * @param statement : The statement
  5. * @param row : The row
  6. */
  7. public void queryRowAsync(String statement, String row, SqlCallback<List<Object>> callback) {
  8. if (!checkConnection()) throw new IllegalStateException("Cannot connect into the database!");
  9. Bukkit.getScheduler().runTaskAsynchronously(getOwningPlugin(), () -> {
  10. PreparedStatement ps = null;
  11. ResultSet rs;
  12. List<Object> values = new ArrayList<>();
  13. try {
  14. ps = connection.prepareStatement(statement);
  15. rs = ps.executeQuery();
  16. while (rs.next()) {
  17. values.add(rs.getObject(row));
  18. }
  19. callback.onSuccess(values);
  20. } catch (SQLException ex) {
  21. try {
  22. if (ps != null) {
  23. ps.close();
  24. }
  25. if (connection != null) {
  26. connection.close();
  27. }
  28. callback.onError(ex);
  29. } catch (SQLException ex2) {
  30. callback.onError(ex2);
  31. }
  32. }
  33. finally {
  34. try {
  35. if (ps != null) {
  36. ps.close();
  37. }
  38. if (connection != null) {
  39. connection.close();
  40. }
  41. }
  42. catch (SQLException ex2) {
  43. callback.onError(ex2);
  44. }
  45. }
  46. });
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement