Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.deinplugin.betakeysystem.database;
- import net.md_5.bungee.BungeeCord;
- import java.sql.*;
- import java.util.ArrayList;
- /**
- * JavaDoc this file!
- * Created: 10.11.2018
- *
- * @author WeLoveSpigotPlugins (welovespigotplugins@gmail.com)
- */
- public class MySQLAdapter {
- private final String host;
- private final int port;
- private final String password;
- private final String database;
- private final String username;
- private Connection connection;
- public MySQLAdapter(String host, int port, String password, String username, String database) {
- this.host = host;
- this.port = port;
- this.password = password;
- this.database = database;
- this.username = username;
- }
- public void connect() {
- if(!isConnected()){
- try {
- this.connection = DriverManager.getConnection("jdbc:MySQL://" + host + ":" + port + "/" + database+"?autoReconnect=true", username, password);
- BungeeCord.getInstance().getConsole().sendMessage( "§aDie Verbindung zur Datenbank wurde erfolgreich hergestellt.");
- } catch (SQLException e) {
- e.printStackTrace();
- BungeeCord.getInstance().getConsole().sendMessage("§cDie Verbindung zur Datenbank wurde nicht hergestellt. (Falsche Daten?)");
- }
- }
- }
- public void disconnect() {
- if(isConnected()){
- try {
- this.connection.close();
- BungeeCord.getInstance().getConsole().sendMessage( "§cDie Verbindung zur Datenbank wurde geschlossen.");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public ResultSet getResult(String query) {
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- ps = this.connection.prepareStatement(query);
- rs = ps.executeQuery();
- return rs;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- public ArrayList<String> getAllFromTable(final String tableName, final String columnName){
- ResultSet rs = getResult("SELECT * FROM " + tableName);
- String namen = null;
- ArrayList<String> nicks = new ArrayList<>();
- try {
- while (rs.next()) {
- namen = rs.getString(columnName);
- nicks.add(namen);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return nicks;
- }
- public boolean isConnected(){
- return (this.connection == null ? false : true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement