Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. Main :
  2. package Coins;
  3.  
  4. import org.bukkit.plugin.java.JavaPlugin;
  5.  
  6. public class Main extends JavaPlugin {
  7.  
  8. @Override
  9. public void onEnable() {
  10. MySQL.connect();
  11. MySQL.createTable();
  12. CoinAPI.setCoins("test", 10);
  13. }
  14. public void onDisable() {
  15. MySQL.disconnect();
  16. }
  17. }
  18. CoinAPI:
  19. package Coins;
  20.  
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24.  
  25. import org.bukkit.entity.Player;
  26.  
  27. public class CoinAPI {
  28.  
  29. public static int getCoins(String uuid) {
  30. try {
  31. PreparedStatement st = MySQL.con.prepareStatement("SELECT coins FROM coinTable WHERE UUID = ?");
  32. st.setString(1, uuid);
  33. ResultSet rs = st.executeQuery();
  34. while (rs.next()) {
  35. return rs.getInt("coins");
  36. }
  37.  
  38.  
  39. } catch (SQLException e) {
  40. e.printStackTrace();
  41. }
  42. return -1;
  43. }
  44. public static void setCoins(String uuid, int coins) {
  45. if(getCoins(uuid) == 1) {
  46. try {
  47. PreparedStatement st = MySQL.con.prepareStatement("INSERT INTO coinTable (UUID,coins) VALUES (?,?)");
  48. st.setString(1, uuid);
  49. st.setInt(2, coins);
  50. st.executeUpdate();
  51. } catch (SQLException e) {
  52. e.printStackTrace();
  53. }
  54. } else {
  55. PreparedStatement st;
  56. try {
  57. st = MySQL.con.prepareStatement("UPDATE coinTable SET coins = ? WHERE UUID = ?");
  58. st.setString(2, uuid);
  59. st.setInt(1, coins);
  60. st.executeUpdate();
  61. } catch (SQLException e) {
  62. e.printStackTrace();
  63. }
  64.  
  65. }
  66. }
  67. public static void addCoins(String uuid, int coins) {
  68.  
  69. setCoins(uuid, coins + getCoins(uuid));
  70. }
  71. public static void removeCoins(String uuid, int coins) {
  72. setCoins(uuid, getCoins(uuid) - coins);
  73. }
  74. }
  75. MySQL:
  76. package Coins;
  77.  
  78. import java.sql.Connection;
  79. import java.sql.DriverManager;
  80. import java.sql.SQLException;
  81.  
  82.  
  83.  
  84. public class MySQL {
  85.  
  86. public static Connection con;
  87. public static String passvd = "m9TM3RfpLkeRfJdKbqtu";
  88. public static String database = "KontanexCoins";
  89. public static String user = "root";
  90. public static String host = "localhost";
  91. public static int port = 3306;
  92.  
  93.  
  94. public static void connect() {
  95. if(!isConnected()) {
  96. try {
  97. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user, passvd);
  98. System.out.println("§aMySQL Verbunden!");
  99. } catch (SQLException e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. }
  104. public static void disconnect() {
  105. if (isConnected()) {
  106. try {
  107. con.close();
  108. System.out.println("§aMySQL verbindung getrennt!");
  109. } catch (SQLException e) {
  110. e.printStackTrace();
  111. }
  112. }
  113.  
  114. }
  115. public static boolean isConnected() {
  116. return (con != null);
  117. }
  118. public static void createTable() {
  119. try {
  120. con.prepareStatement("CREATE TABLE IF NOT EXISTS coinTable (UUID VARCHAR (100), UUID VARCHAR (100), coins INT (16))").executeUpdate();
  121. } catch (SQLException e) {
  122. e.printStackTrace();
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement