Guest User

Untitled

a guest
Dec 22nd, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. package me.luist.survivalplus.database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. import org.bukkit.plugin.Plugin;
  11.  
  12. public class MySQL {
  13. private final String user;
  14. private final String database;
  15. private final String password;
  16. private final String port;
  17. private final String hostname;
  18. private Connection connection;
  19. private Plugin plugin;
  20.  
  21. public MySQL(Plugin plugin) {
  22. this.hostname = plugin.getConfig().getString("database.host");
  23. this.port = plugin.getConfig().getString("database.port");
  24. this.database = plugin.getConfig().getString("database.name");
  25. this.user = plugin.getConfig().getString("database.username");
  26. this.password = plugin.getConfig().getString("database.password");
  27. this.connection = null;
  28. this.plugin = plugin;
  29.  
  30. openConnection();
  31.  
  32. plugin.getLogger().info("MySQL > Fixing database timezone");
  33. updateSQL("SET time_zone = '+00:00';");
  34. }
  35.  
  36. public Connection openConnection() {
  37. try {
  38. Class.forName("com.mysql.jdbc.Driver");
  39. this.connection = DriverManager.getConnection(
  40. "jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database, this.user, this.password);
  41. } catch (ClassNotFoundException e) {
  42. plugin.getLogger().severe(e.getMessage());
  43. return null;
  44. } catch (SQLException e) {
  45. plugin.getLogger().severe(e.getMessage());
  46. return null;
  47. }
  48. return this.connection;
  49. }
  50.  
  51. public boolean checkConnection() {
  52. return this.connection != null;
  53. }
  54.  
  55. public Connection getConnection() {
  56. return this.connection;
  57. }
  58.  
  59. public void closeConnection() {
  60. if (this.connection != null) {
  61. try {
  62. this.connection.close();
  63. } catch (SQLException e) {
  64. plugin.getLogger().warning("Error closing the MySQL Connection!");
  65. e.printStackTrace();
  66. }
  67. }
  68. }
  69.  
  70. public ResultSet querySQL(String query) {
  71. Connection c = null;
  72. Statement s = null;
  73. ResultSet ret = null;
  74. if (checkConnection()) {
  75. c = getConnection();
  76. } else {
  77. c = openConnection();
  78. }
  79. try {
  80. s = c.createStatement();
  81. } catch (SQLException e1) {
  82. plugin.getLogger().warning(e1.getMessage());
  83. }
  84. try {
  85. ret = s.executeQuery(query);
  86. } catch (SQLException e) {
  87. plugin.getLogger().warning(e.getMessage());
  88. }
  89. closeConnection();
  90. return ret;
  91. }
  92.  
  93. public void updateSQL(String update) {
  94. Connection c = null;
  95. Statement s = null;
  96. if (checkConnection()) {
  97. c = getConnection();
  98. } else {
  99. c = openConnection();
  100. }
  101. try {
  102. s = c.createStatement();
  103. s.executeUpdate(update);
  104. } catch (SQLException e1) {
  105. plugin.getLogger().warning(e1.getMessage());
  106. }
  107. closeConnection();
  108. }
  109.  
  110. public ResultSet select(String statement, String... args) throws SQLException {
  111. PreparedStatement mySQLstatement = this.connection.prepareStatement(statement);
  112. for (int i = 0; i < args.length; i++) {
  113. mySQLstatement.setString(i + 1, args[i]);
  114. }
  115. ResultSet result = mySQLstatement.executeQuery();
  116. return result;
  117. }
  118.  
  119. public void update(String statement, String... args) throws SQLException {
  120. PreparedStatement mySQLstatement = this.connection.prepareStatement(statement);
  121. for (int i = 0; i < args.length; i++) {
  122. mySQLstatement.setString(i + 1, args[i]);
  123. }
  124. mySQLstatement.executeUpdate();
  125. mySQLstatement.close();
  126. }
  127.  
  128. public void insert(String statement, String... args) throws SQLException {
  129. PreparedStatement mySQLstatement = this.connection.prepareStatement(statement);
  130. for (int i = 0; i < args.length; i++) {
  131. mySQLstatement.setString(i + 1, args[i]);
  132. }
  133. mySQLstatement.execute();
  134. mySQLstatement.close();
  135. }
  136. }
Add Comment
Please, Sign In to add comment