Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. package me.riderstorm.WarnMe;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10.  
  11. import org.bukkit.configuration.file.FileConfiguration;
  12. import org.bukkit.configuration.file.YamlConfiguration;
  13.  
  14.  
  15.  
  16.  
  17. public class MySQL
  18. {
  19. private String host;
  20. private int port;
  21. private String database;
  22. private String username;
  23. private String password;
  24. private Connection conn;
  25. public WarnSystem plugin;
  26.  
  27. public MySQL(WarnSystem plugin) {
  28. this.plugin = plugin;
  29. }
  30.  
  31. public MySQL() throws Exception {
  32. File file = new File("plugins/WarnSystem/", "database.yml");
  33. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
  34. String db = "database.";
  35. cfg.addDefault(db + "host", "localhost");
  36. cfg.addDefault(db + "port", 3306);
  37. cfg.addDefault(db + "user", "user");
  38. cfg.addDefault(db + "password", "password");
  39. cfg.addDefault(db + "database", "database");
  40. cfg.addDefault(db + "autoReconnect", true);
  41. cfg.options().copyDefaults(true);
  42. try
  43. {
  44. cfg.save(file);
  45. } catch (IOException e)
  46. {
  47. // TODO Auto-generated catch block
  48. e.printStackTrace();
  49. }
  50. this.host = cfg.getString(db + "host");
  51. this.port = cfg.getInt(db + "port");
  52. this.username = cfg.getString(db + "user");
  53. this.database = cfg.getString(db + "database");
  54. this.password = cfg.getString(db + "password");
  55.  
  56. this.conn = this.openConnection();
  57. if(this.conn == null) {
  58. this.plugin.getServer().getPluginManager().disablePlugin(this.plugin);
  59. }
  60.  
  61. }
  62. public Connection getConnection() {
  63. return this.conn;
  64. }
  65. public void queryUpdate(String query) {
  66. PreparedStatement st = null;
  67. try
  68. {
  69. st = this.conn.prepareStatement(query);
  70. st.executeUpdate();
  71. } catch (SQLException e)
  72. {
  73. System.out.println("Failed to send query update '" + query + "'.");
  74. } finally {
  75. this.closeRessources(null, st);
  76. }
  77.  
  78. }
  79. public void closeConnection() {
  80. try
  81. {
  82. this.conn.close();
  83. } catch (SQLException e)
  84. {
  85. e.printStackTrace();
  86. } finally {
  87. this.conn = null;
  88. }
  89.  
  90. }
  91. public boolean hasConnection() {
  92. try
  93. {
  94. return this.conn != null || this.conn.isValid(1);
  95. } catch (SQLException e)
  96.  
  97. {
  98. return false;
  99. }
  100. }
  101.  
  102. public void closeRessources(ResultSet rs, PreparedStatement st) {
  103. if(rs != null) {
  104. try {
  105. rs.close();
  106. } catch(SQLException e) {
  107.  
  108. }
  109. }
  110.  
  111. if(st != null) {
  112. try {
  113. st.close();
  114. } catch(SQLException e) {
  115.  
  116. }
  117. }
  118. }
  119.  
  120. public Connection openConnection() {
  121.  
  122.  
  123.  
  124.  
  125. try
  126. {
  127. Class.forName("com.mysql.jdbc.Driver");
  128. Connection con = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password);
  129. this.conn = con;
  130. return conn;
  131. } catch (SQLException e)
  132. {
  133. // TODO Auto-generated catch block
  134. e.printStackTrace();
  135. } catch (ClassNotFoundException e)
  136. {
  137. // TODO Auto-generated catch block
  138. e.printStackTrace();
  139. }
  140.  
  141.  
  142. return null;
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement