Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 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", USER, PASSWORD);
  22. System.out.println("[MySQL] Die Verbindung zur MySQL wurde hergestellt!");
  23. } catch (SQLException e) {
  24. System.out.println("[MySQL] Die Verbindung zur MySQL ist fehlgeschlagen! Fehler: " + e.getMessage());
  25. }
  26. }
  27.  
  28. public void close() {
  29. try {
  30. if(con != null) {
  31. con.close();
  32. System.out.println("[MySQL] Die Verbindung zur MySQL wurde Erfolgreich beendet!");
  33. }
  34. } catch (SQLException e) {
  35. System.out.println("[MySQL] Fehler beim beenden der Verbindung zur MySQL! Fehler: " + e.getMessage());
  36. }
  37. }
  38.  
  39. public void update(String qry) {
  40. try {
  41. Statement st = con.createStatement();
  42. st.executeUpdate(qry);
  43. st.close();
  44. } catch (SQLException e) {
  45. connect();
  46. System.out.println("[MySQL] Fehler beim beenden der Verbindung zur MySQL! Fehler: " + e.getMessage());
  47. }
  48. }
  49.  
  50. public ResultSet query(String qry) {
  51. ResultSet rs = null;
  52.  
  53. try {
  54. Statement st = con.createStatement();
  55. rs = st.executeQuery(qry);
  56. } catch (SQLException e) {
  57. connect();
  58. System.err.println(e);
  59. }
  60. return rs;
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement