Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.koalak.skywars.database;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.HashMap;
- import java.util.UUID;
- import org.bukkit.Bukkit;
- import org.bukkit.entity.Player;
- import fr.tigger.elycoinsapi.util.PlayerUtil;
- /**
- * Created by Tigger_San on 29/08/2016.
- */
- public class SQL
- {
- private Connection connection;
- @SuppressWarnings("unused")
- private PreparedStatement preparedStatement;
- private Statement statement;
- private ResultSet result;
- private String url;
- private String host;
- private String database;
- private String user;
- private String pass;
- public void connect(String url, String host, String database, String user, String pass){
- this.url = url;
- this.host=host;
- this.database=database;
- this.user=user;
- this.pass=pass;
- try{
- this.connection = DriverManager.getConnection(this.url+ this.host+"/"+this.database,this.user,this.pass);
- this.statement = connection.createStatement();
- System.out.println("OK");
- }
- catch(SQLException e){
- e.printStackTrace();
- }
- }
- public void connectTo() throws ClassNotFoundException, SQLException {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database, user, pass);
- statement = connection.createStatement();
- }
- private Connection connect() {
- try {
- if (statement != null && connection != null) {
- return connection;
- } else {
- connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database,user, pass);
- statement = connection.createStatement();
- return connection;
- }
- }catch (SQLException e) {
- e.printStackTrace();
- return connection;
- }
- }
- private Statement statement() {
- try {
- if (statement != null && connection != null) {
- return statement;
- } else {
- connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + database, user, pass);
- statement = connection.createStatement();
- return statement;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- return statement;
- }
- }
- public void close() {
- try {
- if (result != null)
- result.close();
- if (statement != null)
- statement.close();
- if (connect() != null)
- connect().close();
- } catch (SQLException e) {
- }
- }
- public void initTable(){
- try{
- statement().executeUpdate("create table if not exists Skywars_stats (UUID VARCHAR(64) INT,WIN INT, PLAYED INT,TIME_PLAYED INT, LOOSE INT, KILLS INT, DEATH INT, COINS INT, ELYCOINS INT, TIME_PLAYED INT)");
- System.out.println("FAIT");
- }catch (SQLException e) {
- try {
- close();
- connectTo();
- }catch (ClassNotFoundException | SQLException err) {
- err.printStackTrace();
- }
- }
- }
- public void addPlayedGame(UUID playerID){
- try {
- statement().executeUpdate("update Skywars_stats set PLAYED=PLAYED+"+1+" where UUID='"+playerID+"'");
- } catch (SQLException e) {
- try{
- close();
- connectTo();
- }
- catch(ClassNotFoundException| SQLException e1){
- e1.printStackTrace();
- }
- }
- }
- public void addKill(UUID playerID){
- try {
- statement().executeUpdate("update Skywars_stats set KILLS=KILLS+"+1+" where UUID='"+playerID+"'");
- } catch (SQLException e) {
- try{
- close();
- connectTo();
- }
- catch(ClassNotFoundException| SQLException e1){
- e1.printStackTrace();
- }
- }
- }
- public void addDeath(UUID playerID){
- try {
- statement().executeUpdate("update Skywars_stats set DEATH=DEATH+"+1+" where UUID='"+playerID+"'");
- } catch (SQLException e) {
- try{
- close();
- connectTo();
- }
- catch(ClassNotFoundException| SQLException e1){
- e1.printStackTrace();
- }
- }
- }
- public void addCoins(UUID playerID){
- try {
- for(Player pls : Bukkit.getOnlinePlayers()){
- statement().executeUpdate("update Skywars_stats set COINS=COINS+"+PlayerUtil.getEarnedCoins(pls.getUniqueId())+" where UUID='"+playerID+"'");
- }
- } catch (SQLException e) {
- try{
- close();
- connectTo();
- }
- catch(ClassNotFoundException| SQLException e1){
- e1.printStackTrace();
- }
- }
- }
- public void addElyCoins(UUID playerID){
- try {
- for(Player pls : Bukkit.getOnlinePlayers()){
- statement().executeUpdate("update Skywars_stats set ELYCOINS=ELYCOINS+"+PlayerUtil.getEarnedElyCoins(pls.getUniqueId())+" where UUID='"+playerID+"'");
- }
- } catch (SQLException e) {
- try{
- close();
- connectTo();
- }
- catch(ClassNotFoundException| SQLException e1){
- e1.printStackTrace();
- }
- }
- }
- public void addWin(UUID playerID){
- try {
- statement().executeUpdate("update Skywars_stats set WIN=WIN+"+1+" where UUID='"+playerID+"'");
- } catch (SQLException e) {
- try{
- close();
- connectTo();
- }
- catch(ClassNotFoundException| SQLException e1){
- e1.printStackTrace();
- }
- }
- }
- public void addLoose(UUID playerID){
- try {
- statement().executeUpdate("update Skywars_stats set LOOSE=LOOSE+"+1+" where UUID='"+playerID+"'");
- } catch (SQLException e) {
- try{
- close();
- connectTo();
- }
- catch(ClassNotFoundException| SQLException e1){
- e1.printStackTrace();
- }
- }
- }
- public int getKill(UUID playerID){
- int KILLS=0;
- try {
- result = statement().executeQuery("select KILLS from Skywars_stats where UUID='"+playerID+"'");
- while(result.next()){
- KILLS = result.getInt("KILLS");
- }
- } catch (SQLException e) {
- try {
- close();
- connectTo();
- }catch (ClassNotFoundException | SQLException err) {
- err.printStackTrace();
- }
- }
- return KILLS;
- }
- public int getWin(UUID playerID){
- int WIN=0;
- try {
- result = statement().executeQuery("select WIN from Skywars_stats where UUID='"+playerID+"'");
- while(result.next()){
- WIN = result.getInt("WIN");
- }
- } catch (SQLException e) {
- try {
- close();
- connectTo();
- }catch (ClassNotFoundException | SQLException err) {
- err.printStackTrace();
- }
- }
- return WIN;
- }
- public int getDeath(UUID playerID){
- int DEATH=0;
- try {
- result = statement().executeQuery("select DEATH from Skywars_stats where UUID='"+playerID+"'");
- while(result.next()){
- DEATH = result.getInt("WIN");
- }
- } catch (SQLException e) {
- try {
- close();
- connectTo();
- }catch (ClassNotFoundException | SQLException err) {
- err.printStackTrace();
- }
- }
- return DEATH;
- }
- public void sendTimePlayed(HashMap<UUID,Integer> map ){
- for(UUID id : map.keySet()){
- try {
- statement().executeUpdate("update Skywars_stats set TIME_PLAYED=TIME_PLAYED+"+map.get(id)+" where UUID='"+id+"'");
- } catch (SQLException e) {
- try{
- close();
- connectTo();
- }
- catch(ClassNotFoundException| SQLException e1){
- e1.printStackTrace();
- }
- }
- }
- }
- public void createStats(UUID playerID){
- try {
- statement().execute("insert into Skywars_stats values('"+ playerID.toString()+"',0,0,0,0,0,0,0)");
- } catch (SQLException e) {
- try{
- close();
- connectTo();
- }
- catch(ClassNotFoundException| SQLException e1){
- e1.printStackTrace();
- }
- }
- }
- public boolean hasAccount(UUID playerID){
- boolean exist=false;
- try {
- result = statement.executeQuery("select TIME_PLAYED from Skywars_stats where UUID='"+playerID+"'");
- if(result.next()){
- exist=true;
- }
- } catch (SQLException e) {
- try{
- close();
- connectTo();
- }
- catch(ClassNotFoundException| SQLException e1){
- e1.printStackTrace();
- }
- }
- return exist;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement