Advertisement
Guest User

Untitled

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