Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. package de.biosphere.bix.mysql;
  2.  
  3. import org.apache.commons.dbcp.BasicDataSource;
  4.  
  5. import java.sql.*;
  6. import java.util.concurrent.ExecutorService;
  7. import java.util.concurrent.Executors;
  8.  
  9. /**
  10. * @author Biosphere
  11. * @date 18.02.18
  12. */
  13. public class MySQL {
  14.  
  15. private final ExecutorService executor;
  16. private final BasicDataSource dataSource = new BasicDataSource();
  17.  
  18. public MySQL(String host, String user, String password, String database, int port) {
  19. dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  20. dataSource.setUrl("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true");
  21. dataSource.setUsername(user);
  22. dataSource.setPassword(password);
  23.  
  24. this.executor = Executors.newCachedThreadPool();
  25. }
  26.  
  27. public void update(final String query) {
  28. try(final Connection connection = getConnection()) {
  29. PreparedStatement preparedStatement = connection.prepareStatement(query);
  30. preparedStatement.execute();
  31. } catch (SQLException exception){
  32. exception.printStackTrace();
  33. }
  34. }
  35.  
  36. public void updateAsync(final String query) {
  37. executor.execute(() -> update(query));
  38. }
  39.  
  40. public ResultSet query(final String query) {
  41. try {
  42. PreparedStatement preparedStatement = getConnection().prepareStatement(query);
  43. return preparedStatement.executeQuery();
  44. } catch (SQLException exception){
  45. exception.printStackTrace();
  46. }
  47. return null;
  48. }
  49.  
  50. public Connection getConnection() throws SQLException {
  51. return dataSource.getConnection();
  52. }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement