Advertisement
Guest User

MySQL Spigot Class

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