Advertisement
Guest User

Untitled

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