Advertisement
Guest User

Untitled

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