Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.****.MySQL;
- import java.io.File;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.bukkit.Bukkit;
- import org.bukkit.configuration.file.YamlConfiguration;
- public class MySQL {
- // YOU NEED TO CREATE THE FILE BEFORE RUNNING MYSQL
- public static String HOST = "";
- public static String USERNAME = "";
- public static String PASSWORD = "";
- public static String DATABASE = "";
- public static int port;
- public static Connection connection;
- public MySQL(String host, String username, String password, String database) {
- HOST = host;
- USERNAME = username;
- PASSWORD = password;
- DATABASE = database;
- }
- public static void connect() {
- if (!isConnected()) {
- try {
- connection = DriverManager.getConnection("jdbc:mysql://" + HOST + ":" + "3306" + "/" + DATABASE + "?autoReconnect=true", USERNAME, PASSWORD);
- Bukkit.getConsoleSender().sendMessage("§aVerbindung erfolgreich aufgebaut!");
- } catch (SQLException ex) {
- Bukkit.getConsoleSender().sendMessage("§cVerbindung wurde nicht aufgebaut!");
- }
- }
- }
- public void disconnect() {
- if (isConnected()) {
- try {
- connection.close();
- Bukkit.getConsoleSender().sendMessage("§cVerbindung erfolgreich geschlossen!");
- } catch (SQLException localSQLException) {
- }
- }
- }
- public static boolean isConnected() {
- return connection != null;
- }
- public Connection getConnection() {
- return connection;
- }
- public static void update(String qry) {
- try {
- if (connection.isClosed()) {
- connect();
- }
- Statement stmt = connection.createStatement();
- stmt.executeUpdate(qry);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static ResultSet query(String query) throws SQLException {
- if (connection.isClosed()) {
- connect();
- }
- Statement statement = connection.createStatement();
- return statement.executeQuery(query);
- }
- /*
- * Methods
- */
- public static void connectToMySQL() {
- File file = new File("plugins/LPS/MySQL.yml");
- YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
- new MySQL(cfg.getString("host"), cfg.getString("user"), cfg.getString("password"),cfg.getString("database"));
- }
- public static void createMySQL() {
- File file = new File("plugins/LPS/MySQL.yml");
- YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
- try {
- if (!file.exists()) {
- file.createNewFile();
- cfg.options().copyDefaults(true);
- cfg.addDefault("host", "IP");
- cfg.addDefault("user", "Username");
- cfg.addDefault("password", "Passwort");
- cfg.addDefault("database", "Databasename");
- cfg.save(file);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement