Advertisement
Guest User

Untitled

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