Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. package de.DevKill.MySQL;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. import de.DevKill.Friends;
  9. import net.md_5.bungee.api.ProxyServer;
  10. import net.md_5.bungee.api.chat.TextComponent;
  11.  
  12. public class MySQL {
  13.  
  14. public String username;
  15. public String password;
  16. public String database;
  17. public String host;
  18. public String port;
  19. public Connection con;
  20.  
  21. Friends friends;
  22.  
  23. public MySQL(Friends friends) {
  24.  
  25. this.friends = friends;
  26.  
  27. }
  28.  
  29. public void connect() {
  30. if (!isConnected()) {
  31. try {
  32. con = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + database + "?autoReconnect=true",
  33. username, password);
  34. ProxyServer.getInstance().getConsole()
  35. .sendMessage(new TextComponent(friends.getPrefix() + "§aSuccessfully connected to MySQL-Database."));
  36. } catch (SQLException e) {
  37. ProxyServer.getInstance().getConsole()
  38. .sendMessage(new TextComponent(friends.getPrefix() + "§cCould not connect to MySQL-Database, please check your MySQL-Settings."));
  39. }
  40. }
  41. }
  42.  
  43. public void close() {
  44. if (isConnected()) {
  45. try {
  46. con.close();
  47. con = null;
  48. ProxyServer.getInstance().getConsole()
  49. .sendMessage(new TextComponent(friends.getPrefix() + "§aSuccessfully closed MySQL-Connection."));
  50. } catch (SQLException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55.  
  56. public boolean isConnected() {
  57. return con != null;
  58. }
  59.  
  60. public void update(String qry) {
  61. if (isConnected()) {
  62. try {
  63. con.createStatement().executeUpdate(qry);
  64. } catch (SQLException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. }
  69.  
  70. public ResultSet getResult(String qry) {
  71. if (isConnected()) {
  72. try {
  73. return con.createStatement().executeQuery(qry);
  74. } catch (SQLException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. return null;
  79. }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement