Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. package de.lp.bs.sys.mysql.sys;
  2.  
  3. import de.lp.bs.sys.Bungee;
  4. import net.md_5.bungee.api.ChatColor;
  5. import net.md_5.bungee.api.ProxyServer;
  6. import net.md_5.bungee.config.Configuration;
  7. import net.md_5.bungee.config.ConfigurationProvider;
  8. import net.md_5.bungee.config.YamlConfiguration;
  9.  
  10. import java.io.File;
  11. import java.io.IOException;
  12. import java.sql.Connection;
  13. import java.sql.DriverManager;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16.  
  17. public class MYSQL {
  18.  
  19. public static String username;
  20. public static String password;
  21. public static String database;
  22. public static String host;
  23. public static String port;
  24. public static Connection con;
  25.  
  26.  
  27. public MYSQL() {}
  28.  
  29. public static boolean connect() {
  30. if (!isConnected()) {
  31. try {
  32. con = DriverManager.getConnection( "jdbc:mysql://" + host + ":" + port + "/" + database, username, password );
  33. ProxyServer.getInstance().getConsole().sendMessage( ChatColor.DARK_GREEN + " Datenbank Verbindindung aufgebaut..." );
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. ProxyServer.getInstance().getConsole().sendMessage( ChatColor.DARK_RED + " Keine Verbindung zur Datenbank!" );
  37. ProxyServer.getInstance().getConsole().sendMessage( ChatColor.DARK_RED + " Bitte pruefe die DB Einstellungen!" );
  38. }
  39. }
  40. return true;
  41. }
  42.  
  43. public static boolean isConnected() {return con != null;}
  44.  
  45. public static void createTable() {
  46. try{
  47. /** PLAYERLIST **/
  48. con.createStatement().executeQuery("");
  49. ProxyServer.getInstance().getConsole().sendMessage( ChatColor.DARK_GREEN + "MYSQL Tabelle wurde erstellt!" );
  50. }catch (SQLException e){
  51. e.printStackTrace();
  52. ProxyServer.getInstance().getConsole().sendMessage( ChatColor.DARK_RED + "MYSQL Tabelle konnte nicht erstellt werden!" );
  53. }
  54. }
  55.  
  56. public static void update(String qry) {
  57. if (!isConnected()) {
  58. try {
  59. con.createStatement().executeQuery( qry );
  60. }catch (SQLException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65.  
  66. public static ResultSet getResult(String qry){
  67. if (isConnected()){
  68. try {
  69. return con.createStatement().executeQuery( qry );
  70. }catch (SQLException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. return null;
  75. }
  76.  
  77. private static File getMySQLFile() {
  78. return new File( "plugins/ChatClear", "mysql.yml" );
  79. }
  80.  
  81. private static Configuration getMySQLFileConfig() {
  82. try {
  83. if (!Bungee.getInstance().getProxy().getPluginsFolder().exists()) {
  84. Bungee.getInstance().getProxy().getPluginsFolder().mkdir();
  85.  
  86. File file = new File(Bungee.getInstance().getDataFolder().getPath(), "mysql.lp");
  87. if (!file.exists()){
  88. file.createNewFile();
  89. }
  90. Configuration config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
  91. config.set("test", true);
  92. ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, file);
  93. }
  94. }catch (IOException e){
  95. e.printStackTrace();
  96. }
  97. return null;
  98. }
  99.  
  100. public static void setStandardMySQL() {
  101. Configuration cfg = getMySQLFileConfig();
  102. cfg.set( "username", "root" );
  103. cfg.set( "password" , "password" );
  104. cfg.set( "database", "database");
  105. cfg.set( "host" , "localhost");
  106. cfg.set( "port", "3306");
  107. try {
  108. ConfigurationProvider.getProvider(YamlConfiguration.class).save(cfg, getMySQLFile());
  109. }catch (IOException e) {
  110. e.printStackTrace();
  111. ProxyServer.getInstance().getConsole().sendMessage( ChatColor.DARK_RED + " Es konnte keine MySQL Datei erstellt werden!" );
  112. }
  113. }
  114. public static void readMysql() {
  115. Configuration cfg = getMySQLFileConfig();
  116.  
  117. MYSQL.username = cfg.getString( "username" );
  118. MYSQL.password = cfg.getString( "password" );
  119. MYSQL.database = cfg.getString( "database" );
  120. MYSQL.host = cfg.getString( "host" );
  121. MYSQL.port = cfg.getString( "port" );
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement