gpt4 book ai didi

java - 为什么 MySQL 数据库返回空结果集,尽管数据应该是正确的

转载 作者:行者123 更新时间:2023-11-29 15:28:00 25 4
gpt4 key购买 nike

上下文:我的数据库中有两个不同的表。第一个是actionblock(Java中的ActionBlock),第二个是location(Java中的PermanentLocation)。 ActionBlock 类需要一个工作位置,因此为了使代码工作,它首先加载包含所需位置的 ActionBlock 数据,然后代码加载必要的位置。 (是的,我知道以下代码会垃圾邮件加载位置查询)

问题:加载位置会使用与 ActionBlock 加载器相同的数据库连接提供空结果集

我尝试过的:

  1. 使用不同的连接:没有区别
  2. 验证 key 是否相同(id 和 uuid):它们是
  3. 在数据库中测试查询:在 DBMS 中创建选择查询会返回 1 个特定行,但将完全相同的查询代码复制到 Java 仍然会产生空 ResultSet(对于此测试,PreparedStatement 已更改为 Statement)
  4. 验证我使用的是正确的数据库:操作 block 可以从数据库正常加载,并且我使用相同的连接来加载位置
  5. 代码中的其他连接是否已关闭?:是
  6. 控制台中出现什么错误?:控制台没有打印任何错误

“无法加载位置”始终打印到控制台(因为加载器返回 null)ActionBlock加载代码

/**
* Loads the blocks from the database. All registerClass calls must be called before this
*/
public void loadBlocksFromDatabase() {
Connection conn = null;
try {
conn = KaranteeniCore.getDatabaseConnector().openConnection();
PreparedStatement stmt = conn.prepareStatement(
"SELECT uuid,permission,classtype FROM actionblock WHERE serverID = ?;");
stmt.setString(1, KaranteeniCore.getServerIdentificator());
ResultSet set = stmt.executeQuery();

// loop all the blocks from the database
while(set.next()) {
String uuidStr = set.getString(1);
String permission = set.getString(2);
String classType = set.getString(3);

UUID uuid = UUID.fromString(uuidStr);

// check if loaded block is a block or a sign
for(Class<? extends ActionBlock> clazz : subBlockClasses)
if(clazz.getName().equals(classType)) {
// load the location of the block
PermanentLocation loc = PermanentLocation.loadLocation(conn, uuid);
if(loc == null) {
Bukkit.getLogger().log(Level.WARNING, "Could not load location " + uuid.toString() + ". Has it been deleted from database?");
continue;
}

Block block = loc.getLocation().getBlock();
try {
// try to create a new class out of this block
if(permission != null) {
Constructor<? extends ActionBlock> cstr = clazz.getConstructor(Block.class, UUID.class, String.class);
ActionBlock aBlock = cstr.newInstance(block, uuid, permission);
aBlock.onLoad();
registerBlock(aBlock); // register created class
} else {
Constructor<? extends ActionBlock> cstr = clazz.getConstructor(Block.class, UUID.class);
ActionBlock aBlock = cstr.newInstance(block, uuid);
aBlock.onLoad();
registerBlock(aBlock); // register created class
}
} catch (Exception e) {
e.printStackTrace();
}
}

// load all signs
for(Class<? extends ActionSign> clazz : subSignClasses)
if(clazz.getName().equals(classType)) {
// load the location of the block
Block block = PermanentLocation.loadLocation(uuid).getLocation().getBlock();
try {
// try to create a new class out of this block
if(permission != null) {
Constructor<? extends ActionSign> cstr = clazz.getConstructor(Block.class, UUID.class, String.class);
ActionBlock aBlock = cstr.newInstance(block, uuid, permission);
aBlock.onLoad();
registerBlock(aBlock); // register created class
} else {
Constructor<? extends ActionSign> cstr = clazz.getConstructor(Block.class, UUID.class);
ActionBlock aBlock = cstr.newInstance(block, uuid);
aBlock.onLoad();
registerBlock(aBlock); // register created class
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch(SQLException e) {
e.printStackTrace();
// ignored
} finally {
try {
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

PermanentLocation加载代码

/**
* Loads a permanent location from database using the given uuid
* @param conn Connection to the database
* @param uuid uuid of the location
* @return location loaded or null if none found
*/
public static PermanentLocation loadLocation(Connection conn, UUID uuid) {
PermanentLocation location = null;
try {
PreparedStatement st = conn.prepareStatement("SELECT world, x, y, z, pitch, yaw FROM location WHERE id = ? AND serverID = ?;");
st.setString(1, uuid.toString());
st.setString(2, KaranteeniPlugin.getServerIdentificator());
ResultSet set = st.executeQuery();
if(set.next()) {
String w = set.getString(1);
double x = set.getDouble(2);
double y = set.getDouble(3);
double z = set.getDouble(4);
float pitch = set.getFloat(5);
float yaw = set.getFloat(6);

World world = Bukkit.getWorld(w);
if(world == null)
return null;
else
Bukkit.getLogger().log(Level.SEVERE, "Could not find world " + w);

Location loc = new Location(world, x,y,z,pitch,yaw);

location = new PermanentLocation(uuid, loc);
}
else
location = null;
} catch(SQLException e) {
Bukkit.getLogger().log(Level.SEVERE, e.getMessage());
}

return location;
}

sys.statements_with_errors_or_warnings(插入到actionblock中的是indev代码,有一个小错误) sys.statements_with_errors_or_warnings

来自(我假设)Java 的查询 query received from Java

位置表中的内容 database location tables contents

操作 block 表中的内容 actionblock table contents

需要更多信息?本帖源自Spigot forum post

编辑:重启数据库或系统不影响结果

最佳答案

我猜问题只是拼写错误,请更改行

  "SELECT uuid,permission,classtype FROM actionblock WHERE serverID = ?;");

"SELECT uuid,permission,classtype FROM actionblock WHERE serverID = ?");

希望我能有所帮助;

关于java - 为什么 MySQL 数据库返回空结果集,尽管数据应该是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58935853/

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