Advertisement
Guest User

MySQL

a guest
Apr 30th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. main
  2. package de.wave.mysql.main;
  3.  
  4. import java.sql.PreparedStatement;
  5. import java.sql.SQLException;
  6.  
  7. import org.bukkit.plugin.java.JavaPlugin;
  8.  
  9.  
  10. import de.wave.mysql.mysql.MySQL;
  11. import de.wave.mysql.mysql.MySQLFile;
  12. import net.md_5.bungee.api.ChatColor;
  13.  
  14. public class main extends JavaPlugin {
  15.  
  16. public void onEnable() {
  17.  
  18. MySQLFile file = new MySQLFile();
  19. file.setStandard();
  20. file.readData();
  21.  
  22. MySQL.connect();
  23.  
  24. try {
  25. PreparedStatement ps = MySQL.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS test (UUID VARCHAR(1000),Spielername VARCHAR(100),Punkte INT(10000))");
  26. ps.executeUpdate();
  27. } catch (SQLException e) {
  28. e.printStackTrace();
  29. }
  30.  
  31. System.out.println(ChatColor.RED + "[MySQl]Startet");
  32. }
  33.  
  34. public void onDisable() {
  35.  
  36. MySQL.disconnect();
  37.  
  38. System.out.println(ChatColor.RED + "[MySQL]Stoppet");
  39. }
  40. }
  41. ---------------------------------------------------------------------------------------------------
  42. MYSQLFile
  43. package de.wave.mysql.mysql;
  44.  
  45. import java.io.File;
  46. import java.io.IOException;
  47.  
  48. import org.bukkit.configuration.file.FileConfiguration;
  49. import org.bukkit.configuration.file.YamlConfiguration;
  50.  
  51. public class MySQLFile {
  52.  
  53. public void setStandard() {
  54. FileConfiguration cfg = getFileConifuration();
  55.  
  56. cfg.options().copyDefaults(true);
  57.  
  58. cfg.addDefault("host", "localhost");
  59. cfg.addDefault("port", "3306");
  60. cfg.addDefault("database", "deine bank");
  61. cfg.addDefault("username", "root");
  62. cfg.addDefault("password", "pw");
  63.  
  64. try {
  65. cfg.save(getFile());
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. }
  69. }
  70.  
  71. private File getFile() {
  72. return new File("plugins/MySQL", "mysql.yml");
  73. }
  74.  
  75. private FileConfiguration getFileConifuration() {
  76. return YamlConfiguration.loadConfiguration(getFile());
  77. }
  78.  
  79. public void readData() {
  80. FileConfiguration cfg = getFileConifuration();
  81.  
  82. MySQL.host = cfg.getString("host");
  83. MySQL.port = cfg.getString("port");
  84. MySQL.database = cfg.getString("database");
  85. MySQL.username = cfg.getString("username");
  86. MySQL.password = cfg.getString("password");
  87. }
  88. }
  89. ------------------------------------------------------------------------------
  90. MySQL
  91. package de.wave.mysql.mysql;
  92.  
  93. import java.sql.Connection;
  94. import java.sql.DriverManager;
  95. import java.sql.SQLException;
  96.  
  97. public class MySQL {
  98.  
  99. public static String host;
  100. public static String port;
  101. public static String database;
  102. public static String username;
  103. public static String password;
  104. public static Connection con;
  105.  
  106. public static void connect() {
  107. if(!isConnected()) {
  108. try {
  109. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  110. System.out.println("[MySQL] Verbinding aufgebaut!");
  111. } catch (SQLException e) {
  112. e.printStackTrace();
  113. }
  114. }
  115. }
  116.  
  117. public static void disconnect() {
  118. if(isConnected()) {
  119. try {
  120. con.close();
  121. System.out.println("[MySQL] Verbindung geschlossen");
  122. } catch (SQLException e) {
  123. e.printStackTrace();
  124. }
  125. }
  126. }
  127.  
  128. public static boolean isConnected() {
  129. return (con == null ? false : true);
  130. }
  131.  
  132. public static Connection getConnection() {
  133. return con;
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement