Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. public static String username;
  2. public static String password;
  3. public static String database;
  4. public static String host;
  5. public static String port;
  6. public static Connection con;
  7.  
  8. public MySQL(String user, String pass, String host2, String dB) {}
  9.  
  10. public static void connect()
  11. {
  12. if (!isConnected()) {
  13. try
  14. {
  15. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?user=" + username + "&password=" + password + "&autoReconnect=true");
  16. Bukkit.getConsoleSender().sendMessage(messages.getPrefix() +"§aMySQL MySQL connected");
  17. }
  18. catch (SQLException e)
  19. {
  20. e.printStackTrace();
  21. }
  22. }
  23. }
  24.  
  25. public static void close()
  26. {
  27. if (isConnected()) {
  28. try
  29. {
  30. con.close();
  31. Bukkit.getConsoleSender().sendMessage(messages.getPrefix() + "§aMySQL MySQL disconnected");
  32. }
  33. catch (SQLException e)
  34. {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39.  
  40. public static boolean isConnected()
  41. {
  42. return con != null;
  43. }
  44.  
  45. public static void createTable()
  46. {
  47. if (isConnected()) {
  48. try
  49. {
  50. con.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS KnockIt (UUID VARCHAR(100), NAME VARCHAR(100), KILLS int, DEATHS int)");
  51. Bukkit.getConsoleSender().sendMessage(messages.getPrefix() + "§4MySQL MySQL Table created");
  52. }
  53. catch (SQLException e)
  54. {
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59.  
  60. public static void update(String qry)
  61. {
  62. if (isConnected()) {
  63. try
  64. {
  65. con.createStatement().executeUpdate(qry);
  66. }
  67. catch (SQLException e)
  68. {
  69. e.printStackTrace();
  70. }
  71. }
  72. }
  73.  
  74. public static ResultSet getResult(String qry)
  75. {
  76. ResultSet rs = null;
  77. try
  78. {
  79. Statement st = con.createStatement();
  80. rs = st.executeQuery(qry);
  81. }
  82. catch (SQLException e)
  83. {
  84. connect();
  85. System.err.println(e);
  86. }
  87. return rs;
  88. }
  89.  
  90. public static File getMySQLFile()
  91. {
  92. return new File("plugins/KnockIT", "config.yml");
  93. }
  94.  
  95. public static FileConfiguration getMySQLFileConfiguration()
  96. {
  97. return YamlConfiguration.loadConfiguration(getMySQLFile());
  98. }
  99.  
  100. public static void setStandardMySQL()
  101. {
  102. FileConfiguration cfg = getMySQLFileConfiguration();
  103.  
  104. cfg.options().copyDefaults(true);
  105. cfg.addDefault("username", "root");
  106. cfg.addDefault("password", "password");
  107. cfg.addDefault("database", "localhost");
  108. cfg.addDefault("host", "localhost");
  109. cfg.addDefault("port", "3306");
  110. try
  111. {
  112. cfg.save(getMySQLFile());
  113. }
  114. catch (IOException e)
  115. {
  116. e.printStackTrace();
  117. }
  118. }
  119.  
  120. public static void readMySQL()
  121. {
  122. FileConfiguration cfg = getMySQLFileConfiguration();
  123. username = cfg.getString("username");
  124. password = cfg.getString("password");
  125. database = cfg.getString("database");
  126. host = cfg.getString("host");
  127. port = cfg.getString("port");
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement