daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Apr 27th, 2016 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. public class MySQL {
  9.  
  10.  
  11.     private final String host, database, user, password;
  12.  
  13.     private Connection con;
  14.  
  15.     public MySQL(String host, String database, String user, String password) {
  16.         this.host= host;
  17.         this.database= database;
  18.         this.user= user;
  19.         this.password= password;
  20.  
  21.         connect();
  22.     }
  23.  
  24.     public void connect() {
  25.         try {
  26.             con = DriverManager.getConnection("jdbc:mysql://" + host+ ":3306/" + database+ "?autoReconnect=true", user, password);
  27.             System.out.println("[Coins MySQL] The connection to MySQL is made!");
  28.         } catch (SQLException e) {
  29.             System.out.println("[Coins MySQL] The connection to MySQL couldn't be made! reason: " + e.getMessage());
  30.         }
  31.     }
  32.  
  33.     public void close() {
  34.         try {
  35.             if (con != null) {
  36.                 con.close();
  37.                 System.out.println("[MySQL] The connection to MySQL is ended successfully!");
  38.             }
  39.         } catch (SQLException e) {
  40.  
  41.             e.printStackTrace();
  42.         }
  43.     }
  44.  
  45.     public PreparedStatement prepareStatement(String qry) {
  46.         PreparedStatement st = null;
  47.         try {
  48.             st = con.prepareStatement(qry);
  49.         } catch (SQLException e) {
  50.             e.printStackTrace();
  51.         }
  52.         return st;
  53.     }
  54.  
  55.     public void update(PreparedStatement statement) {
  56.         try {
  57.             statement.executeUpdate();    
  58.         } catch (SQLException e) {
  59.             connect();
  60.             e.printStackTrace();
  61.         }finally{
  62.             try {
  63.                 statement.close();
  64.             } catch (SQLException e) {
  65.                 e.printStackTrace();
  66.             }
  67.         }
  68.     }
  69.  
  70.     public boolean hasConnection() {
  71.         return con != null;
  72.     }
  73.  
  74.     public ResultSet query(PreparedStatement statement) {
  75.         try {
  76.             return statement.executeQuery();
  77.         } catch (SQLException e) {
  78.             connect();
  79.             e.printStackTrace();
  80.         }
  81.         return null;
  82.     }
  83. }
  84.  
  85. import java.sql.PreparedStatement;
  86. import java.sql.SQLException;
  87.  
  88. import org.bukkit.Bukkit;
  89. import org.bukkit.event.EventHandler;
  90. import org.bukkit.event.EventPriority;
  91. import org.bukkit.event.Listener;
  92.  
  93. import com.massivecraft.factions.event.FPlayerLeaveEvent;
  94. import com.massivecraft.factions.event.FactionCreateEvent;
  95. import com.massivecraft.factions.event.FactionDisbandEvent;
  96. import com.massivecraft.factions.event.FactionRenameEvent;
  97.  
  98. import fr.forsties08.kryptonia.Kryptonia;
  99.  
  100. public class Factions implements Listener {
  101.  
  102.     Kryptonia instance;
  103.  
  104.     public Factions(Kryptonia main) {
  105.         instance = main;
  106.     }
  107.  
  108.     @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
  109.     public void whenPlayerCreateFaction(FactionCreateEvent event) {
  110.         try {
  111.             PreparedStatement statement = instance.database.prepareStatement("INSERT INTO factions(name, admin) VALUES(?, ?)");
  112.             statement.setString(1, event.getFactionTag());
  113.             statement.setString(2, event.getFPlayer().getName());
  114.             statement.executeUpdate();
  115.         } catch (SQLException e) {
  116.             e.printStackTrace();
  117.         }
  118.     }
  119.    
  120.     @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
  121.     public void whenPlayerRenameFaction(FactionRenameEvent event) {
  122.         try {
  123.             PreparedStatement statement = instance.database.prepareStatement("UPDATE factions SET name=? WHERE name=?");
  124.             statement.setString(1, event.getFactionTag());
  125.             statement.setString(2, event.getFaction().getTag());
  126.             statement.executeUpdate();
  127.         } catch (SQLException e) {
  128.             e.printStackTrace();
  129.         }
  130.     }
  131.    
  132.     @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
  133.     public void whenPlayerLeaveFaction(FPlayerLeaveEvent event) {
  134.         if(event.getFaction().getFPlayers().size() == 1) {
  135.             try {
  136.                 PreparedStatement statement = instance.database.prepareStatement("DELETE FROM factions WHERE name=?");
  137.                 Bukkit.getLogger().info(event.getFaction().getTag());
  138.                 statement.setString(1, event.getFaction().getTag());
  139.                 statement.executeUpdate();
  140.             } catch (SQLException e) {
  141.                 e.printStackTrace();
  142.             }
  143.         }else {
  144.             try {
  145.                 PreparedStatement statement = instance.database.prepareStatement("UPDATE factions SET members=? WHERE name=?");
  146.                 statement.setString(1, event.getFaction().getFPlayers().toString());
  147.                 statement.setString(2, event.getFaction().getTag());
  148.                 statement.executeUpdate();
  149.             } catch (SQLException e) {
  150.                 e.printStackTrace();
  151.             }
  152.         }
  153.     }
  154.    
  155.     @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
  156.     public void whenPlayerDisbandFaction(FactionDisbandEvent event) {
  157.         try {
  158.             PreparedStatement statement = instance.database.prepareStatement("DELETE FROM factions WHERE name=?");
  159.             statement.setString(1, event.getFaction().getTag());
  160.             statement.executeUpdate();
  161.         } catch (SQLException e) {
  162.             e.printStackTrace();
  163.         }
  164.     }
  165.  
  166. }
  167.  
  168.  
  169.     public void onEnable() {
  170.         getServer().getPluginManager().registerEvents(new Factions(this), this);
  171.         database = new MySQL("localhost", "kryptonia", "root", "");
  172.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top