Advertisement
Guest User

Untitled

a guest
Mar 1st, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. private final String host;
  2. private final int port;
  3.  
  4. private final String database;
  5.  
  6. private final String username;
  7. private final String password;
  8.  
  9. private Connection con;
  10.  
  11. private final ExecutorService service = Executors.newCachedThreadPool();
  12.  
  13. public MySQLManager(final String host, final int port, final String database, final String username, final String password) {
  14.  
  15. this.host = host;
  16. this.port = port;
  17.  
  18. this.database = database;
  19.  
  20. this.username = username;
  21. this.password = password;
  22.  
  23. }
  24.  
  25. public MySQLManager openConnection() {
  26. final String url = MessageFormat.format("jdbc:mysql://{0}:{1}/{2}?connectTimeout=0&socketTimeout=0&autoReconnect=true", this.host, String.valueOf(this.port), this.database);
  27. try {
  28. this.con = DriverManager.getConnection(url, this.username, this.password);
  29. } catch (SQLException e) {
  30. e.printStackTrace();
  31. }
  32. return this;
  33. }
  34.  
  35. public MySQLManager openConnection(final Runnable runable) throws SQLException {
  36. final String url = MessageFormat.format("jdbc:mysql://{0}:{1}/{2}?connectTimeout=0&socketTimeout=0&autoReconnect=true", this.host, String.valueOf(this.port), this.database);
  37. this.con = DriverManager.getConnection(url, this.username, this.password);
  38. runable.run();
  39. return this;
  40. }
  41.  
  42. public void update(final PreparedStatement statement) {
  43. this.service.execute(() -> {
  44. try {
  45. this.syncUpdate(statement);
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. }
  49. });
  50. }
  51.  
  52. private void syncUpdate(final PreparedStatement statement) throws SQLException {
  53. this.checkConnection();
  54.  
  55. statement.executeUpdate();
  56. statement.close();
  57. }
  58.  
  59. public void query(final PreparedStatement query, final Callback<ResultSet> callback, final Lock lock, final Condition condition) {
  60. this.service.execute(new QueryRunnable(query, callback, this, lock, condition));
  61. }
  62.  
  63. public void query(final PreparedStatement query, final Callback<ResultSet> callback) {
  64. this.service.execute(new QueryRunnable(query, callback, this, null, null));
  65. }
  66.  
  67. public final PreparedStatement prepareStatement(final String update) {
  68. try {
  69. return this.con.prepareStatement(update);
  70. } catch (SQLException e) {
  71. e.printStackTrace();
  72. }
  73. return null;
  74. }
  75.  
  76. public void checkConnection() throws SQLException {
  77. if(!this.isConnected())
  78. this.openConnection();
  79. }
  80.  
  81. public void closeConnection() {
  82. if(!this.isConnected())
  83. return;
  84.  
  85. try {
  86. this.con.close();
  87. } catch (SQLException e) {
  88. e.printStackTrace();
  89. }
  90. this.service.shutdown();
  91. this.con = null;
  92. }
  93.  
  94. private boolean isConnected() {
  95.  
  96. try {
  97. return this.con != null && !this.con.isClosed();
  98. } catch (SQLException e) {
  99. e.printStackTrace();
  100. }
  101.  
  102. return false;
  103. }
  104.  
  105. public final Connection getConnection() {
  106. return this.con;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement