Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.massivecraft.factions;
- 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;
- import java.util.Map.Entry;
- import org.bukkit.Bukkit;
- public class SqlConnection
- {
- static List<String> list = new ArrayList<>();
- private static Connection connection;
- private String urlbase,host,base,user,pass;
- public SqlConnection(String urlbase, String host, String base, String user, String pass)
- {
- this.urlbase = urlbase;
- this.host = host;
- this.base = base;
- this.user = user;
- this.pass = pass;
- }
- public void connection()
- {
- try {
- connection = DriverManager.getConnection(urlbase + host + "/" + base, user, pass);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void disconnect()
- {
- if(isConnected())
- {
- try {
- connection.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public static boolean isConnected()
- {
- return connection != null;
- }
- public static void updateBase()
- {
- if(isConnected())
- {
- try {
- PreparedStatement q2 = connection.prepareStatement("SELECT nom FROM fclassement");
- ResultSet res = q2.executeQuery();
- while(res.next())
- {
- list.add(res.getString(1));
- }
- q2.close();
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- for(Entry<String, Object> s : FClassementSort.section.getValues(false).entrySet())
- {
- if(s.getKey().equalsIgnoreCase("�eNature") || s.getKey().equalsIgnoreCase("WarZone") || s.getKey().equalsIgnoreCase("SafeZone") || s.getKey().equalsIgnoreCase("Staff") || s.getKey().equalsIgnoreCase("Staff2"))
- continue;
- if(!factionExists(s.getKey()))
- {
- if(Factions.i.getByTag(s.getKey()) != null)
- {
- try {
- PreparedStatement q = connection.prepareStatement("INSERT INTO fclassement(nom,joueurs,claims,kills,morts,money,totem,koth,lastsurvive,points) VALUES (?,?,?,?,?,?,?,?,?,?)");
- q.setString(1, s.getKey());
- q.setInt(2, Factions.i.getByTag(s.getKey()).getFPlayers().size());
- q.setInt(3, Factions.i.getByTag(s.getKey()).getLandRounded());
- int kills = 0;
- for(FPlayer fp : Factions.i.getByTag(s.getKey()).getFPlayers())
- {
- kills += P.p.classementConfig.getInt("classement-faction-joueurs."+fp.getName()+".kill");
- }
- q.setInt(4, kills);
- int morts = 0;
- for(FPlayer fp : Factions.i.getByTag(s.getKey()).getFPlayers())
- {
- morts += P.p.classementConfig.getInt("classement-faction-joueurs."+fp.getName()+".death");
- }
- q.setInt(5, morts);
- int money = 0;
- for(FPlayer fp : Factions.i.getByTag(s.getKey()).getFPlayers())
- {
- money += P.economy.getBalance(fp.getName());
- }
- q.setInt(6, money);
- q.setInt(7, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".event.totem"));
- q.setInt(8, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".event.koth"));
- q.setInt(9, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".event.lastsurvive"));
- q.setInt(10, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".point"));
- q.execute();
- q.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- else
- {
- P.p.classementConfig.set("classement-faction-factions."+s.getKey(), null);
- FClassementSort.section.set(s.getKey(), null);
- P.p.classementConfig.set("classement-faction-factions-entry."+s.getKey(), null);
- P.p.saveLaConfig();
- }
- }
- else
- {
- if(Factions.i.getByTag(s.getKey()) != null)
- {
- try {
- PreparedStatement q = connection.prepareStatement("UPDATE fclassement SET joueurs = ?, claims = ?, kills = ?, morts = ?, money = ?, totem = ?, koth = ?, lastsurvive = ?, points = ? WHERE nom = ?");
- q.setInt(1, Factions.i.getByTag(s.getKey()).getFPlayers().size());
- q.setInt(2, Factions.i.getByTag(s.getKey()).getLandRounded());
- int kills = 0;
- for(FPlayer fp : Factions.i.getByTag(s.getKey()).getFPlayers())
- {
- kills += P.p.classementConfig.getInt("classement-faction-joueurs."+fp.getName()+".kill");
- }
- q.setInt(3, kills);
- int morts = 0;
- for(FPlayer fp : Factions.i.getByTag(s.getKey()).getFPlayers())
- {
- morts += P.p.classementConfig.getInt("classement-faction-joueurs."+fp.getName()+".death");
- }
- q.setInt(4, morts);
- int money = 0;
- for(FPlayer fp : Factions.i.getByTag(s.getKey()).getFPlayers())
- {
- money += P.p.economy.getBalance(fp.getName());
- }
- q.setInt(5, money);
- q.setInt(6, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".event.totem"));
- q.setInt(7, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".event.koth"));
- q.setInt(8, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".event.lastsurvive"));
- q.setInt(9, P.p.classementConfig.getInt("classement-faction-factions."+s.getKey()+".point"));
- q.setString(10, s.getKey());
- q.executeUpdate();
- q.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- else
- {
- try {
- PreparedStatement q = connection.prepareStatement("DELETE FROM fclassement WHERE nom = ?");
- q.setString(1, s.getKey());
- q.executeUpdate();
- q.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- P.p.classementConfig.set("classement-faction-factions."+s.getKey(), null);
- FClassementSort.section.set(s.getKey(), null);
- P.p.classementConfig.set("classement-faction-factions-entry."+s.getKey(), null);
- P.p.saveLaConfig();
- }
- }
- }
- for(String s : list)
- {
- if(!FClassementSort.section.getValues(false).containsKey(s))
- {
- try {
- PreparedStatement q = connection.prepareStatement("DELETE FROM fclassement WHERE nom = ?");
- q.setString(1, s);
- q.executeUpdate();
- q.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
- public static boolean factionExists(String f)
- {
- try {
- PreparedStatement q = connection.prepareStatement("SELECT nom FROM fclassement WHERE nom = ?");
- q.setString(1, f);
- ResultSet res = q.executeQuery();
- boolean factionExists = res.next();
- q.close();
- return factionExists;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement