Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.bukkit.forsaken.exampleplugin;
- import java.io.File;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.bukkit.Server;
- import org.bukkit.plugin.Plugin;
- import org.bukkit.plugin.PluginDescriptionFile;
- import org.bukkit.plugin.PluginLoader;
- import org.bukkit.plugin.java.JavaPlugin;
- import com.bukkit.forsaken.essencials.ESDatabaseConditionSet.InvalidOrderException;
- import com.bukkit.forsaken.essencials.ESDatabaseDelete;
- import com.bukkit.forsaken.essencials.ESDatabaseInsert;
- import com.bukkit.forsaken.essencials.ESDatabaseRawSQL;
- import com.bukkit.forsaken.essencials.ESDatabaseSelect;
- import com.bukkit.forsaken.essencials.ESDatabaseUpdate;
- import com.bukkit.forsaken.essencials.Essencials;
- /**
- * An example plugin using most parts of the database layer.
- * Notice the interface implementation!
- *
- * Database structure used for testing:
- * CREATE TABLE `user`
- * ( `name` VARCHAR(50) NOT NULL, `money` MEDIUMINT UNSIGNED NOT NULL, UNIQUE INDEX `name` (`name`) )
- * COLLATE='utf8_general_ci' ENGINE=MyISAM ROW_FORMAT=DEFAULT;
- *
- * @author Forsaken
- *
- */
- public class ExamplePlugin extends JavaPlugin {
- public ExamplePlugin(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) {
- super(pluginLoader, instance, desc, folder, plugin, cLoader);
- // ...
- }
- public void onDisable() {
- // ...
- System.out.println("[ExamplePlugin] Disabled.");
- }
- public void onEnable() {
- // ...
- System.out.println("[ExamplePlugin] Enabled.");
- executeSomeQuerys();
- }
- /**
- * Call this function to execute the example querys
- */
- public void executeSomeQuerys() {
- Plugin pluginGeneric = getServer().getPluginManager().getPlugin("ForsakenEssencials");
- if (pluginGeneric != null) {
- // Get the instance of the plugin
- Essencials pluginEssencials = (Essencials) pluginGeneric;
- /**
- * A player name for our examples
- */
- String playerName = "someone";
- {
- /*
- * Insert example
- *
- * These lines will insert a row into table "user" with the field "name" set to "someone" and the field
- * "money" to 5000. The field "name" is a unique, if there is already a user named "someone" it will update
- * all fields except "money".
- *
- * INSERT INTO `user` (`name`, `money`) VALUES ('someone', 5000) ON DUPLICATE KEY UPDATE `money`=5000
- */
- // Create a query for table "user" and set the insert/update fields
- ESDatabaseInsert insertQuery = pluginEssencials.createInsertQuery("user").addField("name", playerName);
- insertQuery.addField("money", 5000).setUpdateOnDuplicateKey("name").executeUpdateNow();
- // Set a anonymous inner type callback for this query, the second parameter can be used to identify the query.
- insertQuery.setResultCallback(this, "MY_PLAYER_QUERY");
- // Start a thread connecting to the database and executing the query
- insertQuery.start();
- // Print out the query string
- // INSERT INTO `user` (`name`, `money`) VALUES ('someone', 5000) ON DUPLICATE KEY UPDATE `money`=5000
- System.out.println(insertQuery);
- try {
- // Wait until query is complete
- insertQuery.join();
- } catch (InterruptedException e) {
- // Most likely the plugin/server is stopping
- return;
- }
- }
- {
- /*
- * Update example
- *
- * Set the value for field "money" to 10000 for the previously inserted/updated user "someone"
- *
- * UPDATE `user` SET `money`=(money+5000) WHERE (`name` LIKE 'someone')
- */
- try {
- ESDatabaseUpdate updateQuery = pluginEssencials.createUpdateQuery("user");
- // Set the value for field "money" to the SQL-Expression "(money+5000)" (that will increase the money by 5000)
- // for the user "someone"
- updateQuery.addField("money", new ESDatabaseRawSQL("(money+5000)")).addCondition("name", "LIKE", playerName);
- // Start query thread
- updateQuery.setResultCallback(this, "MY_PLAYER_UPDATE").start();
- // Print out the query string
- // UPDATE `user` SET `money`=(money+5000) WHERE (`name` LIKE 'someone')
- System.out.println(updateQuery);
- try {
- // Wait until query is complete
- updateQuery.join();
- } catch (InterruptedException e) {
- // Most likely the plugin/server is stopping
- return;
- }
- } catch (InvalidOrderException e) {
- // This is trown if the condition syntax is invalid
- System.out.println(e.getMessage());
- }
- }
- {
- /*
- * Select example
- *
- * Select the fields "name" and "money" of all rows in table "user".
- *
- * SELECT `name`, `money` FROM `user`
- */
- ESDatabaseSelect selectQuery = pluginEssencials.createSelectQuery("user", "name", "money");
- // Start query thread
- selectQuery.setResultCallback(this, "MY_PLAYER_SELECT").start();
- // Print out the query string
- // SELECT `name`, `money` FROM `user`
- System.out.println(selectQuery);
- try {
- // Wait until query is complete
- selectQuery.join();
- } catch (InterruptedException e) {
- // Most likely the plugin/server is stopping
- return;
- }
- }
- {
- /*
- * Delete example
- *
- * Delete the user named "someone" again.
- *
- * DELETE FROM `user` WHERE (`name` LIKE 'someone')
- */
- try {
- final String deletePlayer = playerName;
- ESDatabaseDelete deleteQuery = pluginEssencials.createDeleteQuery("user").addCondition("name","LIKE",deletePlayer);
- // Start query thread
- deleteQuery.setResultCallback(this, "MY_PLAYER_DELETE").start();
- // Print out the query string
- // DELETE FROM `user` WHERE (`name` LIKE 'someone')
- System.out.println(deleteQuery);
- try {
- // Wait until query is complete
- deleteQuery.join();
- } catch (InterruptedException e) {
- // Most likely the plugin/server is stopping
- return;
- }
- } catch (InvalidOrderException e1) {
- // This is trown if the condition syntax is invalid
- e1.printStackTrace();
- }
- }
- }
- }
- /**
- * Called when a insert query has finished
- *
- * @param generatedKeys
- * @param customVar
- */
- public void onInsertResult(ResultSet generatedKeys, Object customVar) {
- // The first query inserted a new row, this means the user was not known.
- System.out.println("Created new entry or updated existing one.");
- }
- /**
- * Called when an update (or an insert ... on duplicate key update) query has finished.
- *
- * @param affectedRows
- * @param customVar
- */
- public void onUpdateResult(Integer affectedRows, Object customVar) {
- // Second example (update)
- System.out.println("User was updated!");
- }
- /**
- * Called when a select query has finished
- *
- * @param sqlResult
- * @param customVar
- */
- public void onSelectResult(ResultSet sqlResult, Object customVar) {
- // Third example (select)
- System.out.println("Selected all users!");
- // List all selected users
- try {
- if (sqlResult.first()) {
- do {
- System.out.println(sqlResult.getString("name") + " has "+sqlResult.getInt("money")+" money!");
- } while (sqlResult.next());
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * Called when a delete query has finished
- *
- * @param affectedRows
- * @param customVar
- */
- public void onDeleteResult(Integer affectedRows, Object customVar) {
- // Fourth example (delete)
- System.out.println("Deleted user!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement