Advertisement
Guest User

MySQL

a guest
Oct 25th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.logging.Level;
  7.  
  8. import org.bukkit.plugin.Plugin;
  9.  
  10. public class MySQL {
  11.  
  12. private final String user;
  13. private final String database;
  14. private final String password;
  15. private final String port;
  16. private final String hostname;
  17.  
  18. private Connection connection;
  19.  
  20. private Plugin plugin;
  21.  
  22. public MySQL(Plugin plugin, String hostname, String port, String database, String username, String password) {
  23. this.plugin = plugin;
  24. this.hostname = hostname;
  25. this.port = port;
  26. this.database = database;
  27. this.user = username;
  28. this.password = password;
  29. this.connection = null;
  30. }
  31.  
  32.  
  33. public Connection openConnection() {
  34. try {
  35. Class.forName("com.mysql.jdbc.Driver");
  36. connection = DriverManager.getConnection("jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database, this.user, this.password);
  37. } catch (SQLException e) {
  38. plugin.getLogger().log(Level.SEVERE, "Verbindung unterbrochen, weil: " + e.getMessage());
  39. } catch (ClassNotFoundException e) {
  40. plugin.getLogger().log(Level.SEVERE, "JDBC Driver wurde nicht gefunden!");
  41. }
  42. return connection;
  43. }
  44.  
  45. public boolean checkConnection() {
  46. return connection != null;
  47. }
  48.  
  49. public Connection getConnection() {
  50. return connection;
  51. }
  52.  
  53. public void closeConnection() {
  54. if (connection != null) {
  55. try {
  56. connection.close();
  57. } catch (SQLException e) {
  58. plugin.getLogger().log(Level.SEVERE, "Fehler die MySQL verbindung wurde unterbrochen!");
  59. e.printStackTrace();
  60. }
  61. }
  62. }
  63.  
  64. public ResultSet querySQL(String query) {
  65. Connection c = null;
  66.  
  67. if (checkConnection()) {
  68. c = getConnection();
  69. } else {
  70. c = openConnection();
  71. }
  72.  
  73. Statement s = null;
  74.  
  75. try {
  76. s = c.createStatement();
  77. } catch (SQLException e1) {
  78. e1.printStackTrace();
  79. }
  80.  
  81. ResultSet ret = null;
  82.  
  83. try {
  84. ret = s.executeQuery(query);
  85. } catch (SQLException e) {
  86. e.printStackTrace();
  87. }
  88.  
  89. closeConnection();
  90.  
  91. return ret;
  92. }
  93.  
  94. public void updateSQL(String update) {
  95.  
  96. Connection c = null;
  97.  
  98. if (checkConnection()) {
  99. c = getConnection();
  100. } else {
  101. c = openConnection();
  102. }
  103.  
  104. Statement s = null;
  105.  
  106. try {
  107. s = c.createStatement();
  108. s.executeUpdate(update);
  109. } catch (SQLException e1) {
  110. e1.printStackTrace();
  111. }
  112.  
  113. closeConnection();
  114.  
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement