Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.aleksbertassi.sakurakiller.SQL;
- import java.sql.*;
- import org.bukkit.entity.*;
- public class MySQL
- {
- private String user;
- private String password;
- private String database;
- private String host;
- private Connection connection;
- private Statement stmt;
- public MySQL(final String user, final String password, final String database, final String host) {
- try {
- this.user = user;
- this.password = password;
- this.database = database;
- this.host = host;
- this.connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database, user, password);
- (this.stmt = this.connection.createStatement()).execute("CREATE TABLE IF NOT EXISTS killer (player VARCHAR(255), wins INTEGER)");
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void addNew(final String player, final int wins) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- final String sql = "INSERT INTO killer (player, wins) VALUES ('" + player + "', '" + wins + "');";
- this.stmt.executeUpdate(sql);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void updateWins(final String player, final int wins) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- final String sql = "UPDATE killer SET wins='" + (this.getWins(player) + wins) + "' WHERE player='" + player + "';";
- this.stmt.executeUpdate(sql);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- public int getWins(final String player) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- final String sql = "SELECT wins FROM killer WHERE player='" + player + "';";
- final ResultSet rs = this.stmt.executeQuery(sql);
- if (rs.next()) {
- return rs.getInt("wins");
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return 0;
- }
- public boolean hasClan(final String player) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- final String sql = "SELECT * FROM killer WHERE player='" + player + "'";
- final ResultSet rs = this.stmt.executeQuery(sql);
- return rs.next() && rs.getString("player").equalsIgnoreCase(player);
- }
- catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- public void getTOPWins(final Player p) {
- try {
- p.sendMessage("§5»§d TOP 10 VENCEDORES DO EVENTO KILLER:");
- Class.forName("com.mysql.jdbc.Driver");
- final String sql = "SELECT * FROM killer ORDER BY wins DESC LIMIT 10";
- final ResultSet rs = this.stmt.executeQuery("SELECT * FROM killer ORDER BY wins DESC LIMIT 10");
- final String sendM = "&5[@posicao] &d@player &d» &f@vitorias &dvitórias.";
- int i = 0;
- if (rs.next()) {
- while (rs.next()) {
- ++i;
- p.sendMessage(sendM.replace("&", "§").replace("@posicao", new StringBuilder(String.valueOf(i)).toString()).replace("@clan", rs.getString("player")).replace("@vitorias", new StringBuilder(String.valueOf(rs.getInt("wins"))).toString()));
- }
- }
- else {
- p.sendMessage(sendM.replace("&", "§").replace("@posicao", new StringBuilder(String.valueOf(i)).toString()).replace("@clan", rs.getString("player")).replace("@vitorias", new StringBuilder(String.valueOf(rs.getInt("wins"))).toString()));
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void addWinnerPoint(final String clan) {
- if (this.hasClan(clan)) {
- this.updateWins(clan, 1);
- }
- else {
- this.addNew(clan, 1);
- }
- }
- public void purgeRows() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- final String sql = "DELETE FROM killer;";
- this.stmt.executeUpdate("DELETE FROM killer;");
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- public String getUser() {
- return this.user;
- }
- public void setUser(final String user) {
- this.user = user;
- }
- public String getPassword() {
- return this.password;
- }
- public void setPassword(final String password) {
- this.password = password;
- }
- public String getDatabase() {
- return this.database;
- }
- public void setDatabase(final String database) {
- this.database = database;
- }
- public String getHost() {
- return this.host;
- }
- public void setHost(final String host) {
- this.host = host;
- }
- public Connection getConnection() {
- return this.connection;
- }
- public void setConnection(final Connection connection) {
- this.connection = connection;
- }
- public Statement getStmt() {
- return this.stmt;
- }
- public void setStmt(final Statement stmt) {
- this.stmt = stmt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement