Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.virtualbyte.byteutils.storage;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
- import javax.sql.rowset.CachedRowSet;
- import org.bukkit.Bukkit;
- import org.bukkit.plugin.java.JavaPlugin;
- import com.sun.rowset.CachedRowSetImpl;
- import com.zaxxer.hikari.HikariConfig;
- import com.zaxxer.hikari.HikariDataSource;
- /*
- * ByteUtils - Developed by VirtualByte (Lewes D. B.)
- */
- public abstract class Database
- {
- private String jdbcURL;
- private HikariDataSource dataSource;
- private JavaPlugin plugin;
- public Database(String className, String jdbcURL, String username, String password, JavaPlugin plugin)
- {
- this.jdbcURL = jdbcURL;
- this.plugin = plugin;
- try
- {
- Class.forName(className);
- } catch (ClassNotFoundException e)
- {
- e.printStackTrace();
- return;
- }
- HikariConfig config = new HikariConfig();
- config.setJdbcUrl(jdbcURL);
- config.setUsername(username);
- config.setPassword(password);
- config.setLeakDetectionThreshold(10000);
- config.setMaxLifetime(25000);
- config.setIdleTimeout(20000);
- config.setMaximumPoolSize(10);
- config.setConnectionTimeout(10000);
- try
- {
- dataSource = new HikariDataSource(config);
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public void connect()
- {
- isConnected();
- }
- public void disconnect()
- {
- dataSource.shutdown();
- }
- public CachedRowSet query(final PreparedStatement preparedStatement)
- {
- CachedRowSet rowSet = null;
- if (isConnected())
- {
- try
- {
- ExecutorService exe = Executors.newCachedThreadPool();
- Future<CachedRowSet> future = exe.submit(new Callable<CachedRowSet>() {
- public CachedRowSet call()
- {
- try
- {
- ResultSet resultSet = preparedStatement.executeQuery();
- CachedRowSet cachedRowSet = new CachedRowSetImpl();
- cachedRowSet.populate(resultSet);
- resultSet.close();
- if (cachedRowSet.next())
- {
- return cachedRowSet;
- }
- } catch (SQLException e)
- {
- e.printStackTrace();
- } finally
- {
- try
- {
- preparedStatement.close();
- preparedStatement.getConnection().close();
- } catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- return null;
- }
- });
- if (future.get() != null)
- {
- rowSet = future.get();
- }
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- } catch (ExecutionException e)
- {
- e.printStackTrace();
- }
- }
- return rowSet;
- }
- public void execute(final PreparedStatement preparedStatement)
- {
- if (isConnected())
- {
- Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
- @Override
- public void run()
- {
- try
- {
- preparedStatement.execute();
- } catch (SQLException e)
- {
- e.printStackTrace();
- } finally
- {
- try
- {
- preparedStatement.close();
- preparedStatement.getConnection().close();
- } catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
- });
- }
- }
- public PreparedStatement prepareStatement(String query, String... vars)
- {
- try
- {
- PreparedStatement preparedStatement = getConnection().prepareStatement(query);
- int x = 0;
- if (query.contains("?") && vars.length != 0)
- {
- for (String var : vars)
- {
- x++;
- preparedStatement.setString(x, var);
- }
- }
- return preparedStatement;
- } catch (SQLException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- public Connection getConnection()
- {
- try
- {
- return dataSource.getConnection();
- } catch (SQLException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- public boolean isConnected()
- {
- try
- {
- Connection connection = dataSource.getConnection();
- connection.close();
- } catch (SQLException e)
- {
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement