Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.MrObst.KnockOut.MySQL;
- 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 String user, password, host, database, prot;
- private Connection con;
- public Connection getCon() {
- return con;
- }
- 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.prot = port;
- }
- public void connect(){
- if(!isConnected()){
- try {
- setCon(DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.prot + "/" + this.database + "7autoReconnect=true", this.user, this.password));
- } 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)");
- update("CREATE TABLE IF NOT EXISTS PlayerInfo(UUID VARCHAR(64), NAME VARCHAR(16))");
- }
- public boolean isExistsByName(String name){
- try {
- ResultSet rs = query("SELECT * FROM PlayerInfo WHERE NAME='" + name + "'");
- if(rs.next()){
- return rs.getString("NAME") != null;
- }
- } catch (SQLException exc) {
- }
- return false;
- }
- public boolean isExistsByUUID(String name){
- try {
- ResultSet rs = query("SELECT * FROM PlayerInfo WHERE UUID='" + name + "'");
- if(rs.next()){
- return rs.getString("UUID") != null;
- }
- } catch (SQLException exc) {
- }
- return false;
- }
- public void createPlayerInfo(String uuid, String name){
- if(!isExistsByUUID(uuid)){
- update("INSERT INTO PlayerInfo(UUID NAME) VALUES ('" + uuid + "', '" + name + "'')");
- }
- }
- public void updateName(String uuid, String name){
- if(isExistsByUUID(uuid)){
- update("UPDATE PlayerInfo SET NAME='" + name + "' WHERE UUID='" + uuid + "'");
- }
- }
- public String getName(String uuid){
- if(isExistsByUUID(uuid)){
- try{
- ResultSet rs = query("SELECT * FROM PlayerInfo WHERE UUID='" + uuid + "'");
- if(rs.next()){
- return rs.getString("NAME");
- }
- }catch (SQLException exc) {
- }
- }else{
- createPlayer(uuid);
- getName(uuid);
- }
- return null;
- }
- public String getUUID(String uuid){
- if(isExistsByUUID(uuid)){
- try{
- ResultSet rs = query("SELECT * FROM PlayerInfo WHERE NAME='" + uuid + "'");
- if(rs.next()){
- return rs.getString("UUID");
- }
- }catch (SQLException exc) {
- }
- }else{
- createPlayer(uuid);
- getUUID(uuid);
- }
- return null;
- }
- 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='" + uuid + "'");
- }else{
- createPlayer(uuid);
- setPoints(uuid, kills);
- }
- }
- public void setDeaths(String uuid, int deaths){
- if(isExists(uuid)){
- update("UPDATE Stats SET Deaths='" + deaths + "' WHERE UUID='" + uuid + "'");
- }else{
- createPlayer(uuid);
- setPoints(uuid, deaths);
- }
- }
- public Integer getKills(String uuid){
- if(isExists(uuid)){
- try{
- ResultSet rs = query("SELECT * FROM Stats WHERE UUID='" + uuid + "'");
- if(rs.next()){
- return rs.getInt("Kills");
- }
- }catch (SQLException exc) {
- }
- }else{
- createPlayer(uuid);
- getKills(uuid);
- }
- return 0;
- }
- public Integer getDeaths(String uuid){
- if(isExists(uuid)){
- try{
- ResultSet rs = query("SELECT * FROM Stats WHERE UUID='" + uuid + "'");
- if(rs.next()){
- return rs.getInt("Deaths");
- }
- }catch (SQLException exc) {
- }
- }else{
- createPlayer(uuid);
- getDeaths(uuid);
- }
- return 0;
- }
- 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)){
- setPoints(uuid, getPoints(uuid) - points);
- }else{
- createPlayer(uuid);
- removePoints(uuid, points);
- }
- }
- public void addKills(String uuid, int kills){
- if(isExists(uuid)){
- setKills(uuid, getKills(uuid) + kills);
- }else{
- createPlayer(uuid);
- addKills(uuid, kills);
- }
- }
- public void removeKills(String uuid, int kills){
- if(isExists(uuid)){
- setKills(uuid, getKills(uuid) - kills);
- }else{
- createPlayer(uuid);
- removeKills(uuid, kills);
- }
- }
- public void addDeaths(String uuid, int deaths){
- if(isExists(uuid)){
- setDeaths(uuid, getDeaths(uuid) + deaths);
- }else{
- createPlayer(uuid);
- addDeaths(uuid, deaths);
- }
- }
- public void removeDeaths(String uuid, int deaths){
- if(isExists(uuid)){
- setDeaths(uuid, getDeaths(uuid) - deaths);
- }else{
- createPlayer(uuid);
- removeDeaths(uuid, deaths);
- }
- }
- public int getRank(String uuid){
- int rank = -1;
- try{
- ResultSet rs = query("SELECT * FROM Stats ORDER 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 LIMIT 10");
- while(rs.next()){
- list.add(rs.getString("UUID"));
- }
- }catch (SQLException exc) {
- }
- return list;
- }
- public List<String> getAll(){
- 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