Advertisement
Guest User

Untitled

a guest
Mar 16th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. package fr.lyntcraftapi.mysql;
  2.  
  3. import java.sql.*;
  4.  
  5. import org.bukkit.entity.Player;
  6.  
  7. public class LyntMaintenanceAPI {
  8.  
  9. private String url_base, host, name, user, pass, table;
  10. private Connection connection;
  11.  
  12. public LyntMaintenanceAPI(String url_base, String host, String name, String user, String pass, String table) {
  13. this.url_base = url_base;
  14. this.host = host;
  15. this.name = name;
  16. this.user = user;
  17. this.pass = pass;
  18. this.table = table;
  19. }
  20.  
  21. public void connection() {
  22. if (!isConnected()) {
  23. try {
  24. connection = DriverManager.getConnection(url_base + host + "/" + name, user, pass);
  25. } catch (SQLException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30.  
  31. public void deconnection() {
  32. if (isConnected()) {
  33. try {
  34. connection.close();
  35. } catch (SQLException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40.  
  41. private boolean isConnected() {
  42. try {
  43. if ((connection == null) || (connection.isClosed()) || (connection.isValid(5))) {
  44. return false;
  45. } else {
  46. return true;
  47. }
  48. } catch (SQLException e) {
  49. e.printStackTrace();
  50. }
  51. return false;
  52. }
  53.  
  54. private Connection getConnection() {
  55. return connection;
  56. }
  57.  
  58. public void createAccount(Player p) {
  59. try {
  60. PreparedStatement sts = getConnection().prepareStatement("SELECT mn FROM " + table + " WHERE uuid = ?");
  61. sts.setString(1, p.getUniqueId().toString());
  62. ResultSet rs = sts.executeQuery();
  63. if (!rs.next()) {
  64. sts.close();
  65.  
  66. PreparedStatement sts2 = getConnection()
  67. .prepareStatement("INSERT INTO " + table + " (uuid, mn) VALUES (?, ?)");
  68. sts2.setString(1, p.getUniqueId().toString());
  69. sts2.setInt(2, 0);
  70. sts2.executeUpdate();
  71. }
  72. } catch (SQLException e) {
  73. e.printStackTrace();
  74. }
  75. }
  76.  
  77. public void createMaintenance() {
  78. try {
  79. PreparedStatement sts = getConnection().prepareStatement("SELECT mn FROM " + table + " WHERE uuid = ?");
  80. sts.setString(1, "maintenance");
  81. ResultSet rs = sts.executeQuery();
  82. if (!rs.next()) {
  83. sts.close();
  84.  
  85. PreparedStatement sts2 = getConnection()
  86. .prepareStatement("INSERT INTO " + table + " (uuid, mn) VALUES (?, ?)");
  87. sts2.setString(1, "maintenance");
  88. sts2.setBoolean(2, false);;
  89. sts2.executeUpdate();
  90. }
  91. } catch (SQLException e) {
  92. e.printStackTrace();
  93. }
  94. }
  95.  
  96. public void setMaintenanceOn() {
  97. try {
  98. PreparedStatement sts = getConnection()
  99. .prepareStatement("UPDATE " + table + " SET mn = ? WHERE uuid = ?");
  100. sts.setBoolean(1, true);
  101. sts.setString(2, "maintenance");
  102. sts.executeUpdate();
  103. sts.close();
  104. } catch (SQLException e) {
  105. e.printStackTrace();
  106. }
  107. }
  108.  
  109. public void setMaintenanceOff() {
  110. try {
  111. PreparedStatement sts = getConnection()
  112. .prepareStatement("UPDATE " + table + " SET mn = ? WHERE uuid = ?");
  113. sts.setBoolean(1, false);
  114. sts.setString(2, "maintenance");
  115. sts.executeUpdate();
  116. sts.close();
  117. } catch (SQLException e) {
  118. e.printStackTrace();
  119. }
  120. }
  121.  
  122. public void addMaintenance(String uuid) {
  123. try {
  124. PreparedStatement sts = getConnection()
  125. .prepareStatement("UPDATE " + table + " SET mn = ? WHERE uuid = ?");
  126. sts.setBoolean(1, true);
  127. sts.setString(2, uuid);
  128. sts.executeUpdate();
  129. sts.close();
  130. } catch (SQLException e) {
  131. e.printStackTrace();
  132. }
  133. }
  134.  
  135. public void removeMaintenance(String uuid) {
  136. try {
  137. PreparedStatement sts = getConnection()
  138. .prepareStatement("UPDATE " + table + " SET mn = ? WHERE uuid = ?");
  139. sts.setBoolean(1, false);
  140. sts.setString(2, uuid);
  141. sts.executeUpdate();
  142. sts.close();
  143. } catch (SQLException e) {
  144. e.printStackTrace();
  145. }
  146. }
  147.  
  148. public boolean isMaintenance() {
  149. boolean mn = false;
  150.  
  151. try {
  152. PreparedStatement sts = getConnection().prepareStatement("SELECT mn FROM " + table + " WHERE uuid = ?");
  153. sts.setString(1, "maintenance");
  154. ResultSet rs = sts.executeQuery();
  155. if (!rs.next()) {
  156. return mn;
  157. }
  158. mn = rs.getBoolean("mn");
  159. sts.close();
  160. } catch (SQLException e) {
  161. e.printStackTrace();
  162. }
  163.  
  164. return mn;
  165. }
  166.  
  167. public boolean playerIsMaintenance(String uuid) {
  168. boolean mn = false;
  169.  
  170. try {
  171. PreparedStatement sts = getConnection().prepareStatement("SELECT mn FROM " + table + " WHERE uuid = ?");
  172. sts.setString(1, uuid);
  173. ResultSet rs = sts.executeQuery();
  174. if (!rs.next()) {
  175. return mn;
  176. }
  177. mn = rs.getBoolean("mn");
  178. sts.close();
  179. } catch (SQLException e) {
  180. e.printStackTrace();
  181. }
  182.  
  183. return mn;
  184. }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement