Advertisement
Guest User

Untitled

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