Advertisement
Guest User

Untitled

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