Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. /* 1: */ package mintcraftian.stats.mysql;
  2. /* 2: */
  3. /* 3: */ import java.sql.Connection;
  4. /* 4: */ import java.sql.DriverManager;
  5. /* 5: */ import java.sql.PreparedStatement;
  6. /* 6: */ import java.sql.ResultSet;
  7. /* 7: */ import java.sql.SQLException;
  8. /* 8: */ import mintcraftian.stats.Main;
  9. /* 9: */ import org.bukkit.configuration.file.FileConfiguration;
  10. /* 10: */ import org.bukkit.scheduler.BukkitRunnable;
  11. /* 11: */
  12. /* 12: */ public class MySQL
  13. /* 13: */ {
  14. /* 14: */ private String address;
  15. /* 15: */ private String database;
  16. /* 16: */ private String pass;
  17. /* 17: */ private String username;
  18. /* 18: */ private Connection connection;
  19. /* 19: */
  20. /* 20: */ public MySQL(Main instance)
  21. /* 21: */ {
  22. /* 22: 19 */ this.address = instance.getConfig().getString("Address");
  23. /* 23: 20 */ this.database = instance.getConfig().getString("Database");
  24. /* 24: 21 */ this.pass = instance.getConfig().getString("Password");
  25. /* 25: 22 */ this.username = instance.getConfig().getString("Username");
  26. /* 26: 23 */ connect();
  27. /* 27: */ }
  28. /* 28: */
  29. /* 29: */ public synchronized boolean connect()
  30. /* 30: */ {
  31. /* 31: */ try
  32. /* 32: */ {
  33. /* 33: 28 */ this.connection = DriverManager.getConnection("jdbc:mysql://" + this.address + "/" + this.database, this.username, this.pass);
  34. /* 34: 29 */ connectionCheck();
  35. /* 35: 30 */ return true;
  36. /* 36: */ }
  37. /* 37: */ catch (SQLException e)
  38. /* 38: */ {
  39. /* 39: 32 */ e.printStackTrace();
  40. /* 40: */ }
  41. /* 41: 33 */ return false;
  42. /* 42: */ }
  43. /* 43: */
  44. /* 44: */ private void connectionCheck()
  45. /* 45: */ {
  46. /* 46: 50 */ new BukkitRunnable()
  47. /* 47: */ {
  48. /* 48: */ public void run()
  49. /* 49: */ {
  50. /* 50: */ try
  51. /* 51: */ {
  52. /* 52: 42 */ MySQL.this.connection.close();
  53. /* 53: */ }
  54. /* 54: */ catch (SQLException e)
  55. /* 55: */ {
  56. /* 56: 44 */ e.printStackTrace();
  57. /* 57: */ }
  58. /* 58: 46 */ cancel();
  59. /* 59: 47 */ MySQL.this.connect();
  60. /* 60: */ }
  61. /* 61: 50 */ }.runTaskLater(Main.getInstance(), 2400L);
  62. /* 62: */ }
  63. /* 63: */
  64. /* 64: */ public Object getValue(String uuid, String table, String column, boolean isString)
  65. /* 65: */ {
  66. /* 66: 54 */ if (this.connection == null) {
  67. /* 67: 55 */ connect();
  68. /* 68: */ }
  69. /* 69: 57 */ String query = "SELECT * FROM `" + table + "` WHERE `name`='" + uuid + "'";
  70. /* 70: */ try
  71. /* 71: */ {
  72. /* 72: 60 */ ResultSet res = this.connection.prepareStatement(query).executeQuery();
  73. /* 73: */
  74. /* 74: 62 */ Object value = null;
  75. /* 75: 63 */ while (res.next()) {
  76. /* 76: 64 */ if (!isString) {
  77. /* 77: 65 */ value = res.getObject(column);
  78. /* 78: */ } else {
  79. /* 79: 67 */ value = res.getString(column);
  80. /* 80: */ }
  81. /* 81: */ }
  82. /* 82: 71 */ res.close();
  83. /* 83: 72 */ return value;
  84. /* 84: */ }
  85. /* 85: */ catch (SQLException e)
  86. /* 86: */ {
  87. /* 87: 74 */ e.printStackTrace();
  88. /* 88: */ }
  89. /* 89: 75 */ return null;
  90. /* 90: */ }
  91. /* 91: */
  92. /* 92: */ public void setValue(String uuid, String table, String column, Object value)
  93. /* 93: */ {
  94. /* 94: 80 */ if (this.connection == null) {
  95. /* 95: 81 */ connect();
  96. /* 96: */ }
  97. /* 97: */ try
  98. /* 98: */ {
  99. /* 99: 84 */ String query = "";
  100. /* 100: 85 */ if ((value instanceof String))
  101. /* 101: */ {
  102. /* 102: 86 */ if (containsElement(table, "name", uuid)) {
  103. /* 103: 87 */ query = "UPDATE `" + table + "` SET `name`='" + uuid + "',`" + column + "`='" + value + "' WHERE `name`='" + uuid + "'";
  104. /* 104: */ } else {
  105. /* 105: 89 */ query = "INSERT INTO `" + table + "`(`name`, `" + column + "`) VALUES ('" + uuid + "','" + value + "')";
  106. /* 106: */ }
  107. /* 107: */ }
  108. /* 108: 92 */ else if (containsElement(table, "name", uuid)) {
  109. /* 109: 93 */ query = "UPDATE `" + table + "` SET `name`='" + uuid + "',`" + column + "`=" + value + " WHERE `name`='" + uuid + "'";
  110. /* 110: */ } else {
  111. /* 111: 95 */ query = "INSERT INTO `" + table + "`(`name`, `" + column + "`) VALUES ('" + uuid + "'," + value + ")";
  112. /* 112: */ }
  113. /* 113: 99 */ PreparedStatement p = this.connection.prepareStatement(query);
  114. /* 114:100 */ p.execute();
  115. /* 115:101 */ p.close();
  116. /* 116: */ }
  117. /* 117: */ catch (SQLException e)
  118. /* 118: */ {
  119. /* 119:103 */ e.printStackTrace();
  120. /* 120: */ }
  121. /* 121: */ }
  122. /* 122: */
  123. /* 123: */ public boolean containsElement(String table, String column, Object element)
  124. /* 124: */ {
  125. /* 125:108 */ if (this.connection == null) {
  126. /* 126:109 */ connect();
  127. /* 127: */ }
  128. /* 128:111 */ String query = "SELECT `" + column + "` FROM `" + table + "` WHERE `" + column + "`='" + element + "'";
  129. /* 129: */ try
  130. /* 130: */ {
  131. /* 131:113 */ ResultSet res = this.connection.prepareStatement(query).executeQuery();
  132. /* 132:114 */ boolean temp = res.next();
  133. /* 133:115 */ res.close();
  134. /* 134:116 */ return temp;
  135. /* 135: */ }
  136. /* 136: */ catch (SQLException e)
  137. /* 137: */ {
  138. /* 138:118 */ e.printStackTrace();
  139. /* 139: */ }
  140. /* 140:119 */ return true;
  141. /* 141: */ }
  142. /* 142: */ }
  143.  
  144.  
  145.  
  146. /* Location: C:\Users\Internet\Desktop\Stats\bin\
  147.  
  148. * Qualified Name: mintcraftian.stats.mysql.MySQL
  149.  
  150. * JD-Core Version: 0.7.0.1
  151.  
  152. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement