Advertisement
Guest User

Untitled

a guest
Dec 8th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. package de.****.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.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.configuration.file.YamlConfiguration;
  13.  
  14. public class MySQL {
  15.  
  16. // YOU NEED TO CREATE THE FILE BEFORE RUNNING MYSQL
  17.  
  18. public static String HOST = "";
  19. public static String USERNAME = "";
  20. public static String PASSWORD = "";
  21. public static String DATABASE = "";
  22. public static int port;
  23. public static Connection connection;
  24.  
  25. public MySQL(String host, String username, String password, String database) {
  26. HOST = host;
  27. USERNAME = username;
  28. PASSWORD = password;
  29. DATABASE = database;
  30.  
  31. }
  32.  
  33. public static void connect() {
  34. if (!isConnected()) {
  35. try {
  36. connection = DriverManager.getConnection("jdbc:mysql://" + HOST + ":" + "3306" + "/" + DATABASE + "?autoReconnect=true", USERNAME, PASSWORD);
  37. Bukkit.getConsoleSender().sendMessage("§aVerbindung erfolgreich aufgebaut!");
  38. } catch (SQLException ex) {
  39. Bukkit.getConsoleSender().sendMessage("§cVerbindung wurde nicht aufgebaut!");
  40. }
  41. }
  42. }
  43.  
  44. public void disconnect() {
  45. if (isConnected()) {
  46. try {
  47. connection.close();
  48. Bukkit.getConsoleSender().sendMessage("§cVerbindung erfolgreich geschlossen!");
  49. } catch (SQLException localSQLException) {
  50. }
  51. }
  52. }
  53.  
  54. public static boolean isConnected() {
  55. return connection != null;
  56. }
  57.  
  58. public Connection getConnection() {
  59. return connection;
  60. }
  61.  
  62. public static void update(String qry) {
  63. try {
  64. if (connection.isClosed()) {
  65. connect();
  66. }
  67. Statement stmt = connection.createStatement();
  68. stmt.executeUpdate(qry);
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. }
  73.  
  74. public static ResultSet query(String query) throws SQLException {
  75. if (connection.isClosed()) {
  76. connect();
  77. }
  78. Statement statement = connection.createStatement();
  79. return statement.executeQuery(query);
  80. }
  81.  
  82. /*
  83. * Methods
  84. */
  85.  
  86. public static void connectToMySQL() {
  87. File file = new File("plugins/LPS/MySQL.yml");
  88. YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  89. new MySQL(cfg.getString("host"), cfg.getString("user"), cfg.getString("password"),cfg.getString("database"));
  90. }
  91.  
  92. public static void createMySQL() {
  93. File file = new File("plugins/LPS/MySQL.yml");
  94. YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  95. try {
  96. if (!file.exists()) {
  97. file.createNewFile();
  98. cfg.options().copyDefaults(true);
  99. cfg.addDefault("host", "IP");
  100. cfg.addDefault("user", "Username");
  101. cfg.addDefault("password", "Passwort");
  102. cfg.addDefault("database", "Databasename");
  103.  
  104. cfg.save(file);
  105. }
  106. } catch (IOException e) {
  107. e.printStackTrace();
  108. }
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement