Advertisement
Guest User

Untitled

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