Advertisement
Guest User

Untitled

a guest
Jun 6th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. package me.jannis.bungee.utils;
  2.  
  3. import net.md_5.bungee.api.ProxyServer;
  4.  
  5. import java.sql.*;
  6.  
  7. public class MySQL {
  8.  
  9. Data data = new Data();
  10.  
  11. private String HOST = "";
  12. private String DATABASE = "";
  13. private String USER = "";
  14. private String PASSWORD = "";
  15.  
  16. public static Connection connection;
  17.  
  18. public MySQL(String host, String database, String user, String password) {
  19. this.HOST = host;
  20. this.DATABASE = database;
  21. this.USER = user;
  22. this.PASSWORD = password;
  23.  
  24. connect();
  25. }
  26.  
  27. public void connect() {
  28. if(connection == null) {
  29. try {
  30. connection = DriverManager.getConnection("jdbc:mysql://" + HOST + ":3306/" + DATABASE + "?autoReconnect=true", USER, PASSWORD);
  31. ProxyServer.getInstance().getConsole().sendMessage(data.getPrefix() +"Die Verbindung zur MySQL wurde erfolgreich hergestellt!");
  32. } catch(SQLException e) {
  33. ProxyServer.getInstance().getConsole().sendMessage(data.getPrefix() +"Die Verbindung zur MySQL konnte nicht hergestellt werden!");
  34. e.printStackTrace();
  35. }
  36. }
  37. }
  38.  
  39. public void close() {
  40. if(connection != null) {
  41. try {
  42. connection.close();
  43. ProxyServer.getInstance().getConsole().sendMessage(data.getPrefix() +"Die Verbindung zur MySQL wurde erfolgreich geschlossen!");
  44. } catch(SQLException e) {
  45. ProxyServer.getInstance().getConsole().sendMessage(data.getPrefix() +"Fehler beim beenden der Verbindung zur MySQL!");
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50.  
  51. public void update(String qry) {
  52. try {
  53. connection.createStatement().executeUpdate(qry);
  54. } catch(SQLException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58.  
  59. public ResultSet getResults(String qry) {
  60. try {
  61. return connection.createStatement().executeQuery(qry);
  62. } catch(SQLException e) {
  63. e.printStackTrace();
  64. }
  65.  
  66. return null;
  67. }
  68.  
  69. public static PreparedStatement getStatement(String sql) {
  70. if(connection != null) {
  71. PreparedStatement ps;
  72. try {
  73. ps = connection.prepareStatement(sql);
  74. return ps;
  75. } catch (SQLException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. return null;
  80. }
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement