Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.bradley.TestingMySQL;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerJoinEvent;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.scheduler.BukkitRunnable;
- public class Core extends JavaPlugin implements Listener {
- Connection connection;
- private Core plugin;
- private String username = "sql8154449";
- private String password = "hGEl3P6H7K";
- public void onEnable() {
- getServer().getPluginManager().registerEvents(this, this);
- plugin = this;
- printData();
- }
- private void printData() {
- new BukkitRunnable() {
- @Override
- public void run() {
- openConnection();
- List<String> temp = getNameColumn("name");
- new BukkitRunnable() {
- @Override
- public void run() {
- if (!temp.isEmpty()) {
- for (String name : temp) {
- System.out.println("I found the name: " + name);
- }
- } else {
- System.out.println("There were no names :(");
- }
- }
- }.runTask(plugin);
- }
- }.runTaskAsynchronously(this);
- }
- public synchronized void openConnection() {
- try {
- connection = DriverManager.getConnection("jdbc:mysql://sql8.freemysqlhosting.net:3306/sql8154449", username,
- password);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public boolean containsElement(String field, String element) {
- String query = "SELECT `" + field + "` FROM `testing` WHERE `" + field + "`='" + element + "'";
- try {
- ResultSet res = connection.prepareStatement(query).executeQuery();
- boolean temp = res.next();
- res.close();
- return temp;
- } catch (SQLException e) {
- e.printStackTrace();
- return true;
- }
- }
- public void insertElement(String element, String uuid) {
- String query = "INSERT INTO `testing` (`name`, `uuid`) VALUES('" + element + "', '" + uuid + "')";
- PreparedStatement p;
- try {
- p = connection.prepareStatement(query);
- p.execute();
- p.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public List<String> getNameColumn(String columnName) {
- List<String> temp = new ArrayList<String>();
- String query = "SELECT '" + columnName + "' FROM `testing`";
- try {
- ResultSet res = connection.prepareStatement(query).executeQuery();
- while (res.next()) {
- temp.add(res.getString(columnName));
- }
- res.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return temp;
- }
- //testing that it works :)
- @EventHandler
- public void onJoin(PlayerJoinEvent e) {
- Player player = e.getPlayer();
- if (containsElement("name", player.getName())) {
- player.sendMessage("I have you! Your UUID is: " + player.getUniqueId().toString());
- return;
- } else {
- insertElement(player.getName(), player.getUniqueId().toString());
- player.sendMessage("I didn't have you but now I do! Your UUID is: " + player.getUniqueId().toString());
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement