Advertisement
Guest User

DbHandlerTutorial

a guest
May 15th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. //Change to your package location
  2. package ;
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.plugin.Plugin;
  12.  
  13. //Change to the location of your MySqlCallback Interface
  14. import ;
  15.  
  16. public class DbHandlerTutorial {
  17.    
  18.     /**
  19.      * This method is in charge of grabbing queries and sending the result to the MySqlCallback
  20.      * @param plugin The instance of your plugin
  21.      * @param query The MySql query
  22.      * @param callback The MySql Callback
  23.      */
  24.     public static void executeQuery(Plugin plugin, String query, MySqlCallback callback) {
  25.        
  26.         //Schedules a new Asynchronous task
  27.         Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
  28.             @Override
  29.             public void run() {
  30.                 try {
  31.                     //Loads the MySql JDBC driver
  32.                     Class.forName("com.mysql.jdbc.Driver");
  33.                     //Create a connection to your mysql server
  34.                     Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/database?user=root&password=password");
  35.                     //Creates a statement for our connection
  36.                     Statement stmt = conn.createStatement();
  37.                     //Executes the query and grabs the data from the MySql Server
  38.                     ResultSet rs = stmt.executeQuery(query);
  39.                     //Creates another task but this time throwing us back insync with the minecraft server
  40.                     Bukkit.getScheduler().runTask(plugin, new Runnable() {
  41.                         @Override
  42.                         public void run() {
  43.                             //passes the MySql data to out MySqlCallback
  44.                             callback.onQueryDone(rs);
  45.                         }
  46.                     });
  47.                 } catch (ClassNotFoundException e) {
  48.                     e.printStackTrace();
  49.                 } catch (SQLException e) {
  50.                     e.printStackTrace();
  51.                 } finally {
  52.                    
  53.                 }
  54.             }
  55.            
  56.         });
  57.     }
  58.    
  59.     /**
  60.      * This method is in charge up Sending UPDATE and INSERT MySql Queries
  61.      * @param query The MySql Query you want to send
  62.      * @param pluginName The instance of your plugin
  63.      */
  64.     public static void executeUpdate(String query, String pluginName) {
  65.         try {
  66.             Class.forName("com.mysql.jdbc.Driver");
  67.             Connection conn = DriverManager.getConnection(Core.driver);
  68.             Statement stmt = conn.createStatement();
  69.             Core.debug(pluginName, "DbHandler.executeUpdate", "Query String = " + query);
  70.             stmt.executeUpdate(query);
  71.             stmt.close();
  72.             conn.close();
  73.         } catch (ClassNotFoundException e) {
  74.             e.printStackTrace();
  75.         } catch (SQLException e) {
  76.             // TODO Auto-generated catch block
  77.             e.printStackTrace();
  78.         } finally {
  79.            
  80.         }
  81.     }
  82.    
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement