Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package me.sky.sql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.event.EventHandler;
  12. import org.bukkit.event.EventPriority;
  13. import static org.bukkit.event.EventPriority.HIGHEST;
  14. import org.bukkit.event.Listener;
  15. import org.bukkit.event.player.PlayerJoinEvent;
  16. import org.bukkit.plugin.Plugin;
  17. import org.bukkit.plugin.java.JavaPlugin;
  18.  
  19. public class Main extends JavaPlugin
  20.         implements Listener {
  21.  
  22. //Variables
  23.     private static Connection connection;
  24.     private static ResultSet lastQueryResult;
  25.  
  26.     //
  27.     //Basic Bukkit Stuff
  28.     @Override
  29.     public void onEnable() {
  30.         System.out.println("Enabled.");
  31.         connect();
  32.  
  33.         getServer().getPluginManager().registerEvents(this, this);
  34.     }
  35.  
  36.     public void onDisable() {
  37.         System.out.println("Disabled.");
  38.     }
  39.     //
  40.  
  41.     @EventHandler(priority = EventPriority.HIGHEST)
  42.     public void onLogin(PlayerJoinEvent e) {
  43.         executeUpdate("INSERT INTO dep (username) VALUES ("+e.getPlayer().getName()+")");
  44.         System.out.println("Inserted");
  45.     }
  46.     //
  47.  
  48.     public void executeUpdate(final String query) {
  49.  
  50.         Bukkit.getServer().getScheduler().runTaskAsynchronously(this, new Runnable() {
  51.  
  52.             @Override
  53.             public void run() {
  54.                 PreparedStatement preparedStatement = null;
  55.                 try {
  56.                     preparedStatement = connection.prepareStatement(query);
  57.                     preparedStatement.executeUpdate();
  58.                 } catch (SQLException exception) {
  59.                 } finally {
  60.                     try {
  61.                         if (preparedStatement != null) {
  62.                             preparedStatement.close();
  63.                         }
  64.                     } catch (SQLException exception) {
  65.                     }
  66.                 }
  67.             }
  68.         });
  69.     }
  70.  
  71.     public void connect() {
  72.         Bukkit.getScheduler().runTaskAsynchronously(this, new Runnable() {
  73.          
  74.             @Override
  75.             public void run() {
  76.                 try {
  77.                     Class.forName("com.mysql.jdbc.Driver");
  78.                     connection = DriverManager.getConnection("jdbc:mysql://" + "xxxxx" + ":" + "3306" + "/" + "db", "xxxx", "xxxxx");
  79.                     System.out.println("Successfully connected to database...");
  80.  
  81.                 } catch (ClassNotFoundException | SQLException exception) {
  82.                     //Util.throwErrorMessage("Encountered error whilst connecting to database...");
  83.                 }
  84.             }
  85.         });
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement