Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.mcorbita.topki.mysql;
- import org.bukkit.plugin.*;
- import pl.mcorbita.topki.Main;
- import pl.mcorbita.topki.objects.User;
- import pl.mcorbita.topki.objects.UserManager;
- import pl.mcorbita.topki.utils.TopType;
- import pl.mcorbita.topki.utils.Utils;
- import java.sql.*;
- import java.util.*;
- public class MySQL
- {
- private String host;
- private int port;
- private String user;
- private String pass;
- private String database;
- private String url;
- private Connection conn;
- public MySQL(final String host, final int port, final String user, final String pass, final String database) {
- this.host = host;
- this.port = port;
- this.user = user;
- this.pass = pass;
- this.database = database;
- this.buildURL();
- this.connect();
- }
- public void buildURL() {
- this.url = "jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true";
- }
- public boolean isConnected() {
- return this.conn != null;
- }
- public boolean connect() {
- try {
- Utils.sendMsgToConsole((Plugin)Main.getInst(), "&aUsing url to connect: " + this.url);
- this.conn = DriverManager.getConnection(this.url, this.user, this.pass);
- Utils.sendMsgToConsole((Plugin)Main.getInst(), "&aPolaczono z baza danych MySQL!");
- return true;
- }
- catch (SQLException e) {
- Utils.sendMsgToConsole((Plugin)Main.getInst(), "&c" + e.getMessage());
- return false;
- }
- }
- public void createUsersFromMySQL() {
- try {
- int i = 0;
- final ResultSet rs = this.query("SELECT * FROM `tops`");
- while (rs.next()) {
- final UUID uuid = UUID.fromString(rs.getString("uuid"));
- final String name = rs.getString("name");
- final int kills = rs.getInt("kills");
- final int kox = rs.getInt("kox");
- final int ref = rs.getInt("ref");
- final int breakedBlocks = rs.getInt("breakedBlocks");
- final int placedBlocks = rs.getInt("placedBlocks");
- final int obsidian = rs.getInt("obsidian");
- final long time = rs.getLong("time");
- final int money = rs.getInt("money");
- UserManager.createUser(uuid, name, kills, kox, ref, breakedBlocks, placedBlocks, obsidian, time, money);
- ++i;
- }
- Utils.sendMsgToConsole((Plugin)Main.getInst(), "&aZaladowany: &c" + i + " &agraczy z bazy danych!");
- }
- catch (SQLException e) {
- Utils.sendMsgToConsole((Plugin)Main.getInst(), e.getMessage());
- }
- }
- public void send(final String sql) {
- if (!this.isConnected()) {
- this.connect();
- }
- PreparedStatement ps;
- final Thread t = new Thread(() -> {
- try {
- ps = this.conn.prepareStatement(sql);
- ps.execute();
- }
- catch (SQLException e) {
- Utils.sendMsgToConsole((Plugin)Main.getInst(), "&c" + e.getMessage());
- e.printStackTrace();
- }
- return;
- });
- t.start();
- }
- public ResultSet query(final String sql) {
- if (!this.isConnected()) {
- this.connect();
- }
- try {
- return this.conn.createStatement().executeQuery(sql);
- }
- catch (SQLException e) {
- Utils.sendMsgToConsole((Plugin)Main.getInst(), "&c" + e.getMessage());
- return null;
- }
- }
- public Object get(final String tableName, final String columnName, final UUID uuid) {
- try {
- final PreparedStatement ps = this.conn.prepareStatement("SELECT * FROM `" + tableName + "` WHERE uuid = ?");
- ps.setString(1, uuid.toString());
- final ResultSet rs = ps.executeQuery();
- rs.last();
- if (rs.getRow() != 0) {
- rs.first();
- return rs.getObject(columnName);
- }
- }
- catch (SQLException e) {
- Utils.sendMsgToConsole((Plugin)Main.getInst(), "&c" + e.getMessage());
- }
- return null;
- }
- public void set(final String tableName, final String columnName, final Object newValue, final UUID uuid) {
- final Thread t = new Thread() {
- @Override
- public void run() {
- try {
- final PreparedStatement ps = MySQL.this.conn.prepareStatement("UPDATE `" + tableName + "` SET `" + columnName + "`='" + newValue + "' WHERE uuid = ?");
- ps.setString(1, uuid.toString());
- ps.executeUpdate();
- ps.close();
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- };
- t.start();
- }
- public List<User> getTop(final TopType type) {
- final List<User> users = new ArrayList<User>();
- try {
- final PreparedStatement ps = this.conn.prepareStatement("SELECT * FROM `tops` ORDER BY " + TopType.getMySQLType(type) + " DESC LIMIT 10;");
- final ResultSet rs = ps.executeQuery();
- while (rs.next()) {
- users.add(UserManager.getUser(rs.getString("name")));
- }
- }
- catch (SQLException e) {
- Utils.sendMsgToConsole((Plugin)Main.getInst(), "&c" + e.getMessage());
- }
- return users;
- }
- public void saveDefaultData(final User u) {
- this.send("INSERT INTO `tops` (uuid, name, kills, kox, ref, breakedBlocks, placedBlocks, obsidian, time, money) VALUES ('" + u.getUuid() + "', '" + u.getName() + "', '0', '0', '0', '0', '0', '0', '0', '0')");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement