我试图在打破块时将玩家和玩家的位置加载到数据库中,但它输出了一个错误

I tried to load the Location of the player and the player into the database when breaking the block, but it outputs an error

提问人:Dami 78 提问时间:11/14/2023 最后编辑:TylerHDami 78 更新时间:11/18/2023 访问量:47

问:

事件块

package minecord.plugingenerin.Event;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;

import javax.management.timer.Timer;
import java.util.concurrent.ExecutorService;

public class EventBlock implements Listener {


    @EventHandler
    public void onDestroy(BlockBreakEvent blockBreak) throws SQLException {
        Player player = blockBreak.getPlayer(); //Получение игрока
        Block block = blockBreak.getBlock();
        Location loc = block.getLocation();
        World world = block.getWorld();
        Material material = block.getType();
        player.sendMessage("Блок сломан");
        SqliteDB db =new SqliteDB();
        db.insert(loc,player);


    }
}

SqliteDB数据库

package minecord.plugingenerin.Event;

import org.bukkit.Location;
import org.bukkit.entity.Player;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class SqliteDB {
    SqliteDB program = new SqliteDB();
    Connection c = DriverManager.getConnection("jdbc:sqlite:memory:F:/Location.db");
    Statement stmt = c.createStatement();
    void insert(Location location, Player user) throws SQLException {
        stmt.executeUpdate("insert LocatioInfo values('"+location+"','"+user+"')");
        stmt.executeUpdate("backup to C:Users/Татьяна/Desktop/Server 1.20.1 paper/plugins/DB/backup.db");
    }

    public SqliteDB() throws SQLException {
        stmt.executeUpdate("create table Location(Location Location, User TEXT)");

    }
}

错误

[16:56:39 错误]: 无法将事件 BlockBreakEvent 传递给 PluginGenerin v1.0 java.lang.StackOverflowError:minecord.plugingenerin.Event.SqliteDB
为空。(SqliteDB.java:12) ~[original-PluginGenerin-1.0.jar:?]
在 minecord.plugingenerin.Event.SqliteDB。(SqliteDB.java:12) ~[original-PluginGenerin-1.0.jar:?]
在 minecord.plugingenerin.Event.SqliteDB。(SqliteDB.java:12) ~[original-PluginGenerin-1.0.jar:?]
在 minecord.plugingenerin.Event.SqliteDB。(SqliteDB.java:12) ~[original-PluginGenerin-1.0.jar:?]
在 minecord.plugingenerin.Event.SqliteDB。(SqliteDB.java:12) ~[original-PluginGenerin-1.0.jar:?]
在 minecord.plugingenerin.Event.SqliteDB。(SqliteDB.java:12) ~[original-PluginGenerin-1.0.jar:?]

我尝试删除

SqliteDB db =new SqliteDB();
db.insert(loc,player);
java sqlite jdbc

评论


答:

2赞 k314159 11/14/2023 #1

您的问题是由以下语句引起的:

public class SqliteDB {
    SqliteDB program = new SqliteDB();
    ...
}

当 SqliteDB 类被实例化时,该字段被创建并分配给一个新的 SqliteDB 类,该类有一个字段被创建并分配给新的 SqliteDB 类,依此类推,直到发生堆栈溢出。programprogram