Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.59 KB | None | 0 0
  1. package com.bukkit.forsaken.exampleplugin;
  2.  
  3. import java.io.File;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6.  
  7. import org.bukkit.Server;
  8. import org.bukkit.plugin.Plugin;
  9. import org.bukkit.plugin.PluginDescriptionFile;
  10. import org.bukkit.plugin.PluginLoader;
  11. import org.bukkit.plugin.java.JavaPlugin;
  12.  
  13. import com.bukkit.forsaken.essencials.ESDatabaseConditionSet.InvalidOrderException;
  14. import com.bukkit.forsaken.essencials.ESDatabaseDelete;
  15. import com.bukkit.forsaken.essencials.ESDatabaseInsert;
  16. import com.bukkit.forsaken.essencials.ESDatabaseRawSQL;
  17. import com.bukkit.forsaken.essencials.ESDatabaseSelect;
  18. import com.bukkit.forsaken.essencials.ESDatabaseUpdate;
  19. import com.bukkit.forsaken.essencials.Essencials;
  20. /**
  21.  * An example plugin using most parts of the database layer.
  22.  * Notice the interface implementation!
  23.  *
  24.  * Database structure used for testing:
  25.  * CREATE TABLE `user`
  26.  *      (  `name` VARCHAR(50) NOT NULL,  `money` MEDIUMINT UNSIGNED NOT NULL,  UNIQUE INDEX `name` (`name`) )
  27.  * COLLATE='utf8_general_ci' ENGINE=MyISAM ROW_FORMAT=DEFAULT;
  28.  *
  29.  * @author Forsaken
  30.  *
  31.  */
  32. public class ExamplePlugin extends JavaPlugin {
  33.  
  34.     public ExamplePlugin(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) {
  35.         super(pluginLoader, instance, desc, folder, plugin, cLoader);
  36.         // ...
  37.     }
  38.  
  39.     public void onDisable() {
  40.         // ...
  41.         System.out.println("[ExamplePlugin] Disabled.");
  42.     }
  43.    
  44.     public void onEnable() {
  45.         // ...
  46.         System.out.println("[ExamplePlugin] Enabled.");
  47.         executeSomeQuerys();
  48.     }
  49.  
  50.     /**
  51.      * Call this function to execute the example querys
  52.      */
  53.     public void executeSomeQuerys() {
  54.         Plugin pluginGeneric = getServer().getPluginManager().getPlugin("ForsakenEssencials");
  55.         if (pluginGeneric != null) {
  56.             // Get the instance of the plugin
  57.             Essencials pluginEssencials = (Essencials) pluginGeneric;
  58.            
  59.             /**
  60.              * A player name for our examples
  61.              */
  62.             String playerName = "someone";
  63.            
  64.             {
  65.                 /*
  66.                  * Insert example
  67.                  *
  68.                  * These lines will insert a row into table "user" with the field "name" set to "someone" and the field
  69.                  * "money" to 5000. The field "name" is a unique, if there is already a user named "someone" it will update
  70.                  * all fields except "money".
  71.                  *
  72.                  * INSERT INTO `user` (`name`, `money`) VALUES ('someone', 5000)  ON DUPLICATE KEY UPDATE `money`=5000
  73.                  */
  74.                
  75.                 // Create a query for table "user" and set the insert/update fields
  76.                 ESDatabaseInsert insertQuery = pluginEssencials.createInsertQuery("user").addField("name", playerName);
  77.                 insertQuery.addField("money", 5000).setUpdateOnDuplicateKey("name").executeUpdateNow();
  78.                 // Set a anonymous inner type callback for this query, the second parameter can be used to identify the query.
  79.                 insertQuery.setResultCallback(this, "MY_PLAYER_QUERY");
  80.                 // Start a thread connecting to the database and executing the query
  81.                 insertQuery.start();
  82.                 // Print out the query string
  83.                 // INSERT INTO `user` (`name`, `money`) VALUES ('someone', 5000)  ON DUPLICATE KEY UPDATE `money`=5000
  84.                 System.out.println(insertQuery);
  85.                
  86.                 try {
  87.                     // Wait until query is complete
  88.                     insertQuery.join();
  89.                 } catch (InterruptedException e) {
  90.                     // Most likely the plugin/server is stopping
  91.                     return;
  92.                 }
  93.             }
  94.            
  95.            
  96.            
  97.             {
  98.                 /*
  99.                  * Update example
  100.                  *
  101.                  * Set the value for field "money" to 10000 for the previously inserted/updated user "someone"
  102.                  *
  103.                  * UPDATE `user` SET `money`=(money+5000) WHERE (`name` LIKE 'someone')
  104.                  */
  105.                 try {
  106.                     ESDatabaseUpdate updateQuery = pluginEssencials.createUpdateQuery("user");
  107.                     // Set the value for field "money" to the SQL-Expression "(money+5000)" (that will increase the money by 5000)
  108.                     // for the user "someone"
  109.                     updateQuery.addField("money", new ESDatabaseRawSQL("(money+5000)")).addCondition("name", "LIKE", playerName);
  110.                     // Start query thread
  111.                     updateQuery.setResultCallback(this, "MY_PLAYER_UPDATE").start();
  112.                     // Print out the query string
  113.                     // UPDATE `user` SET `money`=(money+5000) WHERE (`name` LIKE 'someone')
  114.                     System.out.println(updateQuery);
  115.                    
  116.                     try {
  117.                         // Wait until query is complete
  118.                         updateQuery.join();
  119.                     } catch (InterruptedException e) {
  120.                         // Most likely the plugin/server is stopping
  121.                         return;
  122.                     }
  123.                 } catch (InvalidOrderException e) {
  124.                     // This is trown if the condition syntax is invalid
  125.                     System.out.println(e.getMessage());
  126.                 }
  127.             }
  128.            
  129.            
  130.            
  131.            
  132.             {
  133.                 /*
  134.                  * Select example
  135.                  *
  136.                  * Select the fields "name" and "money" of all rows in table "user".
  137.                  *
  138.                  * SELECT `name`, `money` FROM `user`
  139.                  */
  140.                 ESDatabaseSelect selectQuery = pluginEssencials.createSelectQuery("user", "name", "money");
  141.                 // Start query thread
  142.                 selectQuery.setResultCallback(this, "MY_PLAYER_SELECT").start();
  143.                 // Print out the query string
  144.                 // SELECT `name`, `money` FROM `user`
  145.                 System.out.println(selectQuery);
  146.                
  147.                 try {
  148.                     // Wait until query is complete
  149.                     selectQuery.join();
  150.                 } catch (InterruptedException e) {
  151.                     // Most likely the plugin/server is stopping
  152.                     return;
  153.                 }
  154.             }
  155.            
  156.            
  157.            
  158.            
  159.             {
  160.                 /*
  161.                  * Delete example
  162.                  *
  163.                  * Delete the user named "someone" again.
  164.                  *
  165.                  * DELETE FROM `user` WHERE (`name` LIKE 'someone')
  166.                  */
  167.                 try {
  168.                     final String deletePlayer = playerName;
  169.                     ESDatabaseDelete deleteQuery = pluginEssencials.createDeleteQuery("user").addCondition("name","LIKE",deletePlayer);
  170.                     // Start query thread
  171.                     deleteQuery.setResultCallback(this, "MY_PLAYER_DELETE").start();
  172.                     // Print out the query string
  173.                     // DELETE FROM `user` WHERE (`name` LIKE 'someone')
  174.                     System.out.println(deleteQuery);
  175.                    
  176.                     try {
  177.                         // Wait until query is complete
  178.                         deleteQuery.join();
  179.                     } catch (InterruptedException e) {
  180.                         // Most likely the plugin/server is stopping
  181.                         return;
  182.                     }
  183.                 } catch (InvalidOrderException e1) {
  184.                     // This is trown if the condition syntax is invalid
  185.                     e1.printStackTrace();
  186.                 }
  187.             }
  188.         }
  189.     }
  190.  
  191.     /**
  192.      * Called when a insert query has finished
  193.      *
  194.      * @param generatedKeys
  195.      * @param customVar
  196.      */
  197.     public void onInsertResult(ResultSet generatedKeys, Object customVar) {
  198.         // The first query inserted a new row, this means the user was not known.
  199.         System.out.println("Created new entry or updated existing one.");
  200.     }
  201.    
  202.     /**
  203.      * Called when an update (or an insert ... on duplicate key update) query has finished.
  204.      *
  205.      * @param affectedRows
  206.      * @param customVar
  207.      */
  208.     public void onUpdateResult(Integer affectedRows, Object customVar) {
  209.         // Second example (update)
  210.         System.out.println("User was updated!");
  211.     }
  212.    
  213.     /**
  214.      * Called when a select query has finished
  215.      *
  216.      * @param sqlResult
  217.      * @param customVar
  218.      */
  219.     public void onSelectResult(ResultSet sqlResult, Object customVar) {
  220.         // Third example (select)
  221.         System.out.println("Selected all users!");
  222.         // List all selected users
  223.         try {
  224.             if (sqlResult.first()) {
  225.                 do {
  226.                     System.out.println(sqlResult.getString("name") + " has "+sqlResult.getInt("money")+" money!");
  227.                 } while (sqlResult.next());
  228.             }
  229.         } catch (SQLException e) {
  230.             // TODO Auto-generated catch block
  231.             e.printStackTrace();
  232.         }
  233.     }
  234.    
  235.     /**
  236.      * Called when a delete query has finished
  237.      *
  238.      * @param affectedRows
  239.      * @param customVar
  240.      */
  241.     public void onDeleteResult(Integer affectedRows, Object customVar) {
  242.         // Fourth example (delete)
  243.         System.out.println("Deleted user!");
  244.     }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement