Advertisement
Guest User

Untitled

a guest
Dec 24th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. package net.deinplugin.betakeysystem.database;
  2.  
  3. import net.md_5.bungee.BungeeCord;
  4.  
  5. import java.sql.*;
  6. import java.util.ArrayList;
  7.  
  8. /**
  9. * JavaDoc this file!
  10. * Created: 10.11.2018
  11. *
  12. * @author WeLoveSpigotPlugins (welovespigotplugins@gmail.com)
  13. */
  14. public class MySQLAdapter {
  15.  
  16. private final String host;
  17. private final int port;
  18. private final String password;
  19. private final String database;
  20. private final String username;
  21.  
  22. private Connection connection;
  23.  
  24. public MySQLAdapter(String host, int port, String password, String username, String database) {
  25. this.host = host;
  26. this.port = port;
  27. this.password = password;
  28. this.database = database;
  29. this.username = username;
  30. }
  31.  
  32. public void connect() {
  33.  
  34. if(!isConnected()){
  35. try {
  36. this.connection = DriverManager.getConnection("jdbc:MySQL://" + host + ":" + port + "/" + database+"?autoReconnect=true", username, password);
  37. BungeeCord.getInstance().getConsole().sendMessage( "§aDie Verbindung zur Datenbank wurde erfolgreich hergestellt.");
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. BungeeCord.getInstance().getConsole().sendMessage("§cDie Verbindung zur Datenbank wurde nicht hergestellt. (Falsche Daten?)");
  41. }
  42. }
  43.  
  44. }
  45.  
  46. public void disconnect() {
  47. if(isConnected()){
  48. try {
  49. this.connection.close();
  50. BungeeCord.getInstance().getConsole().sendMessage( "§cDie Verbindung zur Datenbank wurde geschlossen.");
  51. } catch (SQLException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56.  
  57. public ResultSet getResult(String query) {
  58. PreparedStatement ps = null;
  59. ResultSet rs = null;
  60. try {
  61. ps = this.connection.prepareStatement(query);
  62. rs = ps.executeQuery();
  63. return rs;
  64. } catch (SQLException e) {
  65. e.printStackTrace();
  66. }
  67. return null;
  68. }
  69.  
  70. public ArrayList<String> getAllFromTable(final String tableName, final String columnName){
  71.  
  72. ResultSet rs = getResult("SELECT * FROM " + tableName);
  73. String namen = null;
  74. ArrayList<String> nicks = new ArrayList<>();
  75. try {
  76. while (rs.next()) {
  77. namen = rs.getString(columnName);
  78. nicks.add(namen);
  79. }
  80. } catch (SQLException e) {
  81. e.printStackTrace();
  82. }
  83.  
  84. return nicks;
  85.  
  86. }
  87.  
  88.  
  89. public boolean isConnected(){
  90. return (this.connection == null ? false : true);
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement