gpt4 book ai didi

java - 插入 MySQL Java

转载 作者:行者123 更新时间:2023-11-30 22:20:47 24 4
gpt4 key购买 nike

我的 mysql setBanned 语句有问题。我试图将一个用户设置为禁止进入数据库,但它向我抛出“连接关闭后不允许进行任何操作”。谁能帮帮我?

它抛出一个错误

statement.setString(3, reason);

在setBanned代码中。

当前的 setBanned 代码:

    public boolean setBanned(Player player, boolean state, String reason){
Connection conn = null;
PreparedStatement statement = null;
String setBanned = "INSERT INTO `test_bans` (UUID, BANNED, REASON) VALUES ( ?, ?, ?)";
String setUnbanned = "UPDATE `test_bans` SET BANNED= ? WHERE UUID= ?";
try{
conn = MySql.getConnection();
if(isBanned(player) && state == false){
statement = conn.prepareStatement(setUnbanned);
statement.setBoolean(1, state);
statement.setString(2, player.getUniqueId().toString());
statement.executeUpdate();
} else {
statement = conn.prepareStatement(setBanned);
statement.setString(1, player.getUniqueId().toString());
statement.setBoolean(2, state);
statement.setString(3, reason);
statement.executeUpdate();
}
} catch (SQLException e){
e.printStackTrace();
} finally {
if(statement != null){
try{
statement.close();
} catch (SQLException e){
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e){
e.printStackTrace();
}
}
}
return false;
}

被禁止的代码:

    public boolean isBanned(Player player){
Connection conn = null;
PreparedStatement statement = null;
ResultSet result = null;
String query = "SELECT BANNED FROM `test_bans` WHERE UUID= ?";
try{
conn = MySql.getConnection();
statement = conn.prepareStatement(query);
statement.setString(1, player.getUniqueId().toString());
result = statement.executeQuery();
return result.next();
} catch (SQLException e){
e.printStackTrace();
} finally {
if (result != null){
try{
result.close();
} catch (SQLException e){
e.printStackTrace();
}
}
if(statement != null){
try{
statement.close();
} catch (SQLException e){
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e){
e.printStackTrace();
}
}
}
return false;
}

编辑:MySQL 类

public class MySql {

private static String DATABASE_NAME ="testing_database";
private static String DATABASE_IP ="localhost:3306";
private static String connection ="jdbc:mysql://"+DATABASE_IP+"/"+DATABASE_NAME;
private static String username ="root";
private static String password ="pass";

private static Connection conn;

public static void handleConnection(){
Bukkit.getServer().getLogger().info("Establishing connection to MySQL Database....");
try{
conn = DriverManager.getConnection(connection, username, password);
if(!conn.isClosed()){
Bukkit.getServer().getLogger().info("Successfully synced with MySQL!");
return;
}
return;
}
catch (SQLException e){
e.printStackTrace();
return;
}
}

public static void disconnectConnection(){
Bukkit.getServer().getLogger().info("Disconnecting from MySQL Databse....");
try{
conn.close();
if(conn.isClosed()){
Bukkit.getServer().getLogger().info("Successfully Disconnected");
}
return;
}
catch(SQLException e){
e.printStackTrace();
return;
}
}

public static boolean setupTables(){
try{
System.out.println("Checking Tables");
conn.prepareStatement("CREATE TABLE IF NOT EXISTS test_ranks (UUID VARCHAR(255), RANK VARCHAR(25));").executeUpdate();
conn.prepareStatement("CREATE TABLE IF NOT EXISTS test_bans (UUID VARCHAR(255), BANNED BOOLEAN, REASON TEXT);").executeUpdate();
return true;
}
catch(SQLException e){
e.printStackTrace();
return false;
}
}

public static boolean checkConnection(){
try{
if((conn != null) && (!conn.isClosed())){
return true;
}
return false;
}
catch(SQLException e){
e.printStackTrace();
return false;
}
}

public static Connection getConnection(){
try {
if(conn.isClosed()){
connectMySQL();
return conn;
}
if(conn == null){
connectMySQL();
return conn;
}
} catch (SQLException e){
e.printStackTrace();
}
return conn;
}

public static void connectMySQL(){
try {
conn = DriverManager.getConnection(connection, username, password);
return;
} catch(SQLException e){
e.printStackTrace();
}
}

最佳答案

你的问题不清楚....

没有任何类 MySql。如果它的自定义类...提供它的代码片段。

Mysql JDBC连接的实际使用方式

Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password");

//Create statement

//Execute statement
//Iterate over results
//close statement
conn.close(); // close connection

关于java - 插入 MySQL Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36685359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com