Advertisement
Guest User

Untitled

a guest
Aug 5th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. package eu.craftgames.craftcore.mysql;
  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.util.logging.Level;
  9.  
  10. import org.bukkit.Bukkit;
  11.  
  12. import eu.craftgames.craftcore.CraftCore;
  13.  
  14. /**
  15. * @version 1.0
  16. * @author MrKrisKrisu
  17. */
  18. public class MySQL {
  19.  
  20. private Connection connect;
  21.  
  22. private String host = "";
  23. private int port = 3306;
  24. private String database;
  25. private String user;
  26. private String password;
  27.  
  28. public MySQL(String database, String user, String password) {
  29. this.database = database;
  30. this.user = user;
  31. this.password = password;
  32. CraftCore.getInstance().getLogger().log(Level.INFO, "[MySQL-Manager] Neue MySQL Instanz fuer " + database + "/" + user + " erstellt!");
  33. }
  34.  
  35. public MySQL(String database) {
  36. this.database = database;
  37. CraftCore.getInstance().getLogger().log(Level.INFO, "[MySQL-Manager] Neue MySQL Instanz fuer " + database + "/" + user + " erstellt!");
  38. }
  39.  
  40. /**
  41. * Startet die Verbindung.
  42. *
  43. * @return
  44. * @throws SQLException
  45. * @throws ClassNotFoundException
  46. */
  47. public Connection openConnection() throws SQLException, ClassNotFoundException {
  48. try {
  49. CraftCore.getInstance().getLogger().log(Level.INFO, "[MySQL-Manager] Verbindung zur Datenbank '" + database + "' wird aufgebaut!");
  50. Class.forName("com.mysql.jdbc.Driver");
  51. connect = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?user=" + user + "&password=" + password + "&autoReconnect=true");
  52. CraftCore.getInstance().getLogger().log(Level.INFO, "[MySQL-Manager] Verbindung zur Datenbank '" + database + "' hergestellt!");
  53. } catch (SQLException e) {
  54. CraftCore.getInstance().getLogger().log(Level.SEVERE, "[MySQL-Manager] Kann die Verbindung zur Datenbank '" + database + "' nicht herstellen!");
  55. CraftCore.getInstance().getLogger().log(Level.SEVERE, "[MySQL-Manager] " + e.getMessage());
  56. } catch (ClassNotFoundException e) {
  57. CraftCore.getInstance().getLogger().log(Level.SEVERE, "[MySQL-Manager] Der MySQL Treiber wurde nicht gefunden!");
  58. }
  59. return connect;
  60. }
  61.  
  62. /**
  63. * @return Die Verbindung
  64. */
  65. public Connection getConnection() {
  66. return connect;
  67. }
  68.  
  69. /**
  70. * @return Ob eine Verbindung aufgebaut ist
  71. */
  72. public boolean hasConnection() {
  73. try {
  74. return connect != null || connect.isValid(1);
  75. } catch (SQLException e) {
  76. return false;
  77. }
  78. }
  79.  
  80. /**
  81. * F�hrt einen MySQL Query aus (welcher Daten �ndert!!!)
  82. *
  83. * @param query
  84. */
  85. @SuppressWarnings("deprecation")
  86. public void queryUpdate(final String query) {
  87. Bukkit.getScheduler().scheduleAsyncDelayedTask(CraftCore.getInstance(), new Runnable() {
  88.  
  89. @Override
  90. public void run() {
  91. Connection con = connect;
  92. PreparedStatement st = null;
  93. try {
  94. st = con.prepareStatement(query);
  95. st.executeUpdate();
  96. } catch (SQLException e) {
  97. e.printStackTrace();
  98. } finally {
  99. closeRessources(null, st);
  100. }
  101. }
  102. });
  103. }
  104.  
  105. /**
  106. * F�hrt einen MySQL Query aus und gibt die Ausgabe zur�ck. KEINE DATEN �NDERUNG M�GLICH!
  107. *
  108. * @param query
  109. * @return
  110. */
  111. public ResultSet getQuery(String query) {
  112. try {
  113. PreparedStatement stmt = connect.prepareStatement(query);
  114. return stmt.executeQuery(query);
  115. } catch (Exception ex) {
  116. ex.printStackTrace();
  117. }
  118. return null;
  119. }
  120.  
  121. public void closeRessources(ResultSet rs, PreparedStatement st) {
  122. if (rs != null) {
  123. try {
  124. rs.close();
  125. } catch (SQLException e) {
  126. e.printStackTrace();
  127. }
  128. }
  129. if (st != null) {
  130. try {
  131. st.close();
  132. } catch (SQLException e) {
  133. e.printStackTrace();
  134. }
  135. }
  136. }
  137.  
  138. public void closeConnection() {
  139. try {
  140. connect.close();
  141. } catch (SQLException e) {
  142. e.printStackTrace();
  143. } finally {
  144. connect = null;
  145. }
  146. }
  147.  
  148. public String getDatabase() {
  149. return database;
  150. }
  151.  
  152. public String getUser() {
  153. return user;
  154. }
  155.  
  156. public void setDatabase(String database) {
  157. this.database = database;
  158. }
  159.  
  160. public void setUser(String user) {
  161. this.user = user;
  162. }
  163.  
  164. public void setPassword(String password) {
  165. this.password = password;
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement