Advertisement
Guest User

Untitled

a guest
Dec 16th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. package fr.kerwan.calendar;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.entity.Player;
  5.  
  6. import java.sql.*;
  7.  
  8. /**
  9. * Created by Kerwan on 08/11/2016.
  10. */
  11. public class Database {
  12.  
  13. private static Connection connection;
  14.  
  15. public static void close() {
  16. try {
  17. if (connection != null && !connection.isClosed()) {
  18. connection.close();
  19. }
  20. } catch (SQLException e) {
  21. e.printStackTrace();
  22. }
  23.  
  24. }
  25.  
  26. public synchronized static void open(){
  27. Bukkit.getLogger().info(">> Connection to database");
  28. try{
  29. connection = DriverManager.getConnection("jdbc:mysql://mineaurion.com:3306/kerwan", "kerwan", "QLar1QV0Bmyss7eZ");
  30. Bukkit.getLogger().info(">> Connection succesfull");
  31. }catch (Exception e){
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. public synchronized static boolean hasVoted(Player p){
  37. try{
  38. PreparedStatement sql = connection.prepareStatement("SELECT `Voted` FROM `Calendar` WHERE UUID=?;");
  39. sql.setString(1, p.getUniqueId().toString());
  40. ResultSet resultSet = sql.executeQuery();
  41. boolean containsPlayer = resultSet.next();
  42. resultSet.close();
  43.  
  44. return containsPlayer;
  45. }catch (Exception e){
  46. return false;
  47. }
  48. }
  49.  
  50. public synchronized static boolean containPlayer(Player p){
  51. try{
  52. PreparedStatement sql = connection.prepareStatement("SELECT * FROM `Calendar` WHERE UUID=?;");
  53. sql.setString(1, p.getUniqueId().toString());
  54. ResultSet resultSet = sql.executeQuery();
  55. boolean containsPlayer = resultSet.next();
  56. resultSet.close();
  57.  
  58. return containsPlayer;
  59. }catch (Exception e){
  60. return false;
  61. }
  62. }
  63.  
  64. public synchronized static void createPlayer(Player p){
  65. try {
  66. PreparedStatement newPlayer = connection.prepareStatement("INSERT INTO `Calendar` values(?,?,0);");
  67. newPlayer.setString(1, p.getName());
  68. newPlayer.setString(2, p.getUniqueId().toString());
  69. newPlayer.execute();
  70. newPlayer.close();
  71. } catch (SQLException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75.  
  76. public synchronized static void setVoted(Player p){
  77. try {
  78. PreparedStatement newAura = connection.prepareStatement("UPDATE `Calendar` SET `Voted`=true WHERE Nickname='" + p.getName() +"';");
  79. newAura.execute();
  80. newAura.close();
  81. } catch (SQLException e) {
  82. e.printStackTrace();
  83. }
  84. }
  85.  
  86. public synchronized static void clearDatabase(){
  87. try {
  88. PreparedStatement newAura = connection.prepareStatement("DELETE FROM `Calendar`;");
  89. newAura.execute();
  90. newAura.close();
  91. } catch (SQLException e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement