Advertisement
Guest User

Untitled

a guest
Nov 25th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. package com.toppecraft.ToppeBattles.stats;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.ChatColor;
  11. import org.bukkit.entity.Player;
  12.  
  13. import com.toppecraft.ToppeBattles.ToppeBattles;
  14.  
  15. /**
  16. * MySQL system.
  17. * I'm not very familiar with the MySQL things so I'm sorry if it's badly made now.
  18. * @author Toppe5
  19. * @since 0.1
  20. */
  21. public class MySQL {
  22.  
  23. private String host;
  24. private int port;
  25. private String user;
  26. private String password;
  27. private String db;
  28. private Connection conn;
  29. public static final String TABLE = "stats";
  30.  
  31. /**
  32. * Create a new MySQL with the given values.
  33. * @param host
  34. * @param port
  35. * @param user
  36. * @param password
  37. * @param name
  38. */
  39. public MySQL(String host, int port, String user, String password, String name) {
  40. this.host = host;
  41. this.port = port;
  42. this.user = user;
  43. this.password = password;
  44. this.db = name;
  45. }
  46.  
  47. public boolean isConnected(boolean reconnect) {
  48. try {
  49. if(conn != null && !conn.isClosed() && conn.isValid(5000)) {
  50. return true;
  51. }
  52. } catch (SQLException e) {
  53. e.printStackTrace();
  54. }
  55. if(reconnect) connect();
  56. return false;
  57. }
  58.  
  59. public void connect() {
  60. if (!isConnected(false)) {
  61. try{
  62. conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/"+
  63. db, user, password);
  64. }catch (SQLException e){
  65. Bukkit.getLogger().warning("There was an error while connecting to the MySQL...");
  66. e.printStackTrace();
  67. return;
  68. }
  69. }
  70. }
  71.  
  72. public void createTable() {
  73. if(isConnected(true)) {
  74. query("CREATE TABLE IF NOT EXISTS " + TABLE + "(UUID VARCHAR(100), " + Stats.KILLS
  75. + " int, " + Stats.DEATHS + " int, " + Stats.ELO + " int, " + Stats.LMS + " int, "
  76. + Stats.BRACKETS + " int, " + Stats.PARTY_VS_PARTY_WINS + " int)");
  77. }
  78. }
  79.  
  80. public void close() {
  81. try {
  82. if (isConnected(false)) {
  83. conn.close();
  84. conn = null;
  85. }
  86. }catch (SQLException localSQLException) {}
  87. }
  88. public void query(String q) {
  89. query(q, true);
  90. }
  91.  
  92. private void query(String q, boolean first) {
  93. if (isConnected(true)) {
  94. PreparedStatement ps = null;
  95. try{
  96. ps = conn.prepareStatement(q);
  97. ps.execute();
  98. } catch (SQLException e) {
  99. if(first) {
  100. query(q, false);
  101. }
  102. else {
  103. e.printStackTrace();
  104. Bukkit.getScheduler().runTask(ToppeBattles.getInstance(), new Runnable() {
  105.  
  106. @Override
  107. public void run() {
  108. for(Player pl : Bukkit.getOnlinePlayers()) {
  109. if(pl.hasPermission("toppebattles.admin")) {
  110. pl.sendMessage(ChatColor.RED + "Failed to update data "
  111. + "(Query:" +q + ")");
  112. }
  113. }
  114. }
  115. });
  116. }
  117. } finally {
  118. if(ps != null) {
  119. try {
  120. ps.close();
  121. } catch (SQLException e) {
  122. e.printStackTrace();
  123. }
  124. }
  125. }
  126. }
  127. }
  128.  
  129. public ResultSet getResult(String q) {
  130. if (isConnected(true)) {
  131. Statement ps = null;
  132. try {
  133. ps = conn.createStatement();
  134. ResultSet rs = ps.executeQuery(q);
  135. return rs;
  136. } catch (SQLException e) {
  137. e.printStackTrace();
  138. Bukkit.getScheduler().runTask(ToppeBattles.getInstance(), new Runnable() {
  139.  
  140. @Override
  141. public void run() {
  142. for(Player pl : Bukkit.getOnlinePlayers()) {
  143. if(pl.hasPermission("toppebattles.admin")) {
  144. pl.sendMessage(ChatColor.RED + "Failed to collect data "
  145. + "(Query: " + q + ")");
  146. }
  147. }
  148. }
  149. });
  150. }
  151. }
  152. return null;
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement