【问题标题】:"ResultSet not positioned properly" Called .next() but still doesn't work, please advise?“ResultSet 未正确定位”调用 .next() 但仍然无法正常工作,请指教?
【发布时间】:2019-04-13 11:56:05
【问题描述】:

我正在尝试从使用 postgresql 创建的数据库中提取有关歌曲的数据。我一直收到这个错误,我不知道该怎么做,因为我调用了 resultSet.next()。 有什么建议吗?

     String SQL = "SELECT trackName, albumName FROM tracks, artists, 
     albums WHERE 'song' = tracks.trackname AND tracks.mainartist = 
     artists.artistname AND albums.artist = artists.artistname";

    try
    {
        org.postgresql.Driver.isRegistered();
        Class.forName("org.postgresql.Driver");
        Connection connection = DriverManager.getConnection(url,username,password); //creates a new connection

        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(SQL);
        resultSet.next();


        String album = resultSet.getString(3);
        String artist = resultSet.getString(4);
        String trackName = resultSet.getString(2);
        String feat = resultSet.getString(5);
        String date = resultSet.getString(6);

        console.println("");
        console.println("");
        console.println("Track Name: "+trackName);
        console.println("");
        console.println("Track Album: "+album);
        console.println("");
        console.println("Track Artist(s): "+artist);
        console.println("");
        console.println("Featuring: "+feat);
        console.println("");
        console.println("Track Release Date: "+date);
        console.println("");
        console.println("\n");

    }
    catch(Exception e)
    {
        console.println("SORRY, COULD NOT CONNECT YOU TO THE DATABASE.");
        console.println(e);
        console.println("");
        e.printStackTrace();
    }

我正在终端中打印出来:

    SORRY, COULD NOT CONNECT YOU TO THE DATABASE.
    org.postgresql.util.PSQLException: ResultSet not positioned properly, 
    perhaps you need to call next.

这是堆栈跟踪:

    org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
at org.postgresql.jdbc.PgResultSet.checkResultSet(PgResultSet.java:2772)
at org.postgresql.jdbc.PgResultSet.getString(PgResultSet.java:1894)
at Info.songInfo(Info.java:49)
at __SHELL14.run(__SHELL14.java:5)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at bluej.runtime.ExecServer$3.run(ExecServer.java:752)

【问题讨论】:

    标签: java sql postgresql


    【解决方案1】:

    在尝试从一行中检索信息之前,您应该始终检查来自next 的返回值,因为您可能不在一行中。来自the JavaDoc

    如果新的当前行有效,则返回:truefalse 如果没有更多行了

    我怀疑您的查询与任何行都不匹配,所以 next 返回 false,但随后您尝试从不存在的行中读取。

    所以在你的情况下:

    if (resultSet.next()) {
        String album = resultSet.getString(3);
        // ...
    } else {
        // There were no matching rows
    }
    

    【讨论】:

    • 谢谢,我试过了,现在跳转到 else 语句,有什么线索吗?
    • 您的查询可能没有返回任何内容?在终端中通过 psql 手动运行它,并确保您确实得到了一些返回的行。
    • @GabePitchford - 因为您的查询没有返回任何内容。
    • 查询不应该返回trackname和albumname吗??
    • @GabePitchford - 仅当存在匹配行时。显然,没有。我建议遵循 Filip 的上述建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多