Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.sky.sql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.bukkit.Bukkit;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.EventPriority;
- import static org.bukkit.event.EventPriority.HIGHEST;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerJoinEvent;
- import org.bukkit.plugin.Plugin;
- import org.bukkit.plugin.java.JavaPlugin;
- public class Main extends JavaPlugin
- implements Listener {
- //Variables
- private static Connection connection;
- private static ResultSet lastQueryResult;
- //
- //Basic Bukkit Stuff
- @Override
- public void onEnable() {
- System.out.println("Enabled.");
- connect();
- getServer().getPluginManager().registerEvents(this, this);
- }
- public void onDisable() {
- System.out.println("Disabled.");
- }
- //
- @EventHandler(priority = EventPriority.HIGHEST)
- public void onLogin(PlayerJoinEvent e) {
- executeUpdate("INSERT INTO dep (username) VALUES ("+e.getPlayer().getName()+")");
- System.out.println("Inserted");
- }
- //
- public void executeUpdate(final String query) {
- Bukkit.getServer().getScheduler().runTaskAsynchronously(this, new Runnable() {
- @Override
- public void run() {
- PreparedStatement preparedStatement = null;
- try {
- preparedStatement = connection.prepareStatement(query);
- preparedStatement.executeUpdate();
- } catch (SQLException exception) {
- } finally {
- try {
- if (preparedStatement != null) {
- preparedStatement.close();
- }
- } catch (SQLException exception) {
- }
- }
- }
- });
- }
- public void connect() {
- Bukkit.getScheduler().runTaskAsynchronously(this, new Runnable() {
- @Override
- public void run() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://" + "xxxxx" + ":" + "3306" + "/" + "db", "xxxx", "xxxxx");
- System.out.println("Successfully connected to database...");
- } catch (ClassNotFoundException | SQLException exception) {
- //Util.throwErrorMessage("Encountered error whilst connecting to database...");
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement