Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. package de.leon.ascalterfriends.sql;
  2.  
  3. import net.md_5.bungee.config.Configuration;
  4. import net.md_5.bungee.config.ConfigurationProvider;
  5. import net.md_5.bungee.config.YamlConfiguration;
  6.  
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.sql.*;
  10.  
  11. public class MySQL {
  12.  
  13. public static Connection publiccon;
  14.  
  15. private static Connection privatecon;
  16.  
  17. public static String host = "";
  18. public static String datenbankname = "";
  19. public static String benutzername = "";
  20. public static String passwort = "";
  21.  
  22. public static void connect() {
  23. try {
  24. publiccon = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + datenbankname + "?autoReconnect=true", benutzername, passwort);
  25. System.out.println("§aDie MySQL Verbindung konnte hergestellt werden");
  26. try {
  27. File mysql = new File("plugins/system/mysql.yml");
  28. Configuration cfg;
  29.  
  30. cfg = ConfigurationProvider.getProvider(YamlConfiguration.class).load(mysql);
  31.  
  32.  
  33.  
  34. host = cfg.getString("MySQL.host");
  35. benutzername = cfg.getString("MySQL.benutzername");
  36. passwort = cfg.getString("MySQL.passwort");
  37. datenbankname = cfg.getString("MySQL.datenbankname");
  38.  
  39. } catch (Exception e1) {
  40. e1.printStackTrace();
  41. }
  42.  
  43.  
  44.  
  45. } catch (SQLException e) {
  46. System.out.println("§cDie MySQL Verbindung ist gescheitert, folgender Fehler liegt vor: §7");
  47. e.printStackTrace();
  48. }
  49. }
  50.  
  51. public static void disconnect() {
  52. if (publiccon != null) {
  53. try {
  54. publiccon.close();
  55. } catch (SQLException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. }
  60.  
  61.  
  62. public static boolean isRegistered(String uuid) {
  63. try {
  64. PreparedStatement tabelle = publiccon.prepareStatement("SELECT * FROM Info WHERE uuid=?");
  65. tabelle.setString(1, uuid);
  66.  
  67. ResultSet result = tabelle.executeQuery();
  68.  
  69. boolean Contains = result.next();
  70.  
  71. tabelle.close();
  72. result.close();
  73.  
  74. return Contains;
  75. } catch (Exception e) {
  76. e.printStackTrace();
  77. }
  78. return false;
  79. }
  80.  
  81. public static void register(String uuid) {
  82. try {
  83. PreparedStatement tabelle = publiccon.prepareStatement("INSERT INTO Info values(?)");
  84. tabelle.setString(1, uuid);
  85.  
  86. tabelle.execute();
  87. tabelle.close();
  88. } catch (Exception e) {
  89. e.printStackTrace();
  90. }
  91. }
  92.  
  93. public static void update(String qry) {
  94. Statement st = null;
  95. try {
  96. st = publiccon.createStatement();
  97. st.executeUpdate(qry);
  98. } catch (Exception e) {
  99. connect();
  100. System.err.println(e);
  101. }
  102. closeStatement(st);
  103. }
  104.  
  105. public static void closeStatement(Statement st) {
  106. if (st != null)
  107. try {
  108. st.close();
  109. } catch (Exception e) {
  110. e.printStackTrace();
  111. }
  112. }
  113.  
  114. public static void createTables() {
  115.  
  116. MySQL.update("CREATE TABLE IF NOT EXISTS Online(name varchar(32),Server varchar(200))");
  117. MySQL.update("CREATE TABLE IF NOT EXISTS Freunde(uuid varchar(100),uuidfriend varchar(100))");
  118. MySQL.update("CREATE TABLE IF NOT EXISTS Info(name varchar(32),uuid varchar(100))");
  119.  
  120. }
  121.  
  122. public static void setOrdnerAndConfig() {
  123.  
  124. File ordner = new File("plugins/system");
  125.  
  126. if (!ordner.exists()) {
  127. ordner.mkdir();
  128. }
  129. File mysql = new File("plugins/system/mysql.yml");
  130. if (!mysql.exists()) {
  131. try {
  132. mysql.createNewFile();
  133. } catch (IOException e) {
  134. e.printStackTrace();
  135. }
  136. } else {
  137. return;
  138. }
  139. try {
  140. Configuration cfg = ConfigurationProvider.getProvider(YamlConfiguration.class).load(mysql);
  141. cfg.set("MySQL.host", "localhost");
  142. cfg.set("MySQL.benutzername", "root");
  143. cfg.set("MySQL.passwort", "123");
  144. cfg.set("MySQL.datanbankname", "freundesys");
  145. ConfigurationProvider.getProvider(YamlConfiguration.class).save(cfg, mysql);
  146.  
  147. } catch (IOException e) {
  148. e.printStackTrace();
  149. }
  150. }
  151.  
  152.  
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement