【问题标题】:Why does MySQL database return empty ResultSet although data should be correct为什么 MySQL 数据库返回空 ResultSet 虽然数据应该是正确的
【发布时间】:2020-03-15 01:54:18
【问题描述】:

背景: 我的数据库中有 2 个不同的表。第一个是动作块(Java 中的 ActionBlock),第二个是位置(Java 中的 PermanentLocation)。 ActionBlock 类需要一个位置才能工作,因此为了让代码工作,它首先加载包含所需位置的 ActionBlock 数据,然后代码加载必要的位置。 (是的,我知道下面的代码会垃圾加载位置查询)

问题: 加载位置会使用与 ActionBlock 加载器相同的 DB 连接提供空的 ResultSet

我的尝试:

  1. 使用不同的连接:没有区别
  2. 验证密钥是否相同(id 和 uuid):它们是
  3. 在数据库中测试查询:在 DBMS 中创建选择查询会返回 1 个特定行,但将完全相同的查询代码复制到 Java 仍会导致空 ResultSet(对于此测试,PreparedStatement 已更改为 Statement)
  4. 验证我使用的是正确的数据库:动作块可以从数据库中正常加载,并且我使用相同的连接来加载位置
  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();
        }
    }
}

永久位置加载代码

/**
 * 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代码有一个小错误)

来自(我假设)Java 的查询

位置表中的内容

动作块表中的内容

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

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

【问题讨论】:

    标签: java mysql resultset


    【解决方案1】:

    我猜是错字,请换行

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

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

    希望我能有所帮助;

    【讨论】:

    • 您建议的修改仅影响动作块加载器,而不影响可能是问题所在的位置加载器。据我所知,“;”在 Java 中的单个查询语句中应该无关紧要,因为它只标记查询的结束,由于我认为 Java 的工作方式,它仍然会以任何一种方式结束(这就是我想你的这个解决方案的意思)。我测试了这个解决方案,但它不影响结果; ResultSet 仍然是空的
    【解决方案2】:

    找到答案:在服务器启动时无法加载位置,因为其他类只有在完全启动后才能找到必要的信息(世界)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多