Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. package SQLManager;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.util.UUID;
  6.  
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.entity.Player;
  9.  
  10. import com.mysql.jdbc.PreparedStatement;
  11.  
  12. public class MySQL {
  13.  
  14. public static void criarTabela() {
  15.  
  16. try {
  17.  
  18. Connection con = openConexao();
  19.  
  20. PreparedStatement prepareStat = (PreparedStatement) con
  21. .prepareStatement("create table if not exists coins(Jogador varChar(15), quantia decimal(10,2));");
  22. prepareStat.executeUpdate();
  23.  
  24. con.close();
  25.  
  26. } catch (Exception e) {
  27.  
  28. Bukkit.getConsoleSender().sendMessage("A tabela não foi criada.");
  29.  
  30. }
  31. }
  32.  
  33. public static void addJogadorNaTabela(Player jogador) {
  34. UUID IDDOJOGADOR = jogador.getUniqueId();
  35. try {
  36.  
  37. Connection con = openConexao();
  38. PreparedStatement prepareStat = (PreparedStatement) con.prepareStatement("Insert into coins values (?, 0)");
  39. prepareStat.setString(1, IDDOJOGADOR.toString());
  40. prepareStat.executeUpdate();
  41. con.close();
  42.  
  43. } catch (Exception e) {
  44. Bukkit.getConsoleSender().sendMessage("O jogador não conseguiu ser adicionado a tabela.");
  45. }
  46. }
  47.  
  48.  
  49. public static void setarCoins(Player jogador, double quantia) {
  50.  
  51. UUID IDDOJOGADOR = jogador.getUniqueId();
  52.  
  53. try {
  54.  
  55. Connection con = openConexao();
  56. PreparedStatement prepareStat = (PreparedStatement) con
  57. .prepareStatement("Update coins set coins = ? where id = ?");
  58. prepareStat.setDouble(1, quantia);
  59. prepareStat.setString(2, IDDOJOGADOR.toString());
  60. prepareStat.executeUpdate();
  61. con.close();
  62.  
  63. } catch (Exception e) {
  64. Bukkit.getConsoleSender().sendMessage("A quantia não conseguiu ser adicionada.");
  65. }
  66.  
  67. }
  68.  
  69. public static Connection openConexao() {
  70.  
  71. try {
  72.  
  73. String senha = "";
  74. String usuario = "root";
  75. String servidor = "localhost";
  76. String port = "3306";
  77. String database = "coins";
  78. String type = "jdbc:mysql://";
  79.  
  80. String url = type + servidor + ":" + port + "/" + database;
  81.  
  82. return DriverManager.getConnection(url, usuario, senha);
  83.  
  84. } catch (Exception e) {
  85.  
  86. Bukkit.getConsoleSender().sendMessage("O mysql não pode ser iniciado.");
  87.  
  88. }
  89.  
  90. return null;
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement