Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SQLManager;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.util.UUID;
- import org.bukkit.Bukkit;
- import org.bukkit.entity.Player;
- import com.mysql.jdbc.PreparedStatement;
- public class MySQL {
- public static void criarTabela() {
- try {
- Connection con = openConexao();
- PreparedStatement prepareStat = (PreparedStatement) con
- .prepareStatement("create table if not exists coins(Jogador varChar(15), quantia decimal(10,2));");
- prepareStat.executeUpdate();
- con.close();
- } catch (Exception e) {
- Bukkit.getConsoleSender().sendMessage("A tabela não foi criada.");
- }
- }
- public static void addJogadorNaTabela(Player jogador) {
- UUID IDDOJOGADOR = jogador.getUniqueId();
- try {
- Connection con = openConexao();
- PreparedStatement prepareStat = (PreparedStatement) con.prepareStatement("Insert into coins values (?, 0)");
- prepareStat.setString(1, IDDOJOGADOR.toString());
- prepareStat.executeUpdate();
- con.close();
- } catch (Exception e) {
- Bukkit.getConsoleSender().sendMessage("O jogador não conseguiu ser adicionado a tabela.");
- }
- }
- public static void setarCoins(Player jogador, double quantia) {
- UUID IDDOJOGADOR = jogador.getUniqueId();
- try {
- Connection con = openConexao();
- PreparedStatement prepareStat = (PreparedStatement) con
- .prepareStatement("Update coins set coins = ? where id = ?");
- prepareStat.setDouble(1, quantia);
- prepareStat.setString(2, IDDOJOGADOR.toString());
- prepareStat.executeUpdate();
- con.close();
- } catch (Exception e) {
- Bukkit.getConsoleSender().sendMessage("A quantia não conseguiu ser adicionada.");
- }
- }
- public static Connection openConexao() {
- try {
- String senha = "";
- String usuario = "root";
- String servidor = "localhost";
- String port = "3306";
- String database = "coins";
- String type = "jdbc:mysql://";
- String url = type + servidor + ":" + port + "/" + database;
- return DriverManager.getConnection(url, usuario, senha);
- } catch (Exception e) {
- Bukkit.getConsoleSender().sendMessage("O mysql não pode ser iniciado.");
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement