Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.bytropical.funnyperms.mysql;
- import java.sql.*;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class MySQLManager {
- private ExecutorService executorService = Executors.newFixedThreadPool(4);
- private String host;
- private String user;
- private String password;
- private String database;
- private int port;
- private Connection connection;
- public MySQLManager(String host, String user, String password, String database, int port) {
- this.host = host;
- this.user = user;
- this.password = password;
- this.database = database;
- this.port = port;
- connect();
- }
- private void connect() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.user, this.password);
- System.out.println("MySQL wird nun aufgebaut.");
- } catch (SQLException e) {
- this.connection = null;
- e.getMessage();
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- public void asyncUpdate(String qry) {
- executorService.execute(() -> {
- Statement statement = null;
- try {
- statement = connection.createStatement();
- statement.executeUpdate(qry);
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- statement.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- });
- }
- public ResultSet query(String qry) {
- if (isConnected()) {
- try {
- return connection.createStatement().executeQuery(qry);
- } catch (SQLException e) {
- System.out.println("[Error] Error while getting Result" + e);
- }
- }
- return null;
- }
- private boolean isConnected() {
- if (connection != null)
- return true;
- return false;
- }
- public Connection getConnection() {
- return connection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement