【问题标题】:Java and SQL: ResultSetMetaData and ResultSetJava 和 SQL:ResultSetMetaData 和 ResultSet
【发布时间】:2016-11-02 15:34:23
【问题描述】:

我真的需要帮助。我正在尝试使用 ResultSetMetaData 和 ResultSet 从我的数据库中检索数据。我也在使用存储过程。该程序运行查找,但是当我运行它并期望从行中得到 3 个结果时,它会给我 2 个。例如,如果我要求用户输入一个位置并且该位置有 6 个玩家,它会给我 5 个玩家而不是 6 个玩家。

这是我的代码

            if (!rs.next()) {


                System.out.println("There is no match in the database " + position);
            }
            else {
                System.out.println(String.format("  " + " %-19s %s", "Name",
                        "Rank") + "\t" + String.format("%-11s %s", "Position",
                                "School") + "\t" + String.format("%-6s %s",
                                        "Age", "War") + "\n-----------------"
                        + "-------------------------------------------------");
                while (rs.next()) {
                    int rank = 0, age = 0;
                    String name = null, pos = null, sch = null;
                    double war = 0;
                    for (int i = 1; i < colum - 1; i++) {

                        rank = rs.getInt(1);
                        name = rs.getString(2);
                        pos = rs.getString(3);
                        sch = rs.getString(4);
                        age = rs.getInt(5);
                        war = rs.getDouble(6);
                    }

When I run my java code I get this result. It's not getting the first index in the list

When I call my stored procedure in MYSQL Workbench, I get this result

【问题讨论】:

    标签: java mysql jdbc netbeans-8


    【解决方案1】:

    您已阅读

    中的第一个索引
        if (!rs.next()) {
    

    本身。这会将光标移动到下一行。您必须删除它,它会给出所有行。

    【讨论】:

    • 太棒了!如果你愿意,你可以接受答案。
    猜你喜欢
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    相关资源
    最近更新 更多