Guest User

Untitled

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