删除数据库中间的一些行后,有没有办法滚动 SQL 中的所有行?

Is there any way to scroll all row in SQL after delete some rows in middle of my database?

我正在尝试滚动我的所有数据库(对于某些操作,如显示所有常量等) 一切都很好,直到我删除了 SQL 数据库中间的一些行。 我使用 person id 作为我的主键并尝试使用该键滚动数据。 personId也是自增的。

这是我删除前的数据库,我可以很好地滚动

+------------------------------+
| personId |  name  |  family  |
+------------------------------+
| 1        | name1  | family1  |
| 2        | name2  | family2  |
| 3        | name3  | family3  |
| 4        | name4  | family4  |
| 5        | name5  | family5  |
| 6        | name6  | family6  |
+------------------------------+

这是我删除后的数据库

+------------------------------+
| personId |  name  |  family  |
+------------------------------+
| 1        | name1  | family1  | <-- num2 is deleted
| 3        | name3  | family3  | <-- num4 is deleted
| 5        | name5  | family5  |
| 6        | name6  | family6  |
+------------------------------+

我已经尝试过:

1. use ROW_NUMBER() function in WHERE clause.
2. reset personId number and auto incremental.
3. sort data by date created in Unix-time.

But the logic of them is wrong and some even not working.

这是检索 SQL 数据库中特定行的代码。我的主要问题起源于哪里 (=starts)。

Person class include personId and name and family property and there Getter and Setter method. you can see it at the end of page.

public static Person getRow(int rowNumber) throws SQLException {
    String sql = "SELECT * FROM person WHERE personId = ?";
    ResultSet rs = null;
    try (
            Connection con = DriverManager.getConnection(...)
            PreparedStatement stmt = con.prepareStatement(sql);
    ) {
        stmt.setInt(1, rowNumber);
        rs = stmt.executeQuery();
        if (rs.next()) {            
            Person bean = new Person(rowNumber, rs.getString("name"), 
                                  rs.getString("family"));
            return bean;
        } else {
            System.err.println("No rows were found!");
            return null;
        }
    } catch (SQLException e) {
        System.err.println(e);
        return null;
    } finally {
        if (rs != null) {
            rs.close();
        }
    }
}

这是 displayAllRows(),它将为 1 调用 getRow(int rowNumber) 以作为 rowNumber 结束。

public static void displayAllRows() throws SQLException {
    String sql = "SELECT * FROM profiles";
    try (
            Connection connection = DriverManager.getConnection(...)
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
    ) {
        int rowNumber = 0;
        while (resultSet.next()) {
            Person bean = getRow(++rowNumber);
            System.out.println(bean.personId + ". " + bean.name + "  " + bean.family); 
        }

    } catch (SQLException e) {
        System.err.println(e);
    }
}

Here's the person class

public class Person {

    private int personId;
    private String name;
    private String family;

    public Person(int personId, String name, String family) {
        this.personId = personId;
        this.name = name.trim();
        this.family = family.trim();
    }

    public Person() {}

    public int getPersonId() {return personId;}
    public void setPersonId(int personId) {this.personId = personId;}
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    public String getFamily() {return family;}
    public void setFamily(String family) {this.family = family;}     
}

I know that there is other and better way to display data. But don't forget the main question and main goal of all of this.

我希望检索所有数据,但我检索到有缺陷的数据和行未找到异常消息。

我希望这样:

+-------------------------+
| Row |  name  |  family  |
+-------------------------+
| 1   | name1  | family1  |
| 2   | name3  | family3  |
| 3   | name5  | family5  |
| 4   | name6  | family6  |
+-------------------------+

但是我得到了这个:

+-------------------------+
| Row |  name  |  family  |
+-------------------------+
| 1   | name1  | family1  |
| 3   | name5  | family5  |
+-------------------------+

And 2 times "Row not found" Error.

原因就在这里。您使用 rowNumber 来搜索 personId。您不应该对哪些值具有 rowNumber 做任何假设。相反,按 personId 搜索。

经过大量搜索,我终于找到了答案。

You can use resultSet.absolute(row_number) method for this goal. Write your getRow() method body like this :

public static Person getRow(int rowNumber) throws SQLException {
    String sql = "SELECT * FROM profiles";
    try (
            Connection connection = DBUtil.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
    ) {
        if (resultSet.absolute(rowNumber)) {
            return new Person(resultSet.getInt("personId"), resultSet.getString("name"), 
                       resultSet.getString("family"), resultSet.getString("nationalId"));
        } else {
            System.err.println("No row were found !");
            return null;
        }
    } catch (SQLException e) {
        System.err.println(e);
        return null;
    }
}

当然,您将以这种方式检索所有数据库。但它适用于像我这样大的数据库 (;