Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.jonxthxnlf.oneline.mysql;
- import java.io.File;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.UUID;
- import org.bukkit.configuration.file.YamlConfiguration;
- import de.jonxthxnlf.oneline.main.Main;
- public class MySQL {
- public static Connection con;
- public static void registerMySQLConfig() {
- File file = new File("plugins//OneLine", "config.yml");
- YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
- cfg.addDefault("mysql.host", "localhost");
- cfg.addDefault("mysql.database", "oneline");
- cfg.addDefault("mysql.user", "root");
- cfg.addDefault("mysql.password", "AmyMustang21");
- cfg.addDefault("mysql.port", 3306);
- cfg.options().copyDefaults(true);
- try {
- cfg.save(file);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public MySQL() {
- connect();
- }
- public void connect() {
- try {
- con = DriverManager.getConnection(
- "jdbc:mysql://" + "localhost" + ":" + "3306" + "/" + "oneline" + "?autoReconnect=true", "root", "AmyMustang21");
- System.out.println("MySQL - Erfolgreich hergestellt");
- } catch (SQLException e) {
- System.out.println("MySQL - Fehler | Fehler: " + e.getMessage());
- }
- }
- public void close() {
- try {
- if (con != null) {
- con.close();
- System.out.println("[MySQL] Die Verbindung zur MySQL wurde Erfolgreich beendet!");
- }
- } catch (SQLException e) {
- System.out.println("[MySQL] Fehler beim beenden der Verbindung zur MySQL! Fehler: " + e.getMessage());
- }
- }
- public void update(String qry) {
- try {
- Statement st = con.createStatement();
- st.executeUpdate(qry);
- st.close();
- } catch (SQLException e) {
- connect();
- System.err.println(e);
- }
- }
- public ResultSet query(String qry) {
- ResultSet rs = null;
- try {
- Statement st = con.createStatement();
- rs = st.executeQuery(qry);
- } catch (SQLException e) {
- connect();
- System.err.println(e);
- }
- return rs;
- }
- public static int getRank(String uuid) {
- int rank = 0;
- try {
- PreparedStatement ps = (PreparedStatement) con
- .prepareStatement("SELECT * FROM oneline ORDER BY POINTS DESC");
- ResultSet result = ps.executeQuery();
- while (result.next()) {
- rank++;
- String uuid2 = result.getString("UUID");
- if (uuid2.equalsIgnoreCase(uuid)) {
- return rank;
- }
- }
- result.close();
- ps.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return rank;
- }
- public static UUID getRank(int id) {
- int i = 0;
- ResultSet rs = Main.mysql.query("SELECT * FROM oneline ORDER BY POINTS DESC LIMIT " + id);
- try {
- while (rs.next()) {
- i++;
- if (i == id) {
- return UUID.fromString(rs.getString("UUID"));
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- public void reconnect() {
- if (con == null) {
- connect();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement