Guest User

Untitled

a guest
Jul 22nd, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. package de.hannezhd.citybuildsystem.util;
  2.  
  3. import org.bukkit.configuration.file.FileConfiguration;
  4. import org.bukkit.configuration.file.YamlConfiguration;
  5.  
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12.  
  13. public class MYSQLManager {
  14. private FileConfiguration mysqlConfig;
  15. private String host;
  16. private int port;
  17. private String database;
  18. private String username;
  19. private String password;
  20. private Connection connection;
  21.  
  22. public MYSQLManager(File dataFolder) {
  23. File configFile = new File(dataFolder, "mysql.config");
  24.  
  25. if (!configFile.exists()) {
  26. try {
  27. configFile.createNewFile();
  28. mysqlConfig = YamlConfiguration.loadConfiguration(configFile);
  29.  
  30. // Setze Standardwerte
  31. mysqlConfig.set("mysql.host", "localhost");
  32. mysqlConfig.set("mysql.port", 3306);
  33. mysqlConfig.set("mysql.database", "meinedatenbank");
  34. mysqlConfig.set("mysql.username", "root");
  35. mysqlConfig.set("mysql.password", "passwort");
  36.  
  37. mysqlConfig.save(configFile);
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. } else {
  42. mysqlConfig = YamlConfiguration.loadConfiguration(configFile);
  43. }
  44.  
  45. loadConfig();
  46. connect();
  47. }
  48.  
  49. private void loadConfig() {
  50. host = mysqlConfig.getString("mysql.host");
  51. port = mysqlConfig.getInt("mysql.port");
  52. database = mysqlConfig.getString("mysql.database");
  53. username = mysqlConfig.getString("mysql.username");
  54. password = mysqlConfig.getString("mysql.password");
  55. }
  56.  
  57. private void connect() {
  58. try {
  59. String url = "jdbc:mysql://" + host + ":" + port + "/" + database;
  60. connection = DriverManager.getConnection(url, username, password);
  61. System.out.println("Connected to MySQL database.");
  62. } catch (SQLException e) {
  63. System.out.println("Failed to connect to MySQL database.");
  64. e.printStackTrace();
  65. connection = null;
  66. }
  67. }
  68.  
  69. public void disconnect() {
  70. try {
  71. if (connection != null && !connection.isClosed()) {
  72. connection.close();
  73. System.out.println("Disconnected from MySQL database.");
  74. }
  75. } catch (SQLException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. public void createTables() {
  80. try {
  81. Statement statement = connection.createStatement();
  82. String createTableQuery = "CREATE TABLE IF NOT EXISTS player_accounts ("
  83. + "player_name VARCHAR(255) PRIMARY KEY,"
  84. + "balance DOUBLE NOT NULL"
  85. + ")";
  86. statement.execute(createTableQuery);
  87. statement.close();
  88. } catch (Exception e) {
  89. e.printStackTrace();
  90. }
  91. }
  92.  
  93. public Connection getConnection() {
  94. return connection;
  95. }
  96.  
  97. }
  98.  
Add Comment
Please, Sign In to add comment