Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.fabegabe.ranks.sql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.UUID;
- import me.fabegabe.ranks.config.Config;
- import me.fabegabe.ranks.util.UUIDFetcher;
- import me.fabegabe.ranks.util.UUIDFetcher.Response;
- import org.bukkit.configuration.ConfigurationSection;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.entity.Player;
- public class SQL
- {
- private Connection connection;
- private String address;
- private String username;
- private String password;
- private String database;
- private int port;
- public SQL(Config config)
- {
- ConfigurationSection section = config.getConfig().getConfigurationSection("sql");
- this.address = section.getString("address");
- this.username = section.getString("username");
- this.password = section.getString("password");
- this.database = section.getString("database");
- this.port = section.getInt("port");
- }
- public synchronized void connect()
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- this.connection = DriverManager.getConnection("jdbc:mysql://" + this.address + ":" + this.port + "/" + this.database,
- this.username, this.password);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public synchronized void disconnect()
- {
- try
- {
- this.connection.close();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public PreparedStatement prepare(String query)
- {
- try
- {
- return this.connection.prepareStatement(query);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- public synchronized boolean containsPlayer(Player p)
- {
- try
- {
- UUID uuid = UUIDFetcher.getResponse(p.getName()).uuid;
- PreparedStatement ps = prepare("SELECT * FROM `users` WHERE uuid=?;");
- ps.setString(1, uuid.toString());
- ResultSet set = ps.executeQuery();
- boolean contains = set.next();
- set.close();
- ps.close();
- return contains;
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return false;
- }
- public synchronized boolean containsPlayer(String name)
- {
- try
- {
- UUID uuid = UUIDFetcher.getResponse(name).uuid;
- PreparedStatement ps = prepare("SELECT * FROM `users` WHERE uuid=?;");
- ps.setString(1, uuid.toString());
- ResultSet set = ps.executeQuery();
- boolean contains = set.next();
- set.close();
- ps.close();
- return contains;
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return false;
- }
- public synchronized void registerPlayer(Player p)
- {
- if (containsPlayer(p)) {
- return;
- }
- try
- {
- UUID uuid = UUIDFetcher.getResponse(p.getName()).uuid;
- PreparedStatement ps = prepare("INSERT INTO `users` values(?,0,1000,1);");
- ps.setString(1, uuid.toString());
- ps.execute();
- ps.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement