Advertisement
Guest User

Untitled

a guest
Apr 12th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. package me.bytropical.funnyperms.mysql;
  2.  
  3. import java.sql.*;
  4. import java.util.concurrent.ExecutorService;
  5. import java.util.concurrent.Executors;
  6.  
  7. public class MySQLManager {
  8.  
  9. private ExecutorService executorService = Executors.newFixedThreadPool(4);
  10. private String host;
  11. private String user;
  12. private String password;
  13. private String database;
  14. private int port;
  15.  
  16. private Connection connection;
  17.  
  18. public MySQLManager(String host, String user, String password, String database, int port) {
  19. this.host = host;
  20. this.user = user;
  21. this.password = password;
  22. this.database = database;
  23. this.port = port;
  24. connect();
  25. }
  26.  
  27. private void connect() {
  28. try {
  29.  
  30. Class.forName("com.mysql.jdbc.Driver");
  31. this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.user, this.password);
  32. System.out.println("MySQL wird nun aufgebaut.");
  33. } catch (SQLException e) {
  34. this.connection = null;
  35. e.getMessage();
  36. e.printStackTrace();
  37. } catch (ClassNotFoundException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41.  
  42. public void asyncUpdate(String qry) {
  43. executorService.execute(() -> {
  44. Statement statement = null;
  45. try {
  46. statement = connection.createStatement();
  47. statement.executeUpdate(qry);
  48. } catch (SQLException e) {
  49. e.printStackTrace();
  50. } finally {
  51. try {
  52. statement.close();
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. });
  58. }
  59.  
  60. public ResultSet query(String qry) {
  61. if (isConnected()) {
  62. try {
  63. return connection.createStatement().executeQuery(qry);
  64. } catch (SQLException e) {
  65. System.out.println("[Error] Error while getting Result" + e);
  66. }
  67. }
  68. return null;
  69. }
  70.  
  71. private boolean isConnected() {
  72. if (connection != null)
  73. return true;
  74.  
  75. return false;
  76. }
  77.  
  78. public Connection getConnection() {
  79. return connection;
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement