Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. public class PlayerSQL {
  2. public static Connection conn;
  3.  
  4. public static void registerPlayer(Player p){
  5. ResultSet rs = executeQuery("SELECT PlayerName FROM playersql WHERE PlayerName='" + p.getName() + "'");
  6. try {
  7. if(!rs.next()){
  8. executeUpdate("INSERT INTO playersql (PlayerName,Wetoins,Weteys) VALUES ('" + p.getName() + "',0,0)");
  9. }
  10. } catch (SQLException e) {
  11. e.printStackTrace();
  12. }
  13. }
  14.  
  15. public static int getWetoins(Player p){
  16. ResultSet coinsRS = executeQuery("SELECT Wetoins FROM playersql WHERE PlayerName='" + p.getName() + "'");
  17. int coins = 0;
  18. try {
  19. while(coinsRS.next()){
  20. coins = coinsRS.getInt("Wetoins");
  21. }
  22. } catch (SQLException e) {
  23. e.printStackTrace();
  24. }
  25. return coins;
  26. }
  27.  
  28. public static void addWetoins(Player p,int amount){
  29. executeUpdate("UPDATE playersql SET Wetoins=" + (getWetoins(p)+amount) + " WHERE PlayerName='" + p.getName() + "'");
  30. }
  31.  
  32. public static void removeWetoins(Player p,int amount){
  33. executeUpdate("UPDATE playersql SET Wetoins=" + (getWetoins(p)-amount) + " WHERE PlayerName='" + p.getName() + "'");
  34. }
  35. public static int getWeteys(Player p){
  36. ResultSet coinsRS = executeQuery("SELECT Weteys FROM playersql WHERE PlayerName='" + p.getName() + "'");
  37. int coins = 0;
  38. try {
  39. while(coinsRS.next()){
  40. coins = coinsRS.getInt("Weteys");
  41. }
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. return coins;
  46. }
  47.  
  48. public static void addWeteys(Player p,int amount){
  49. executeUpdate("UPDATE playersql SET Weteys=" + (getWeteys(p)+amount) + " WHERE PlayerName='" + p.getName() + "'");
  50. }
  51.  
  52. public static void removeWeteys(Player p,int amount){
  53. executeUpdate("UPDATE playersql SET Weteys=" + (getWeteys(p)-amount) + " WHERE PlayerName='" + p.getName() + "'");
  54. }
  55.  
  56. public static void connect(String host,Integer port,String USER,String PASS,String DB_NAME){
  57. String DB = "jdbc:mysql://" + host + ":" + port + "/" +DB_NAME;
  58.  
  59. try {
  60. Class.forName("com.mysql.jdbc.Driver");
  61. Bukkit.getLogger().info("About to connect to database");
  62. conn = DriverManager.getConnection(DB + "?autoReconnect=true", USER, PASS);
  63. Bukkit.getLogger().info("Successfully connected.");
  64. conn.createStatement();
  65. }catch(SQLException e){
  66. e.printStackTrace();
  67. }catch(ClassNotFoundException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71.  
  72. public static Statement createStatement(){
  73. Statement s=null;
  74. try {
  75. s = conn.createStatement();
  76. } catch (SQLException e) {
  77. e.printStackTrace();
  78. }
  79. return s;
  80. }
  81. public static void closeConnection() {
  82. try {
  83. if( conn != null && !conn.isClosed() ) {
  84. conn.close();
  85. }
  86. } catch (SQLException e) {
  87. e.printStackTrace();
  88. }
  89. }
  90.  
  91. public static ResultSet executeQuery(String q){
  92. ResultSet rs = null;
  93. try {
  94. rs = createStatement().executeQuery(q);
  95. } catch (SQLException e) {
  96. e.printStackTrace();
  97. Bukkit.getLogger().warning("[Coins] Error occured while trying to execute query: \"" + q + "\"");
  98. }
  99. return rs;
  100. }
  101.  
  102. public static void executeUpdate(String u){
  103. try {
  104. createStatement().executeUpdate(u);
  105. } catch (SQLException e) {
  106. e.printStackTrace();
  107. Bukkit.getLogger().warning("[Coins] Error occured while trying to execute update: \"" + u + "\"");
  108. }
  109. }
  110.  
  111. public static void updateData(String table, String newData, String conditions){
  112. executeUpdate("UPDATE " + table + " SET " + newData + " WHERE " + conditions);
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement