Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.agentslim.main;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.plugin.java.JavaPlugin;
- import java.io.File;
- import java.io.IOException;
- import java.sql.*;
- public class MySQL
- {
- private JavaPlugin plugin;
- private Connection connection = null;
- private String host = "";
- private int port = 0;
- private String username = "";
- private String password = "";
- private String database = "";
- public MySQL(JavaPlugin plugin)
- {
- this.plugin = plugin;
- File file = new File(this.plugin.getDataFolder(), "db.yml");
- YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
- if (!file.exists())
- {
- configuration.set("MySQL.host", "localhost");
- configuration.set("MySQL.port", 3306);
- configuration.set("MySQL.user", "root");
- configuration.set("MySQL.host", "root");
- configuration.set("MySQL.host", "minecraft_plugins");
- try
- {
- configuration.save(file);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- this.host = configuration.getString("MySQL.host");
- this.port = configuration.getInt("MySQL.port");
- this.username = configuration.getString("MySQL.user");
- this.password = configuration.getString("MySQL.password");
- this.database = configuration.getString("MySQL.database");
- }
- public boolean connect()
- {
- try
- {
- this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8", this.username, this.password);
- plugin.getLogger().info("Database connection established!");
- }
- catch (SQLException e)
- {
- this.plugin.getLogger().warning("Fehler beim verbinden zur Datenbank!");
- }
- return this.connection != null;
- }
- public void disconnect()
- {
- if (isConnected())
- {
- try
- {
- this.connection.close();
- }
- catch (SQLException ignored)
- {
- }
- finally
- {
- this.connection = null;
- }
- }
- }
- public boolean isConnected()
- {
- return this.connection != null;
- }
- public void Update(String qry)
- {
- Statement stmt = null;
- try
- {
- stmt = this.connection.createStatement();
- }
- catch (SQLException e)
- {
- this.plugin.getLogger().severe("MySQL failed to create Statement [Update]!" + e.getMessage());
- }
- try
- {
- stmt.executeUpdate(qry);
- }
- catch (SQLException e)
- {
- this.plugin.getLogger().severe("MySQL failed to execute Update [Update]!" + e.getMessage());
- }
- try
- {
- stmt.close();
- }
- catch (SQLException e)
- {
- this.plugin.getLogger().severe("MySQL failed to close Statement [Update]!" + e.getMessage());
- }
- }
- public ResultSet Query(String qry)
- {
- ResultSet rs = null;
- Statement stmt = null;
- try
- {
- stmt = this.connection.createStatement();
- }
- catch (SQLException e)
- {
- this.plugin.getLogger().severe("MySQL failed to create Statement [Query]! " + e.getMessage());
- }
- try
- {
- rs = stmt.executeQuery(qry);
- }
- catch (SQLException e)
- {
- this.plugin.getLogger().severe("MySQL failed to execute Query [Query]! " + e.getMessage());
- }
- return rs;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement