Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. package host.batt.daemon.sql;
  2.  
  3. import java.util.*;
  4. import java.sql.*;
  5.  
  6. public class MysqlManager
  7. {
  8. private final MysqlConfig config;
  9. private Connection conn;
  10. protected Set<MysqlTask> activeTasks;
  11.  
  12. private synchronized Connection createConnection() throws SQLException {
  13. try {
  14. Class.forName("com.mysql.jdbc.Driver");
  15. }
  16. catch (Exception ex) {}
  17. return DriverManager.getConnection("jdbc:mysql://" + this.config.getHost() + "/" + this.config.getDatabase() + "?useUnicode=true&characterEncoding=UTF-8", this.config.getUser(), this.config.getPassword());
  18. }
  19.  
  20. private synchronized Connection getConnection() throws SQLException {
  21. if (this.conn != null) {
  22. try {
  23. if (this.conn.isValid(28790)) {
  24. return this.conn;
  25. }
  26. }
  27. catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. try {
  31. this.conn.close();
  32. }
  33. catch (Exception ex) {
  34. ex.printStackTrace();
  35. }
  36. }
  37. return this.conn = this.createConnection();
  38. }
  39.  
  40. public void cancelTasks() {
  41. for (final MysqlTask t : new HashSet<MysqlTask>(this.activeTasks)) {
  42. try {
  43. t.cancelTask();
  44. }
  45. catch (Exception e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50.  
  51. public PreparedStatement loadValues(final PreparedStatement stm, final Object... args) throws SQLException {
  52. for (int x = 0; x < args.length; ++x) {
  53. stm.setObject(x + 1, args[x]);
  54. }
  55. return stm;
  56. }
  57.  
  58. public synchronized ResultSet executeQuery(final String query) throws SQLException {
  59. return this.getConnection().createStatement().executeQuery(query);
  60. }
  61.  
  62. public synchronized ResultSet executeQuery(final String query, final Object... args) throws SQLException {
  63. return this.loadValues(this.getConnection().prepareStatement(query), args).executeQuery();
  64. }
  65.  
  66. public synchronized int executeUpdate(final String query) throws SQLException {
  67. final Statement stm = this.getConnection().createStatement();
  68. final int n = stm.executeUpdate(query);
  69. stm.close();
  70. return n;
  71. }
  72.  
  73. public synchronized int executeUpdate(final String query, final Object... args) throws SQLException {
  74. final PreparedStatement stm = this.loadValues(this.getConnection().prepareStatement(query), args);
  75. final int n = stm.executeUpdate();
  76. stm.close();
  77. return n;
  78. }
  79.  
  80. public synchronized boolean execute(final String query) throws SQLException {
  81. final Statement stm = this.getConnection().createStatement();
  82. final boolean result = stm.execute(query);
  83. stm.close();
  84. return result;
  85. }
  86.  
  87. public synchronized boolean execute(final String query, final Object... args) throws SQLException {
  88. final PreparedStatement stm = this.loadValues(this.getConnection().prepareStatement(query), args);
  89. final boolean result = stm.execute();
  90. stm.close();
  91. return result;
  92. }
  93.  
  94. public void executeQueryAsync(final String query, final MysqlCallback<ResultSet> callback) {
  95. new MysqlTask(this) {
  96. @Override
  97. public void run() {
  98. try {
  99. final ResultSet result = MysqlManager.this.executeQuery(query);
  100. final Statement stm = result.getStatement();
  101. callback.onResult(result);
  102. result.close();
  103. stm.close();
  104. }
  105. catch (Exception e) {
  106. callback.onResult(null, e);
  107. }
  108. }
  109. }.runAsync();
  110. }
  111.  
  112. public void executeQueryAsync(final String query, final MysqlCallback<ResultSet> callback, final Object... args) {
  113. new MysqlTask(this) {
  114. @Override
  115. public void run() {
  116. try {
  117. final ResultSet result = MysqlManager.this.executeQuery(query, args);
  118. final Statement stm = result.getStatement();
  119. callback.onResult(result);
  120. result.close();
  121. stm.close();
  122. }
  123. catch (Exception e) {
  124. callback.onResult(null, e);
  125. }
  126. }
  127. }.runAsync();
  128. }
  129.  
  130. public void executeUpdateAsync(final String query, final MysqlCallback<Integer> callback) {
  131. new MysqlTask(this) {
  132. @Override
  133. public void run() {
  134. try {
  135. callback.onResult(MysqlManager.this.executeUpdate(query));
  136. }
  137. catch (Exception e) {
  138. callback.onResult(null, e);
  139. }
  140. }
  141. }.runAsync();
  142. }
  143.  
  144. public void executeUpdateAsync(final String query, final MysqlCallback<Integer> callback, final Object... args) {
  145. new MysqlTask(this) {
  146. @Override
  147. public void run() {
  148. try {
  149. callback.onResult(MysqlManager.this.executeUpdate(query, args));
  150. }
  151. catch (Exception e) {
  152. callback.onResult(null, e);
  153. }
  154. }
  155. }.runAsync();
  156. }
  157.  
  158. public void executeAsync(final String query, final MysqlCallback<Boolean> callback) {
  159. new MysqlTask(this) {
  160. @Override
  161. public void run() {
  162. try {
  163. callback.onResult(MysqlManager.this.execute(query));
  164. }
  165. catch (Exception e) {
  166. callback.onResult(null, e);
  167. }
  168. }
  169. }.runAsync();
  170. }
  171.  
  172. public void executeAsync(final String query, final MysqlCallback<Boolean> callback, final Object... args) {
  173. new MysqlTask(this) {
  174. @Override
  175. public void run() {
  176. try {
  177. callback.onResult(MysqlManager.this.execute(query, args));
  178. }
  179. catch (Exception e) {
  180. callback.onResult(null, e);
  181. }
  182. }
  183. }.runAsync();
  184. }
  185.  
  186. public MysqlManager(final MysqlConfig config) {
  187. this.activeTasks = new HashSet<MysqlTask>();
  188. this.config = config;
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement