Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Change to your package location
- package ;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import org.bukkit.Bukkit;
- import org.bukkit.plugin.Plugin;
- //Change to the location of your MySqlCallback Interface
- import ;
- public class DbHandlerTutorial {
- /**
- * This method is in charge of grabbing queries and sending the result to the MySqlCallback
- * @param plugin The instance of your plugin
- * @param query The MySql query
- * @param callback The MySql Callback
- */
- public static void executeQuery(Plugin plugin, String query, MySqlCallback callback) {
- //Schedules a new Asynchronous task
- Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
- @Override
- public void run() {
- try {
- //Loads the MySql JDBC driver
- Class.forName("com.mysql.jdbc.Driver");
- //Create a connection to your mysql server
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/database?user=root&password=password");
- //Creates a statement for our connection
- Statement stmt = conn.createStatement();
- //Executes the query and grabs the data from the MySql Server
- ResultSet rs = stmt.executeQuery(query);
- //Creates another task but this time throwing us back insync with the minecraft server
- Bukkit.getScheduler().runTask(plugin, new Runnable() {
- @Override
- public void run() {
- //passes the MySql data to out MySqlCallback
- callback.onQueryDone(rs);
- }
- });
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- }
- }
- });
- }
- /**
- * This method is in charge up Sending UPDATE and INSERT MySql Queries
- * @param query The MySql Query you want to send
- * @param pluginName The instance of your plugin
- */
- public static void executeUpdate(String query, String pluginName) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection(Core.driver);
- Statement stmt = conn.createStatement();
- Core.debug(pluginName, "DbHandler.executeUpdate", "Query String = " + query);
- stmt.executeUpdate(query);
- stmt.close();
- conn.close();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement