如何从我的数据库中的第一条记录跳转到最后一条记录

时间:2014-01-02 17:42:20

标签: java postgresql jdbc

我有这段Java代码,我尝试遍历记录,意味着我想从第一个记录跳到最后一个记录,反之亦然。你可以看到'n'在记录中向前迭代,'p'向后迭代。一旦我到达最后一条记录/第一条记录,就会抛出异常。我该如何解决这个问题?

谢谢你的帮助: - )

public void NavigateTable() {
    try {
        statement = dbconnect.connect.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs = statement.executeQuery("SELECT * FROM author");
        char selection;
        rs.beforeFirst();
        do {
            System.out.println("(n)ext data, (p)revious data or (e)xit.");
            selection = readChar("");
            if (selection == 'n') {
                rs.next();
                System.out.print("AuthorID: " + rs.getInt("authorid"));
                System.out.print("\tFirst name: " + rs.getString("firstname"));
                System.out.print("\tLast name: " + rs.getString("lastname"));
                System.out.print("\tPublisherID: " +rs.getInt("publisherid")); 
                System.out.println();      
            } else if (selection == 'p') {
                rs.previous();
                System.out.print("AuthorID: " + rs.getInt("authorid"));
                System.out.print("\tFirst name: " + rs.getString("firstname"));
                System.out.print("\tLast name: " + rs.getString("lastname"));
                System.out.print("\tPublisherID: " +rs.getInt("publisherid")); 
                System.out.println();   
            }

        } while (selection != 'e'); 
    }
    catch (Exception e) {
        System.out.println("Exception " + e.getMessage());
    }

}

3 个答案:

答案 0 :(得分:1)

你应该立即阅读所有记录,并将它们放在像ArrayList这样的地方,你可以轻松地从头到尾跳转,反之亦然。这也允许您释放数据库资源,这是一件好事。

答案 1 :(得分:0)

你盲目地滚动ResultSet的两端。您需要使用isFirst()isLast()方法检查您是在开始还是结束,然后避免在这些情况下调用next()previous()

答案 2 :(得分:0)

通常,您不希望在用户与系统交互时打开ResultSet(即,从用户读取输入以确定是向前还是向后)。

话虽这么说,为了解决你的问题,你可以尝试这样的事情:

public void NavigateTable() {
    try {
        statement = dbconnect.connect.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs = statement.executeQuery("SELECT * FROM author");
        char selection;
        rs.beforeFirst();
        do {
            System.out.println("(n)ext data, (p)revious data or (e)xit.");
            selection = readChar("");
            if (selection == 'n') {
                if (rs.isLast()) rs.beforeFirst();
                else rs.next();
                System.out.print("AuthorID: " + rs.getInt("authorid"));
                System.out.print("\tFirst name: " + rs.getString("firstname"));
                System.out.print("\tLast name: " + rs.getString("lastname"));
                System.out.print("\tPublisherID: " +rs.getInt("publisherid")); 
                System.out.println();      
            } else if (selection == 'p') {
                if (rs.isFirst()) rs.afterLast();
                else rs.previous();
                System.out.print("AuthorID: " + rs.getInt("authorid"));
                System.out.print("\tFirst name: " + rs.getString("firstname"));
                System.out.print("\tLast name: " + rs.getString("lastname"));
                System.out.print("\tPublisherID: " +rs.getInt("publisherid")); 
                System.out.println();

            }