Bom fiz um armazenamento para tags... em breve + coisas só que quando ele tenta adicionar ou remover nada acontece... não sei o por que .... Não há erros nem nada ... e é quase o mesmo codigo que usei em todos meus plugins o unico que mudei foi o sistema para aceitar SQLite e MySQL
Code do banco de dados:
public class Database {
private String host, name, username, password, table;
private Connection connection;
public int tentativas = 0;
public int type = 1;
public Database(final String host, final String name, final String username, final String password, final String table) {
//this.url_base = url_base;
this.host = host;
this.name = name;
this.username = username;
this.password = password;
this.table = table;
}
public void connection() {
if (!isConnected()) {
Exception error = null;
if(tentativas >= 11){
if(error != null) {
error.printStackTrace();
}
System.out.println("Failed to start database.");
Gladiador.instance.getServer().getPluginManager().disablePlugin(Gladiador.instance);
return;
}
tentativas++;
try {
type = 2;
connection = DriverManager.getConnection("jdbc:mysql://" + host + "/" + name, username, password);
} catch (Exception e) {
error = e;
System.out.println("Try " + tentativas);
connection();
}
tabela();
}
}
public void connectionsql() {
File db = new File(Gladiador.instance.getDataFolder() + "/database.db");
if(!db.exists()){
try {
db.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
}
if (!isConnected()) {
try {
type = 1;
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:"+ Gladiador.instance.getDataFolder().getAbsolutePath() + FileSystems.getDefault().getSeparator() + "database.db");
} catch (Exception e) {
e.printStackTrace();
}
tabela();
}
}
public void deconnection() {
if (isConnected()) {
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private boolean isConnected() {
try {
if ((connection == null) || (connection.isClosed()) || (!connection.isValid(5))) {
return false;
} else {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private Connection getConnection() {
return connection;
}
public void tabela() {
try {
PreparedStatement sts = getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS `" + table +"` (`Jogador` varchar(500), `UUID` varchar(500), `Clan` varchar(500))");
sts.executeUpdate();
sts.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public boolean add(final Player p, final String clanname) {
try {
System.out.println("Adicionando "+ p.getName());
PreparedStatement sts = getConnection().prepareStatement("SELECT * FROM "+table+" WHERE UUID = ?");
sts.setString(1, p.getUniqueId().toString());
ResultSet rs = sts.executeQuery();
if (!rs.next()) {
PreparedStatement sts2 = getConnection().prepareStatement("INSERT INTO "+table+"(Jogador, UUID, Clan) VALUES (?, ?, ?)");
sts2.setString(1, p.getName());
sts2.setString(2, p.getUniqueId().toString());
sts2.setString(3, clanname);
sts2.close();
}else{
if(rs.getString("Clan") != clanname) {
PreparedStatement sts2 = getConnection().prepareStatement("UPDATE " + table + " SET Clan = ?, WHERE UUID = ?");
sts2.setString(1, clanname);
sts2.setString(2, p.getUniqueId().toString());
sts2.close();
}
}
sts.close();
} catch (SQLException e) {
e.printStackTrace();
}
return true;
}
public boolean remove(final Player p) {
try {
System.out.println("Removendo "+ p.getName());
PreparedStatement sts = getConnection().prepareStatement("SELECT * FROM "+table+" WHERE UUID = ?");
sts.setString(1, p.getUniqueId().toString());
ResultSet rs = sts.executeQuery();
if (rs.next()) {
PreparedStatement sts2 = getConnection().prepareStatement("DELETE FROM `"+table+"` WHERE UUID = ?");
sts2.setString(1, p.getUniqueId().toString());
sts2.close();
}
sts.close();
} catch (SQLException e) {
e.printStackTrace();
}
return true;
}
public boolean remove(final String uuid) {
try {
System.out.println("Removendo "+ uuid);
PreparedStatement sts = getConnection().prepareStatement("SELECT * FROM "+table+" WHERE UUID = ?");
sts.setString(1, uuid);
ResultSet rs = sts.executeQuery();
if (rs.next()) {
PreparedStatement sts2 = getConnection().prepareStatement("DELETE FROM `"+table+"` WHERE UUID = ?");
sts2.setString(1, uuid);
sts2.close();
}
sts.close();
} catch (SQLException e) {
e.printStackTrace();
}
return true;
}
public ArrayList<String> olds() {
ArrayList<String> players = new ArrayList<>();
try {
PreparedStatement sts = getConnection().prepareStatement("SELECT * FROM "+table);
ResultSet rs = sts.executeQuery();
while (rs.next()) {
players.add(rs.getString("UUID"));
}
sts.close();
} catch (SQLException e) {
e.printStackTrace();
}
return players;
}
}
Ao iniciar (Main):
public static Database db;
db = new Database(getConfig().getString("Database.host"),
getConfig().getString("Database.db"),
getConfig().getString("Database.user"),
getConfig().getString("Database.pass"),
getConfig().getString("Database.tb"));
Pergunta
Solitario
Bom fiz um armazenamento para tags... em breve + coisas só que quando ele tenta adicionar ou remover nada acontece... não sei o por que .... Não há erros nem nada ... e é quase o mesmo codigo que usei em todos meus plugins o unico que mudei foi o sistema para aceitar SQLite e MySQL
Code do banco de dados:
Ao iniciar (Main):
OBS: ele cria a tabela normalmente quando inicia ....
Onde solicito a remoção e adição:
Link para o comentário
Compartilhar em outros sites
5 respostass a esta questão
Posts Recomendados