Advertisement
Guest User

Untitled

a guest
May 27th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. public class MySQL {
  2.  
  3. private String HOST = "";
  4. private String DATABASE = "";
  5. private String USER = "";
  6. private String PASSWORD = "";
  7.  
  8. private Connection con;
  9.  
  10. public MySQL(String host, String database, String user, String password) {
  11. this.HOST = host;
  12. this.DATABASE = database;
  13. this.USER = user;
  14. this.PASSWORD = password;
  15.  
  16. connect();
  17. }
  18.  
  19. public void connect() {
  20. try {
  21. con = DriverManager.getConnection("jdbc:mysql://" + HOST + ":3306/" + DATABASE + "?autoReconnect=true",
  22. USER, PASSWORD);
  23. System.out.println("[MySQL] Die Verbindung zur MySQL wurde hergestellt!");
  24. } catch (SQLException e) {
  25. System.out.println("[MySQL] Die Verbindung zur MySQL ist fehlgeschlagen! Fehler: " + e.getMessage());
  26. }
  27. }
  28.  
  29. public void close() {
  30. try {
  31. if (con != null) {
  32. con.close();
  33. System.out.println("[MySQL] Die Verbindung zur MySQL wurde Erfolgreich beendet!");
  34. }
  35. } catch (SQLException e) {
  36. System.out.println("[MySQL] Fehler beim beenden der Verbindung zur MySQL! Fehler: " + e.getMessage());
  37. }
  38. }
  39.  
  40. public void update(String qry) {
  41. try {
  42. Statement st = con.createStatement();
  43. st.executeUpdate(qry);
  44. st.close();
  45. } catch (SQLException e) {
  46. connect();
  47. System.err.println(e);
  48. }
  49. }
  50.  
  51. public ResultSet query(String qry) {
  52. ResultSet rs = null;
  53.  
  54. try {
  55. Statement st = con.createStatement();
  56. rs = st.executeQuery(qry);
  57. } catch (SQLException e) {
  58. connect();
  59. System.err.println(e);
  60. }
  61. return rs;
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement