Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main :
- package Coins;
- import org.bukkit.plugin.java.JavaPlugin;
- public class Main extends JavaPlugin {
- @Override
- public void onEnable() {
- MySQL.connect();
- MySQL.createTable();
- CoinAPI.setCoins("test", 10);
- }
- public void onDisable() {
- MySQL.disconnect();
- }
- }
- CoinAPI:
- package Coins;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.bukkit.entity.Player;
- public class CoinAPI {
- public static int getCoins(String uuid) {
- try {
- PreparedStatement st = MySQL.con.prepareStatement("SELECT coins FROM coinTable WHERE UUID = ?");
- st.setString(1, uuid);
- ResultSet rs = st.executeQuery();
- while (rs.next()) {
- return rs.getInt("coins");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return -1;
- }
- public static void setCoins(String uuid, int coins) {
- if(getCoins(uuid) == 1) {
- try {
- PreparedStatement st = MySQL.con.prepareStatement("INSERT INTO coinTable (UUID,coins) VALUES (?,?)");
- st.setString(1, uuid);
- st.setInt(2, coins);
- st.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- } else {
- PreparedStatement st;
- try {
- st = MySQL.con.prepareStatement("UPDATE coinTable SET coins = ? WHERE UUID = ?");
- st.setString(2, uuid);
- st.setInt(1, coins);
- st.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void addCoins(String uuid, int coins) {
- setCoins(uuid, coins + getCoins(uuid));
- }
- public static void removeCoins(String uuid, int coins) {
- setCoins(uuid, getCoins(uuid) - coins);
- }
- }
- MySQL:
- package Coins;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class MySQL {
- public static Connection con;
- public static String passvd = "m9TM3RfpLkeRfJdKbqtu";
- public static String database = "KontanexCoins";
- public static String user = "root";
- public static String host = "localhost";
- public static int port = 3306;
- public static void connect() {
- if(!isConnected()) {
- try {
- con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user, passvd);
- System.out.println("§aMySQL Verbunden!");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static void disconnect() {
- if (isConnected()) {
- try {
- con.close();
- System.out.println("§aMySQL verbindung getrennt!");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public static boolean isConnected() {
- return (con != null);
- }
- public static void createTable() {
- try {
- con.prepareStatement("CREATE TABLE IF NOT EXISTS coinTable (UUID VARCHAR (100), UUID VARCHAR (100), coins INT (16))").executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement