Advertisement
Guest User

Untitled

a guest
Mar 4th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1.  
  2. private ExecutorService executorService =Executors.newFixedThreadPool(2);
  3. private String host = "***";
  4. private String user "root";
  5. private String password = "****";
  6. private String database "databaseName";
  7. private int port = 3306;
  8.  
  9. private Connection connection;
  10.  
  11. private void connect() {
  12. try {
  13. this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true", this.user, this.password);
  14. System.out.println("MySQL wird nun aufgebaut.");
  15. } catch (SQLException e) {
  16. this.connection = null;
  17. e.printStackTrace();
  18. }
  19. }
  20.  
  21. public void asyncUpdate(String qry) {
  22. executorService.execute(() -> {
  23. Statement statement = null;
  24. try {
  25. statement = connection.createStatement();
  26. statement.executeUpdate(qry);
  27. } catch (SQLException e) {
  28. e.printStackTrace();
  29. } finally {
  30. try {
  31. statement.close();
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. });
  37. }
  38.  
  39. public ResultSet query(String qry) {
  40. if (isConnected()) {
  41. try {
  42. return connection.createStatement().executeQuery(qry);
  43. } catch (SQLException e) {
  44. System.out.println("[Error] Error while getting Result" + e);
  45. }
  46. }
  47. return null;
  48. }
  49.  
  50. private boolean isConnected() {
  51. if (connection != null)
  52. return true;
  53. return false;
  54. }
  55. public Connection getConnection() {
  56. return connection;
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement