java - 为什么 MySQL 数据库返回空 ResultSet 虽然数据应该是正确的
问题描述
上下文: 我的数据库中有 2 个不同的表。第一个是动作块(Java 中的 ActionBlock),第二个是位置(Java 中的 PermanentLocation)。ActionBlock 类需要一个位置才能工作,因此为了让代码工作,它首先加载包含所需位置的 ActionBlock 数据,然后代码加载必要的位置。(是的,我知道下面的代码会垃圾加载位置查询)
问题: 使用与 ActionBlock 加载器相同的 DB 连接加载位置会给出空的 ResultSet
我试过的:
- 使用不同的连接:没有区别
- 验证密钥是否相同(id 和 uuid):它们是
- 测试数据库中的查询:在 DBMS 中创建选择查询会返回 1 个特定行,但将完全相同的查询代码复制到 Java 仍会导致空 ResultSet(对于此测试,PreparedStatement 已更改为 Statement)
- 验证我使用的是正确的数据库:动作块从数据库加载正常,我使用相同的连接来加载位置
- 代码中的其他连接是否关闭?:是
- 控制台中的错误是什么?:没有错误打印到控制台
“无法加载位置”始终打印到控制台(因为加载程序返回 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(插入到动作块中的是 indev 代码,它有一个小错误)
需要更多信息?本帖来源于Spigot论坛帖子
编辑:重新启动数据库或系统不会影响结果
解决方案
我猜问题只是错字,请换行
"SELECT uuid,permission,classtype FROM actionblock WHERE serverID = ?;");
和
"SELECT uuid,permission,classtype FROM actionblock WHERE serverID = ?");
希望我有帮助;
推荐阅读
- javascript - 组件实例未定义
- asp.net - 如何检查asp.net应用程序中是否启用了身份验证?
- python - TypeError:“LogisticRegression”对象在逻辑回归中不可调用
- scala - 什么功能技术使得不必通过功能传递配置
- reactjs - 使用 no-cors 模式强制 Content-Type 为 application/json
- react-native - 反应本机应用程序:无权使用位置服务
- python - 有没有办法在 Linux 上读取 Access 数据库?Python
- scala - 如何使用一些 MaterializedValue 创建 Sink
- python - 使用 pandas 将 CSV 文件中的字符串转换为浮点数或整数?
- python - 如何将列表中的每 10 个数字除以 10?