Advertisement
DarkDev_

Untitled

Feb 26th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.82 KB | None | 0 0
  1.  private String hostname, database, username, passwort;
  2.     private int port;
  3.     private static Connection connection;
  4.     private ExecutorService executorService;
  5.  
  6.     public Database(String hostname, int port, String database, String username, String passwort) {
  7.         this.hostname = hostname;
  8.         this.port = port;
  9.         this.database = database;
  10.         this.username = username;
  11.         this.passwort = passwort;
  12.         this.executorService = Executors.newCachedThreadPool();
  13.  
  14.         openConnection();
  15.     }
  16.  
  17.     private void openConnection() {
  18.         try {
  19.             if (getConnection() != null && !getConnection().isClosed()) {
  20.                 return;
  21.             }
  22.             Class.forName("com.mysql.jdbc.Driver");
  23.             connection = DriverManager.getConnection("jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database + "?autoReconnect=true", this.username, this.passwort);
  24.             Bukkit.getConsoleSender().sendMessage(Data.returnPrefix() + "Die Verbindung zum Datenbank-Server wurde hergestellt.");
  25.         } catch (SQLException e) {
  26.             e.printStackTrace();
  27.         } catch (ClassNotFoundException e) {
  28.             e.printStackTrace();
  29.         }
  30.     }
  31.  
  32.     public void closeConnection() {
  33.  
  34.         if (isConnected()) {
  35.             try {
  36.                 connection.close();
  37.                 Bukkit.getConsoleSender().sendMessage(Data.returnPrefix() + "Die Verbindung zum Datenbank-Server wurde getrennt.");
  38.             } catch (SQLException e) {
  39.                 e.printStackTrace();
  40.             } finally {
  41.                 connection = null;
  42.             }
  43.         }
  44.     }
  45.  
  46.     public Connection getConnection() {
  47.         if (isConnected()) {
  48.             return connection;
  49.         }
  50.         return null;
  51.     }
  52.  
  53.     public boolean isConnected() {
  54.         try {
  55.             if (getConnection() == null || !getConnection().isValid(10) || getConnection().isClosed()) {
  56.                 return false;
  57.             }
  58.         } catch (SQLException e) {
  59.             e.printStackTrace();
  60.             return false;
  61.         }
  62.         return true;
  63.     }
  64.  
  65.     public void update(PreparedStatement preparedStatement) {
  66.         if (isConnected()) {
  67.             executorService.execute(() -> queryUpdate(preparedStatement));
  68.         }
  69.     }
  70.  
  71.     public void update(String s) {
  72.         if (isConnected()) {
  73.             executorService.execute(() -> queryUpdate(s));
  74.         }
  75.     }
  76.  
  77.     public ResultSet asyncQuery(PreparedStatement preparedStatement) {
  78.         if (isConnected()) {
  79.             Future<ResultSet> future = executorService.submit(() -> query(preparedStatement));
  80.             try {
  81.                 return future.get();
  82.             } catch (InterruptedException | ExecutionException exception) {
  83.                 exception.printStackTrace();
  84.             }
  85.         }
  86.         return null;
  87.     }
  88.  
  89.     public ResultSet asyncQuery(String s) {
  90.         if (isConnected()) {
  91.             Future<ResultSet> future = this.executorService.submit(() -> query(s));
  92.             try {
  93.                 return future.get();
  94.             } catch (InterruptedException | ExecutionException exception) {
  95.                 exception.printStackTrace();
  96.             }
  97.         }
  98.         return null;
  99.     }
  100.  
  101.     public PreparedStatement preparedStatement(String query) {
  102.         if (isConnected()) {
  103.             try {
  104.                 return getConnection().prepareStatement(query);
  105.             } catch (SQLException exception) {
  106.                 exception.printStackTrace();
  107.             }
  108.         }
  109.         return null;
  110.     }
  111.  
  112.     public void queryUpdate(String query) {
  113.         if (isConnected()) {
  114.             try (PreparedStatement statment = getConnection().prepareStatement(query)) {
  115.                 queryUpdate(statment);
  116.             } catch (Exception exception) {
  117.                 exception.printStackTrace();
  118.             }
  119.         }
  120.     }
  121.  
  122.     public ResultSet query(String query) {
  123.         if (isConnected()) {
  124.             try {
  125.                 return query(getConnection().prepareStatement(query));
  126.             } catch (SQLException exception) {
  127.                 exception.printStackTrace();
  128.             }
  129.         }
  130.         return null;
  131.     }
  132.  
  133.     public void queryUpdate(PreparedStatement preparedStatement) {
  134.         if (isConnected()) {
  135.             try {
  136.                 preparedStatement.executeUpdate();
  137.             } catch (SQLException exception) {
  138.                 exception.printStackTrace();
  139.             }
  140.         }
  141.     }
  142.  
  143.     public ResultSet query(PreparedStatement preparedStatement) {
  144.         if (isConnected()) {
  145.             try {
  146.                 return preparedStatement.executeQuery();
  147.             } catch (SQLException exception) {
  148.                 exception.printStackTrace();
  149.             }
  150.         }
  151.         return null;
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement