Advertisement
Guest User

Untitled

a guest
Nov 30th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. package de.cadedev.testplugin.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class MySQL {
  10.  
  11. private String HOST = "";
  12. private String USER = "";
  13. private String PASSWORD = "";
  14. private String DATABASE = "";
  15. private Connection connection;
  16.  
  17. public MySQL(String Host, String Datenbank, String Benutzername, String Passwort) {
  18. this.HOST = Host;
  19. this.USER = Benutzername;
  20. this.PASSWORD = Passwort;
  21. this.DATABASE = Datenbank;
  22. setConnect();
  23. }
  24.  
  25. public void setConnect() {
  26. try {
  27. this.connection = DriverManager.getConnection("jdbc:mysql://" + this.HOST + ":3306/" + this.DATABASE + "?autoReconnect=true", this.USER,this.PASSWORD);
  28. System.out.println("[MySQL] Verbindung hergestellt");
  29. } catch (SQLException e) {
  30. System.out.println("[MySQL] Fehler: " + e.getMessage());
  31. }
  32. }
  33.  
  34. public void setUpdate(String qry) {
  35. try {
  36. Statement st = this.connection.createStatement();
  37. st.executeUpdate(qry);
  38. st.close();
  39. } catch (SQLException e) {
  40. setConnect();
  41. System.err.println(e);
  42. }
  43. }
  44.  
  45. public ResultSet setQuery(String qry) {
  46. ResultSet resultSet = null;
  47. try {
  48. Statement st = this.connection.createStatement();
  49. resultSet = st.executeQuery(qry);
  50. } catch (SQLException e) {
  51. setConnect();
  52. System.err.println(e);
  53. }
  54. return resultSet;
  55. }
  56.  
  57. public void setClose() {
  58. try {
  59. if (this.connection != null) {
  60. this.connection.close();
  61. System.out.println("[MySQL] Verbindung aufgebaut zur MySQL");
  62. }
  63. } catch (SQLException e) {
  64. System.out.println("[MySQL] Fehler:" + e.getMessage());
  65. }
  66. }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement