Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main
- package de.wave.mysql.main;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import org.bukkit.plugin.java.JavaPlugin;
- import de.wave.mysql.mysql.MySQL;
- import de.wave.mysql.mysql.MySQLFile;
- import net.md_5.bungee.api.ChatColor;
- public class main extends JavaPlugin {
- public void onEnable() {
- MySQLFile file = new MySQLFile();
- file.setStandard();
- file.readData();
- MySQL.connect();
- try {
- PreparedStatement ps = MySQL.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS test (UUID VARCHAR(1000),Spielername VARCHAR(100),Punkte INT(10000))");
- ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- System.out.println(ChatColor.RED + "[MySQl]Startet");
- }
- public void onDisable() {
- MySQL.disconnect();
- System.out.println(ChatColor.RED + "[MySQL]Stoppet");
- }
- }
- ---------------------------------------------------------------------------------------------------
- MYSQLFile
- package de.wave.mysql.mysql;
- import java.io.File;
- import java.io.IOException;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- public class MySQLFile {
- public void setStandard() {
- FileConfiguration cfg = getFileConifuration();
- cfg.options().copyDefaults(true);
- cfg.addDefault("host", "localhost");
- cfg.addDefault("port", "3306");
- cfg.addDefault("database", "deine bank");
- cfg.addDefault("username", "root");
- cfg.addDefault("password", "pw");
- try {
- cfg.save(getFile());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private File getFile() {
- return new File("plugins/MySQL", "mysql.yml");
- }
- private FileConfiguration getFileConifuration() {
- return YamlConfiguration.loadConfiguration(getFile());
- }
- public void readData() {
- FileConfiguration cfg = getFileConifuration();
- MySQL.host = cfg.getString("host");
- MySQL.port = cfg.getString("port");
- MySQL.database = cfg.getString("database");
- MySQL.username = cfg.getString("username");
- MySQL.password = cfg.getString("password");
- }
- }
- ------------------------------------------------------------------------------
- MySQL
- package de.wave.mysql.mysql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class MySQL {
- public static String host;
- public static String port;
- public static String database;
- public static String username;
- public static String password;
- public static Connection con;
- public static void connect() {
- if(!isConnected()) {
- try {
- con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
- System.out.println("[MySQL] Verbinding aufgebaut!");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void disconnect() {
- if(isConnected()) {
- try {
- con.close();
- System.out.println("[MySQL] Verbindung geschlossen");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static boolean isConnected() {
- return (con == null ? false : true);
- }
- public static Connection getConnection() {
- return con;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement