Advertisement
Guest User

MySQl

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