Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.biosphere.bix.mysql;
- import org.apache.commons.dbcp.BasicDataSource;
- import java.sql.*;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- /**
- * @author Biosphere
- * @date 18.02.18
- */
- public class MySQL {
- private final ExecutorService executor;
- private final BasicDataSource dataSource = new BasicDataSource();
- public MySQL(String host, String user, String password, String database, int port) {
- dataSource.setDriverClassName("com.mysql.jdbc.Driver");
- dataSource.setUrl("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true");
- dataSource.setUsername(user);
- dataSource.setPassword(password);
- this.executor = Executors.newCachedThreadPool();
- }
- public void update(final String query) {
- try(final Connection connection = getConnection()) {
- PreparedStatement preparedStatement = connection.prepareStatement(query);
- preparedStatement.execute();
- } catch (SQLException exception){
- exception.printStackTrace();
- }
- }
- public void updateAsync(final String query) {
- executor.execute(() -> update(query));
- }
- public ResultSet query(final String query) {
- try {
- PreparedStatement preparedStatement = getConnection().prepareStatement(query);
- return preparedStatement.executeQuery();
- } catch (SQLException exception){
- exception.printStackTrace();
- }
- return null;
- }
- public Connection getConnection() throws SQLException {
- return dataSource.getConnection();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement