Guest User

Untitled

a guest
Jan 6th, 2018
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. /*
  2. * Decompiled with CFR 0_123.
  3. */
  4. package ru.yooxa.yapi.database;
  5.  
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.concurrent.ExecutorService;
  13. import java.util.concurrent.Executors;
  14.  
  15. public class SQLConnection {
  16. private Connection connection;
  17. private ExecutorService executor;
  18. private String host;
  19. private String database;
  20. private String user;
  21. private String password;
  22. public static List<SQLConnection> connections = new ArrayList<SQLConnection>();
  23.  
  24. public SQLConnection(String host, String user, String password) {
  25. this.host = host;
  26. this.user = user;
  27. this.password = password;
  28. this.executor = Executors.newSingleThreadExecutor();
  29. connections.add(this);
  30. }
  31.  
  32. public SQLConnection(String host, String user, String password, String database) {
  33. this(host, user, password);
  34. this.database = database;
  35. }
  36.  
  37. public Connection getConnection() throws SQLException {
  38. if (this.connection == null || this.connection.isClosed()) {
  39. this.connect();
  40. }
  41. return this.connection;
  42. }
  43.  
  44. public void close() throws SQLException {
  45. connections.remove(this);
  46. this.connection.close();
  47. this.executor.shutdownNow();
  48. }
  49.  
  50. public void connect() throws SQLException {
  51. this.connection = DriverManager.getConnection("JDBC:mysql://" + this.host + ":3306/" + this.database, this.user, this.password);
  52. }
  53.  
  54. public void execute(String sql) {
  55. this.execute(sql, true);
  56. }
  57.  
  58. public void execute(String sql, boolean async) {
  59. Runnable runnable = () -> {
  60. try {
  61. Statement statement = this.getConnection().createStatement();
  62. Throwable throwable = null;
  63. try {
  64. try {
  65. statement.executeUpdate(sql);
  66. }
  67. catch (Throwable throwable2) {
  68. throwable = throwable2;
  69. throw throwable2;
  70. }
  71. }
  72. finally {
  73. if (statement != null) {
  74. if (throwable != null) {
  75. try {
  76. statement.close();
  77. }
  78. catch (Throwable throwable3) {
  79. throwable.addSuppressed(throwable3);
  80. }
  81. } else {
  82. statement.close();
  83. }
  84. }
  85. }
  86. }
  87. catch (SQLException ex) {
  88. ex.printStackTrace();
  89. }
  90. };
  91. if (async) {
  92. this.executor.execute(runnable);
  93. } else {
  94. runnable.run();
  95. }
  96. }
  97. }
Add Comment
Please, Sign In to add comment