Advertisement
Guest User

Untitled

a guest
Mar 9th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. package Util;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.command.ConsoleCommandSender;
  11.  
  12. import main.MainClass;
  13.  
  14. public class MySQL {
  15.  
  16. public static String username;
  17. public static String password;
  18. public static String database;
  19. public static String host;
  20. public static String port;
  21.  
  22. public static Connection con;
  23.  
  24. public static void connect() {
  25. if(!isConnected()) {
  26.  
  27. try {
  28. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
  29. ConsoleCommandSender sender = Bukkit.getConsoleSender();
  30. sender.sendMessage(MainClass.getInstance().pr + "§aMySQL-Verbindung wurde erfolgreich aufgebaut.");
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. }
  34.  
  35. }
  36.  
  37. }
  38. public static void close() {
  39. if(isConnected()) {
  40. try {
  41. con.close();
  42. ConsoleCommandSender sender = Bukkit.getConsoleSender();
  43. sender.sendMessage(MainClass.getInstance().pr + "§aMySQL-Verbindung wurde erfolgreich beendet.");
  44.  
  45. } catch (SQLException e) {
  46. e.printStackTrace();
  47. }
  48.  
  49. }
  50.  
  51. }
  52. public static boolean isConnected() {
  53. return con != null;
  54.  
  55. }
  56. public static void createTable() {
  57.  
  58. /*
  59. *
  60. * Syntax: Spielername, UUID, Ende, Grund
  61. *
  62. */
  63.  
  64. if(isConnected()) {
  65.  
  66. Statement stmt = null;
  67.  
  68. try {
  69.  
  70. String sSQL = "CREATE TABLE IF NOT EXISTS BannedPlayers (Spielername VARCHAR(100), UUID VARCHAR(100), Ende VARCHAR(100), Grund VARCHAR(100))";
  71. stmt = con.createStatement();
  72. stmt.executeUpdate(sSQL);
  73.  
  74. } catch (SQLException e) {
  75.  
  76. ConsoleCommandSender sender = Bukkit.getConsoleSender();
  77. sender.sendMessage(MainClass.getInstance().pr + "§a Fehler in MySQL-Verbindung: "+e.getMessage()+", DB-Statement wurde zurückgesetzt.");
  78.  
  79. // e.printStackTrace();
  80.  
  81. } finally {
  82. try {
  83. stmt.close();
  84. } catch (Exception ignore) {
  85.  
  86. }
  87.  
  88. }
  89.  
  90. }
  91.  
  92. }
  93. public static void update(String qry) {
  94. if(isConnected()) {
  95.  
  96. try {
  97. con.createStatement().executeUpdate(qry);
  98. } catch (SQLException e) {
  99. e.printStackTrace();
  100. }
  101.  
  102. }
  103.  
  104. }
  105. public static ResultSet getResult(String qry) {
  106. if(isConnected()) {
  107.  
  108. try {
  109. con.createStatement().executeQuery(qry);
  110. } catch (SQLException e) {
  111. e.printStackTrace();
  112.  
  113. }
  114.  
  115. }
  116. return null;
  117.  
  118. }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement