Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. package mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. import org.bukkit.event.Listener;
  9.  
  10. public class Connector implements Listener {
  11. public static String host = "127.0.0.1";
  12. public static String port = "3306";
  13. public static String database = "MCN";
  14. public static String user = "";
  15. public static String password = "";
  16. public static Connection con;
  17.  
  18. public static void connect() {
  19. if (!isConnected()) {
  20. try {
  21. try {
  22. Class.forName("com.mysql.jdbc.Driver");
  23. } catch (ClassNotFoundException e) {
  24. // TODO Auto-generated catch block
  25. e.printStackTrace();
  26. }
  27. System.out.println("MySQL Connected.");
  28. con = DriverManager.getConnection(
  29. "jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true", user, password);
  30. createTable();
  31. createTableSP();
  32.  
  33.  
  34. } catch (SQLException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39.  
  40. public static void disconnect() {
  41. if (isConnected()) {
  42. try {
  43. con.close();
  44. System.out.println("[EgoPvP] Verbindung Getrennt!");
  45.  
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. }
  51.  
  52. public static boolean isConnected() {
  53. return con != null;
  54. }
  55.  
  56. public static void createTable() {
  57. if (isConnected()) {
  58. try {
  59. con.createStatement()
  60. .executeUpdate("CREATE TABLE IF NOT EXISTS Gems (UUID VARCHAR(100), Gems Int(10))");
  61. } catch (SQLException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65.  
  66. }
  67.  
  68. public static void createTableSP() {
  69. if (isConnected()) {
  70. try {
  71. con.createStatement()
  72. .executeUpdate("CREATE TABLE IF NOT EXISTS Spieler (Anzahl INT(10))");
  73. Methods_Anzahl.addlist();
  74. } catch (SQLException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78.  
  79. }
  80.  
  81. public static void update(String qry) {
  82. if (isConnected()) {
  83. try {
  84. con.createStatement().executeUpdate(qry);
  85. } catch (SQLException e) {
  86. e.printStackTrace();
  87. }
  88. }
  89. }
  90.  
  91. public static ResultSet getResult(String qry) {
  92. if (isConnected()) {
  93. try {
  94. return con.createStatement().executeQuery(qry);
  95. } catch (SQLException e) {
  96. e.printStackTrace();
  97. }
  98. }
  99. return null;
  100. }
  101.  
  102. public static Connection getConnection() {
  103. return con;
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement