ph1823

Code Hub

Jun 20th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.84 KB | None | 0 0
  1. package hub.ph1823.fr;
  2.  
  3.  
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.Hashtable;
  9.  
  10.  
  11. import org.bukkit.inventory.ItemStack;
  12. import org.bukkit.plugin.java.JavaPlugin;
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. public class Main extends JavaPlugin{
  20.      private String url =
  21.         private String user = "serv
  22.         private String passwd = "
  23.         public java.sql.Connection conn;
  24.         public static int easycoin;
  25.         public static int easyeuro;
  26.     public static Hashtable<Integer, ItemStack> ItemsCompass = null;
  27.     public static Hashtable<Integer, ItemStack> ItemsShop = null;
  28.     public void onEnable() {
  29.     getServer().getPluginManager().registerEvents(new EventListener(this), this);
  30.     CustomEntityType.registerEntities();
  31.     getCommand("set").setExecutor(new Commanset(this));
  32.     getCommand("setitem").setExecutor(new CommandSetItem(this));
  33.     getCommand("settitle").setExecutor(new CommandSetTitle(this));
  34.     ItemCompass I = new ItemCompass(this);
  35.     ItemsCompass = I.BuildItems();
  36.    
  37.     ItemShop I1 = new ItemShop(this);
  38.     ItemsShop = I1.BuildItems();
  39.     connect();
  40.     CustomEntityType.registerEntities();
  41.    
  42.     }
  43.     public void onDisable() {
  44.         CustomEntityType.unregisterEntities();
  45.         try { //using a try catch to catch connection errors (like wrong sql password...)
  46.             if(conn!=null && conn.isClosed()){ //checking if connection isn't null to
  47.             //avoid recieving a nullpointer
  48.                     conn.close(); //closing the connection field variable.
  49.             }
  50.     }catch(Exception e){
  51.                     e.printStackTrace();
  52.              
  53.             }
  54.     }
  55.     public void connect(){
  56.          
  57.         try {
  58.             Class.forName("com.mysql.jdbc.Driver");
  59.         } catch (ClassNotFoundException e1) {
  60.             e1.printStackTrace();
  61.         }
  62.  
  63.         try {
  64.  
  65.             conn = DriverManager.getConnection(url, user, passwd);
  66.             System.out.println("Connexion établie!");
  67.         } catch (SQLException e) {
  68.             e.printStackTrace();
  69.         }
  70.  
  71.  
  72.     }
  73.     public void donne(String pseudo) {
  74.         try { //using a try catch to catch connection errors (like wrong sql password...)
  75.             if (this.conn == null || conn.isClosed()) { // on vérivie si la connection est active
  76.                 connect(); // si ce n'est pas le cas, on se reconnecte
  77.             }
  78.            
  79.     }catch(Exception e){
  80.                     e.printStackTrace();
  81.              
  82.             }
  83.         try {
  84.            
  85.             Statement state = conn.createStatement();
  86.             ResultSet result = state.executeQuery("SELECT * FROM easy_membre WHERE pseudo = '" + pseudo + "'");
  87.          
  88.            while(result.next())
  89.            {
  90.                easycoin = result.getInt(2);
  91.                easyeuro = result.getInt(3);    
  92.                state.close();
  93.              
  94.            }
  95.            
  96.  
  97.         } catch (SQLException e) {
  98.      
  99.             e.printStackTrace();
  100.         }
  101.     }
  102.     public void Test(String pseudo) {
  103.         try { //using a try catch to catch connection errors (like wrong sql password...)
  104.             if (this.conn == null || conn.isClosed()) { // on vérivie si la connection est active
  105.                 connect(); // si ce n'est pas le cas, on se reconnecte
  106.             }
  107.            
  108.     }catch(Exception e){
  109.                     e.printStackTrace();
  110.              
  111.             }
  112.         try {
  113.  
  114.             Statement state = conn.createStatement();
  115.             ResultSet result = state.executeQuery("SELECT * FROM easy_membre WHERE pseudo = '" + pseudo + "'");
  116.            
  117.             if (!result.isBeforeFirst()) {
  118.              
  119.        
  120.               state.executeUpdate("INSERT INTO easy_membre (pseudo, easycoin, easyeuro) VALUES ('"+pseudo+"',0,0)");
  121.              
  122.               state.close();
  123.  
  124.             }
  125.            // result.close();
  126.         } catch (SQLException e) {
  127.      
  128.             e.printStackTrace();
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment