Advertisement
MrObst

MySQL

May 10th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. package me.MrObst.SkyWars.mysql;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.configuration.file.FileConfiguration;
  12. import org.bukkit.configuration.file.YamlConfiguration;
  13.  
  14. public class MySQL
  15. {
  16. public static String username;
  17. public static String password;
  18. public static String database;
  19. public static String host;
  20. public static String port;
  21. public static Connection con;
  22.  
  23. public MySQL(String user, String pass, String host2, String dB) {}
  24.  
  25. public static void connect()
  26. {
  27. if (!isConnected()) {
  28. try
  29. {
  30. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  31. Bukkit.getConsoleSender().sendMessage("§4MySQL MySQL connected");
  32. }
  33. catch (SQLException e)
  34. {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39.  
  40. public static void close()
  41. {
  42. if (isConnected()) {
  43. try
  44. {
  45. con.close();
  46. Bukkit.getConsoleSender().sendMessage("§4MySQL MySQL disconnected");
  47. }
  48. catch (SQLException e)
  49. {
  50. e.printStackTrace();
  51. }
  52. }
  53. }
  54.  
  55. public static boolean isConnected()
  56. {
  57. return con != null;
  58. }
  59.  
  60. public static void createTable()
  61. {
  62. if (isConnected()) {
  63. try
  64. {
  65. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS CoinsAPI (UUID VARCHAR(100), KILLS int, DEATHS int , WIN int , LOSE int , COINS int)");
  66. Bukkit.getConsoleSender().sendMessage("§4MySQL MySQL Table created");
  67. }
  68. catch (SQLException e)
  69. {
  70. e.printStackTrace();
  71. }
  72. }
  73. }
  74.  
  75. public static void update(String qry)
  76. {
  77. if (isConnected()) {
  78. try
  79. {
  80. con.createStatement().executeUpdate(qry);
  81. }
  82. catch (SQLException e)
  83. {
  84. e.printStackTrace();
  85. }
  86. }
  87. }
  88.  
  89. public static ResultSet getResult(String qry)
  90. {
  91. ResultSet rs = null;
  92. try
  93. {
  94. Statement st = con.createStatement();
  95. rs = st.executeQuery(qry);
  96. }
  97. catch (SQLException e)
  98. {
  99. connect();
  100. System.err.println(e);
  101. }
  102. return rs;
  103. }
  104.  
  105. public static File getMySQLFile()
  106. {
  107. return new File("plugins/StatsAPI", "MySQL.yml");
  108. }
  109.  
  110. public static FileConfiguration getMySQLFileConfiguration()
  111. {
  112. return YamlConfiguration.loadConfiguration(getMySQLFile());
  113. }
  114.  
  115. public static void setStandardMySQL()
  116. {
  117. FileConfiguration cfg = getMySQLFileConfiguration();
  118.  
  119. cfg.options().copyDefaults(true);
  120. cfg.addDefault("username", "root");
  121. cfg.addDefault("password", "password");
  122. cfg.addDefault("database", "localhost");
  123. cfg.addDefault("host", "localhost");
  124. cfg.addDefault("port", "3306");
  125. try
  126. {
  127. cfg.save(getMySQLFile());
  128. }
  129. catch (IOException e)
  130. {
  131. e.printStackTrace();
  132. }
  133. }
  134.  
  135. public static void readMySQL()
  136. {
  137. FileConfiguration cfg = getMySQLFileConfiguration();
  138. username = cfg.getString("username");
  139. password = cfg.getString("password");
  140. database = cfg.getString("database");
  141. host = cfg.getString("host");
  142. port = cfg.getString("port");
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement