Advertisement
Guest User

Untitled

a guest
Feb 8th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. package de.habdxchlieb.Utils;
  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. import de.habdxchlieb.Main.LifeGeldAPI;
  10.  
  11. public class API_MySQL {
  12.  
  13. private String HOST = LifeGeldAPI.host;
  14. private String DATABASE = LifeGeldAPI.Database;
  15. private String USER = LifeGeldAPI.user;
  16. private String PASSWORD = LifeGeldAPI.password;
  17. private Connection connection;
  18.  
  19. public API_MySQL(String host, String database, String user, String password) {
  20. this.HOST = host;
  21. this.DATABASE = database;
  22. this.USER = user;
  23. this.PASSWORD = password;
  24.  
  25. connect();
  26. }
  27. public void connect() {
  28. try {
  29. this.connection = DriverManager.getConnection("jdbc:mysql://" + this.HOST + ":3306/" + this.DATABASE + "?autoReconnect=true", this.USER, this.PASSWORD);
  30. System.out.println("§c§l[MySQL] §7Die verbindung zur MySQL wurde hergestellt!");
  31.  
  32. }
  33. catch (SQLException e) {
  34. System.out.println("§c§l[MySQL] §4Die verbindung zur MySQL ist fehlgeschlagen!");
  35.  
  36. }
  37. }
  38. public void close() {
  39. try {
  40. if (this.connection !=null) {
  41. this.connection.close();
  42. System.out.println("§c§l[MySQL] §aDie verbindung zur MySQL wurde erfolgreich beendet!");
  43. }
  44. }
  45. catch (SQLException e) {
  46. System.out.println("§c§l[MySQL] §5Fehler beim beenden der Verbindung zu MySQL! &6Fehler:" + e.getMessage());
  47. }
  48. }
  49. public void update(String qry) {
  50. try {
  51. if (this.connection.isClosed()) {
  52. connect();
  53. }
  54.  
  55. Statement st = this.connection.createStatement();
  56. st.executeUpdate(qry);
  57. st.close();
  58. }
  59. catch (SQLException e) {
  60. System.err.println(e);
  61.  
  62. }
  63. }
  64. public ResultSet query(String qry) {
  65. ResultSet rs = null;
  66. try {
  67. if(this.connection.isClosed()) {
  68. connect();
  69. }
  70. Statement st = this.connection.createStatement();
  71. st.executeQuery(qry);
  72. return st.getResultSet();
  73.  
  74. }
  75.  
  76.  
  77. catch (SQLException e) {
  78. System.err.println(e);
  79. }
  80.  
  81. return rs;
  82. }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement