Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Myql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- public class MySQL {
- private static final List<String> List = null;
- private String user, password, host, database, port;
- private Connection con;
- private Object DriverManger;
- public void setCon(Connection con) {
- this.con = con;
- }
- public MySQL(String user, String password, String host, String database, String port) {
- this.user = user;
- this.password = password;
- this.host = host;
- this.database = database;
- this.port = port;
- }
- public void connect() {
- if(isConnected()) {
- try {
- setCon(DriverManager.getConnection("jdbc:msql://" + this.host + ":" + this.port));
- } catch (SQLException exc) {
- exc.printStackTrace();
- }
- }
- }
- public boolean isConnected() {
- return getCon() != null;
- }
- public void close() {
- if (isConnected()) {
- try {
- getCon().close();
- } catch (SQLException exc) {
- exc.printStackTrace();
- }
- }
- }
- public void update(String qry) {
- if(isConnected()) {
- try {
- PreparedStatement ps = getCon().prepareStatement(qry);
- ps.executeUpdate();
- } catch (SQLException exc) {
- exc.printStackTrace();
- }
- }
- }
- public ResultSet query(String qry) {
- ResultSet rs = null;
- try {
- PreparedStatement ps =getCon().prepareStatement(qry);
- rs = ps.executeQuery();
- } catch (SQLException exc) {
- } return rs;
- }
- }
- public void createTable() {
- update("CREATE TABLE IF NOT EXISTS Stats(UUID varchar(64), Points int, Kills int, Deaths int;)");
- }
- public boolean isExists(String uuid) {
- try {
- ResultSet rs = query("SELECT * = FROM Stats WHERE UUID='" + uuid +"'");
- if(rs.next()) {
- return rs.getString("UUID") != null;
- }
- } catch (SQLException exc) {
- }
- return false;
- }
- public void createPlayer(String uuid) {
- if(!isExists(uuid)) {
- update("INSERT INTO Stats(UUID, Points, Kills, Deaths) VALUES ('" + uuid + "', '0', '0', '0')");
- }
- }
- public void setPoints(String uuid, int points) {
- if(!isExists(uuid)) {
- update("UPDATE Stats SET Points='" + points + "' WHERE UUID='" + uuid + "'");
- } else {
- createPlayer(uuid);
- setPoints(uuid, points);
- }
- }
- public Integer getPoints(String uuid) {
- if(isExists(uuid)) {
- try {
- ResultSet rs = query("SELECT * FROM Stats WHERE UUID='" + uuid + "'");
- if (rs.next()) {
- return rs.getInt("Points");
- }
- } catch (SQLException exc) {
- }
- } else {
- createPlayer(uuid);
- getPoints(uuid);
- }
- return 0;
- }
- public void setKills(String uuid, int kills) {
- if(isExists(uuid)) {
- update("UPDATE Stats SET Kills='" + kills + "'WHERE UUID='" + "'");
- } else {
- createPlayer(uuid);
- setKills(uuid, kills);
- }
- }
- public void setDeaths(String uuid, int deaths) {
- if(isExists(uuid)) {
- update("UPDATE Stats SET DEATHS='" + deaths + "'WHERE UUID='" + "'");
- } else {
- createPlayer(uuid);
- setKills(uuid, deaths);
- }
- }
- public Integer getKills(String uuid) {
- Integer i = Integer.valueOf(0);
- if(isExists(uuid)) {
- ResultSet rs =query("Select * FROM Stats WHERE UUID='" + uuid + "'");
- try {
- if(rs.next()) {
- i = rs.getInt("Kills");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- } else {
- createPlayer(uuid);
- getKills(uuid);
- }
- return i;
- }
- public Integer getDeaths(String uuid) {
- int i = Integer.valueOf(0);
- if(isExists(uuid)) {
- ResultSet rs = query("SELECT * FROM Stats WHERE UUID'" + uuid + "'");
- try {
- if(rs.next()) {
- i = rs.getInt("Deaths");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- } else {
- createPlayer(uuid);
- getDeaths(uuid);
- }
- return i;
- }
- public void addPoints(String uuid, int points) {
- if(isExists(uuid)) {
- setPoints(uuid, getPoints(uuid) + points);
- } else {
- createPlayer(uuid);
- addPoints(uuid, points);
- }
- }
- public void removePoints(String uuid, int points) {
- if(isExists(uuid)) {
- removePoints(uuid, getPoints(uuid) + points);
- } else {
- createPlayer(uuid);
- addPoints(uuid, points);
- }
- }
- public void addKills(String uuid, int kills) {
- if(isExists(uuid)) {
- setKills(uuid, getPoints(uuid) + kills);
- } else {
- createPlayer(uuid);
- addKills(uuid, kills);
- }
- }
- public void addDeaths(String uuid, int deaths) {
- if(isExists(uuid)) {
- setDeaths(uuid, getPoints(uuid) + deaths);
- } else {
- createPlayer(uuid);
- addDeaths(uuid, deaths);
- }
- }
- public void removeKills(String uuid, int kills) {
- if(isExists(uuid)) {
- setKills(uuid, getPoints(uuid) + kills);
- } else {
- createPlayer(uuid);
- removeKills(uuid, kills);
- }
- }
- public void removeDeaths(String uuid, int deaths) {
- if(isExists(uuid)) {
- setDeaths(uuid, getPoints(uuid) + deaths);
- } else {
- createPlayer(uuid);
- removeDeaths(uuid, deaths);
- }
- }
- public int getRank(String uuid) {
- int rank = -1;
- try {
- ResultSet rs = query("SELECT * FROM Stats ODER BY POINTS DESC");
- while(rs.next()) {
- String uuid2 = rs.getString("UUID");
- if(uuid2.equalsIgnoreCase(uuid)) {
- rank = rs.getRow();
- break;
- }
- }
- } catch (SQLException exc) {
- }
- return rank;
- }
- public String getUUID(int rank) {
- try {
- int nrank = rank -1;
- ResultSet rs = query("SELECT * FROM Stats ORDER BY POINTS DESC LIMIT" + nrank + ", " + rank);
- if (rs.next()) {
- return rs.getString("UUID");
- }
- } catch (SQLException exc) {
- }
- return null;
- }
- public List<String> getTopTen() {
- List<String> list = new ArrayList<>();
- try {
- ResultSet rs = query("SELECT * FROM Stats ORDER BY POINTS DESC");
- while(rs.next()) {
- list.add(rs.getString("UUID"));
- }
- } catch (SQLException exc) {
- }
- return List;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement