Guest User

Untitled

a guest
Oct 11th, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. package de.ks98.raidsystem.db;
  2.  
  3. import de.ks98.raidsystem.main.Main;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9.  
  10. /**
  11. * Created by ks98 on 10.10.16.
  12. */
  13. public class SQLManager {
  14.  
  15. private String host;
  16. private String port;
  17. private String user;
  18. private String pass;
  19. private String database;
  20. private Connection conn;
  21. private final StayAliveTask aliveTask;
  22. private final AsyncHandler asyncHandler;
  23. private final Updater updater;
  24.  
  25. public SQLManager(String host, String port, String user, String pass, String database) {
  26. this.host = host;
  27. this.port = port;
  28. this.user = user;
  29. this.pass = pass;
  30. this.database = database;
  31.  
  32. this.aliveTask = new StayAliveTask(this);
  33. this.asyncHandler = new AsyncHandler();
  34. this.updater = new Updater();
  35. }
  36.  
  37. public boolean openConnection() {
  38. try {
  39. if ((this.conn != null) && (!this.conn.isClosed())) {
  40. return false;
  41. }
  42. Class.forName("com.mysql.jdbc.Driver");
  43. this.conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.user, this.pass);
  44. this.aliveTask.setActive(true);
  45. this.aliveTask.start();
  46. this.updater.setActive(true);
  47. this.updater.start();
  48. return true;
  49. } catch (Exception e) {
  50. Main.getInstance().getLogger().warning("Verbindung konnte nicht geöffnet werden: " + e.getMessage());
  51. }
  52. return false;
  53. }
  54.  
  55. public Connection getConnection() {
  56. try {
  57. if ((this.conn == null) || (this.conn.isClosed())) {
  58. openConnection();
  59. }
  60. } catch (Exception localException) {}
  61. return this.conn;
  62. }
  63.  
  64. public void closeConnection() {
  65. try {
  66. if ((this.conn != null) && (!this.conn.isClosed())) {
  67. this.conn.close();
  68. this.conn = null;
  69. this.aliveTask.setActive(false);
  70. }
  71. } catch (Exception e) {
  72. Main.getInstance().getLogger().warning("Verbindung konnte nicht geschlossen werden: " + e.getMessage());
  73. }
  74. }
  75.  
  76. public void close(PreparedStatement st, ResultSet rs) {
  77. try {
  78. if (st != null) {
  79. st.close();
  80. }
  81. if (rs != null) {
  82. rs.close();
  83. }
  84. } catch (Exception localException) {}
  85. }
  86.  
  87. public void executeUpdate(String statement) {
  88. try {
  89. PreparedStatement st = this.conn.prepareStatement(statement);
  90. st.executeUpdate();
  91. close(st, null);
  92. } catch (Exception e) {
  93. Main.getInstance().getLogger().warning("executeUpdate konnte nicht ausgeführt werden: " + e.getMessage());
  94. e.printStackTrace();
  95. }
  96. }
  97.  
  98. public void executeUpdate(PreparedStatement statement) {
  99. try {
  100. statement.executeUpdate();
  101. close(statement, null);
  102. } catch (Exception e) {
  103. Main.getInstance().getLogger().warning("executeUpdate konnte nicht ausgeführt werden: " + e.getMessage());
  104. e.printStackTrace();
  105. }
  106. }
  107.  
  108. public ResultSet executeQuery(String statement) {
  109. try {
  110. PreparedStatement st = this.conn.prepareStatement(statement);
  111. return st.executeQuery();
  112. } catch (Exception e) {
  113. Main.getInstance().getLogger().warning("executeQuery konnte nicht ausgeführt werden: " + e.getMessage());
  114. }
  115. return null;
  116. }
  117.  
  118. public ResultSet executeQuery(PreparedStatement statement) {
  119. try {
  120. return statement.executeQuery();
  121. } catch (Exception e) {
  122. Main.getInstance().getLogger().warning("executeQuery konnte nicht ausgeführt werden: " + e.getMessage());
  123. }
  124. return null;
  125. }
  126.  
  127. public AsyncHandler getAsyncHandler() {
  128. return this.asyncHandler;
  129. }
  130.  
  131. public StayAliveTask getAliveTask() {
  132. return this.aliveTask;
  133. }
  134.  
  135. public Updater getUpdater() {
  136. return this.updater;
  137. }
  138. }
Add Comment
Please, Sign In to add comment