Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. package de.xCracked.MelonePvP.MySQL;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. import org.bukkit.Bukkit;
  10.  
  11. import com.mysql.jdbc.PreparedStatement;
  12.  
  13. import de.xCracked.MelonePvP.Main;
  14.  
  15. public class MySQL
  16. {
  17. public static Connection con;
  18.  
  19. public static void connect()
  20. {
  21. try
  22. {
  23. con = DriverManager.getConnection("jdbc:mysql://" + Main.host + ":3306/" + Main.database + "?autoreconnect=true", Main.user, Main.password);
  24. Bukkit.getConsoleSender().sendMessage("§8(§b§lMySQL§8) §aDie Verbindung wurde hergestellt.");
  25. onReconnectScheduler();
  26. }
  27. catch (SQLException e)
  28. {
  29. Bukkit.getConsoleSender().sendMessage("§8(§e§lMySQL§8) §cDie Verbindung konnte nicht hergestellt werden.");
  30. System.err.println(e);
  31. }
  32. }
  33.  
  34. public static boolean hasConnection()
  35. {
  36. return con != null;
  37. }
  38.  
  39. public static void close()
  40. {
  41. try
  42. {
  43. if (con != null)
  44. {
  45. con.close();
  46. con = null;
  47. Bukkit.getConsoleSender().sendMessage("§8(§e§lMySQL§8) §2Die Verbindung wurde erfolgreich getrennt.");
  48. }
  49. }
  50. catch (SQLException e)
  51. {
  52. Bukkit.getConsoleSender().sendMessage("§8(§e§lMySQL§8) §cEs gab ein Fehler bei der abschlie§ung");
  53. System.err.println(e);
  54. }
  55. }
  56.  
  57. public static void update(String qry)
  58. {
  59. try
  60. {
  61. Statement stmt = con.createStatement();
  62. stmt.executeUpdate(qry);
  63. }
  64. catch (Exception e)
  65. {
  66. System.err.println(e);
  67. }
  68. }
  69.  
  70. public static PreparedStatement getStatement(String sql)
  71. {
  72. if (hasConnection()) {
  73. try
  74. {
  75. return (PreparedStatement)con.prepareStatement(sql);
  76. }
  77. catch (SQLException e)
  78. {
  79. try
  80. {
  81. con = DriverManager.getConnection("jdbc:mysql://" + Main.host + ":3306/" + Main.database + "?autoreconnect=true", Main.user, Main.password);
  82. }
  83. catch (SQLException e1)
  84. {
  85. e1.printStackTrace();
  86. }
  87. e.printStackTrace();
  88. }
  89. }
  90. return null;
  91. }
  92.  
  93. public static ResultSet getResult(String sql)
  94. {
  95. if (hasConnection()) {
  96. try
  97. {
  98. PreparedStatement ps = getStatement(sql);
  99. return ps.executeQuery();
  100. }
  101. catch (SQLException e)
  102. {
  103. try
  104. {
  105. con = DriverManager.getConnection("jdbc:mysql://" + Main.host + ":3306/" + Main.database + "?autoreconnect=true", Main.user, Main.password);
  106. }
  107. catch (SQLException e1)
  108. {
  109. e1.printStackTrace();
  110. }
  111. e.printStackTrace();
  112. }
  113. }
  114. return null;
  115. }
  116.  
  117. public static Connection getConnection()
  118. {
  119. return con;
  120. }
  121.  
  122. public static void createTable()
  123. {
  124. if (hasConnection()) {
  125. try
  126. {
  127. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS Melone_Bans (Spielername VARCHAR(100), UUID VARCHAR(100), Ende VARCHAR(100), Wann VARCHAR(100), Grund VARCHAR(100), Banner VARCHAR(100))");
  128. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS Melone_Mutes (Spielername VARCHAR(100), UUID VARCHAR(100), Ende VARCHAR(100), Grund VARCHAR(100), Admin VARCHAR(100))");
  129. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS Melone_Stats (UUID VARCHAR(100), KILLS int, DEATHS int)");
  130. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS Coins (Spielername VARCHAR(100), UUID VARCHAR(100), Coins INT(100))");
  131. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS Spins (Spielername VARCHAR(100), UUID VARCHAR(100), Spins INT(100))");
  132. }
  133. catch (SQLException e)
  134. {
  135. e.printStackTrace();
  136. }
  137. }
  138. }
  139.  
  140. private static void onReconnectScheduler()
  141. {
  142. Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getInstance(), new Runnable()
  143. {
  144. public void run() {}
  145. }, 432000L, 432000L);
  146. }
  147.  
  148. private static void onReconnect()
  149. {
  150. if (con != null) {
  151. try
  152. {
  153. con.close();
  154. }
  155. catch (SQLException e)
  156. {
  157. e.printStackTrace();
  158. }
  159. }
  160. Bukkit.getScheduler().scheduleSyncDelayedTask(Main.getInstance(), new Runnable()
  161. {
  162. public void run()
  163. {
  164. try
  165. {
  166. MySQL.con = DriverManager.getConnection("jdbc:mysql://" + Main.host + ":3306/" + Main.database + "?autoreconnect=true", Main.user, Main.password);
  167. }
  168. catch (SQLException e)
  169. {
  170. e.printStackTrace();
  171. }
  172. }
  173. }, 1L);
  174. }
  175. }
  176.  
  177. // MYSQL - DATEN //
  178.  
  179. public static String host = "localhost";
  180. public static String user = "MeloneSpieler";
  181. public static String password = "f9nP31LWyxHiyGS5";
  182. public static int port = 3306;
  183. public static String database = "MeloneSpieler";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement