Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Decompiled with CFR 0_123.
- */
- package ru.yooxa.yapi.database;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class SQLConnection {
- private Connection connection;
- private ExecutorService executor;
- private String host;
- private String database;
- private String user;
- private String password;
- public static List<SQLConnection> connections = new ArrayList<SQLConnection>();
- public SQLConnection(String host, String user, String password) {
- this.host = host;
- this.user = user;
- this.password = password;
- this.executor = Executors.newSingleThreadExecutor();
- connections.add(this);
- }
- public SQLConnection(String host, String user, String password, String database) {
- this(host, user, password);
- this.database = database;
- }
- public Connection getConnection() throws SQLException {
- if (this.connection == null || this.connection.isClosed()) {
- this.connect();
- }
- return this.connection;
- }
- public void close() throws SQLException {
- connections.remove(this);
- this.connection.close();
- this.executor.shutdownNow();
- }
- public void connect() throws SQLException {
- this.connection = DriverManager.getConnection("JDBC:mysql://" + this.host + ":3306/" + this.database, this.user, this.password);
- }
- public void execute(String sql) {
- this.execute(sql, true);
- }
- public void execute(String sql, boolean async) {
- Runnable runnable = () -> {
- try {
- Statement statement = this.getConnection().createStatement();
- Throwable throwable = null;
- try {
- try {
- statement.executeUpdate(sql);
- }
- catch (Throwable throwable2) {
- throwable = throwable2;
- throw throwable2;
- }
- }
- finally {
- if (statement != null) {
- if (throwable != null) {
- try {
- statement.close();
- }
- catch (Throwable throwable3) {
- throwable.addSuppressed(throwable3);
- }
- } else {
- statement.close();
- }
- }
- }
- }
- catch (SQLException ex) {
- ex.printStackTrace();
- }
- };
- if (async) {
- this.executor.execute(runnable);
- } else {
- runnable.run();
- }
- }
- }
Add Comment
Please, Sign In to add comment