Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. package net.socialgames.common_db;
  2.  
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5.  
  6. import net.socialgames.common_db.Connection;
  7.  
  8. public class Credits {
  9. private static Table table;
  10.  
  11. public static void initialize() {
  12. if(!Connection.isOpen()) {
  13. System.out.println("(CREDITS) Could not initialize: Database is not open yet");
  14. return;
  15. }
  16.  
  17. if(!Connection.existsTable("credits")) {
  18. Connection.createTable("credits",
  19. new String[]{
  20. "uuid varchar(60) NOT NULL UNIQUE",
  21. "credits int default 0",
  22. "primary key (uuid)"
  23. }
  24. );
  25. }
  26.  
  27. System.out.println("(CREDITS) Initialized successfully");
  28. }
  29.  
  30. protected static int get(String uuid) {
  31. try {
  32. ResultSet res = table.select(new String[] { "credits" }, "uuid = \"" + uuid + "\"");
  33. if (res == null || !res.next()) {
  34. // Either error or no language preference
  35. return 0;
  36. }
  37.  
  38. return res.getInt("credits");
  39. } catch (SQLException e) {
  40. System.out.println("Could not get credits for " + uuid);
  41. e.printStackTrace();
  42. return -1;
  43. }
  44. }
  45.  
  46. protected static void save(String uuid, int credits) {
  47. try {
  48. table.insert(new String[] { "uuid", "credits" }, new String[] { "\"" + uuid + "\"", Integer.toString(credits) },
  49. "credits=" + Integer.toString(credits) // On duplicate
  50. );
  51. } catch (SQLException e) {
  52. System.out.println("Could not save credits for " + uuid);
  53. e.printStackTrace();
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement