简体   繁体   English

删除数据库中间的一些行后,有什么方法可以滚动 SQL 中的所有行?

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

I'm trying to scroll all of my data base (for some action like show all constants and etc. ) every thing is fine till i deleted some rows in middle of my SQL database.我正在尝试滚动我的所有数据库(对于某些操作,例如显示所有常量等),一切都很好,直到我删除了 SQL 数据库中间的一些行。 i use person id as my primary key and trying to scroll data with that key.我使用个人 ID 作为我的主键并尝试使用该键滚动数据。 personId is also auto Incremental. personId 也是自动增量的。

this is my data base before delete and i can scroll fine这是我删除前的数据库,我可以很好地滚动

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

and this is my database after delete这是我删除后的数据库

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

I'm already tried to:我已经尝试过:

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.但他们的逻辑是错误的,有些甚至行不通。

This is the code to retrieve a specific row of SQL data base.这是检索 SQL 数据库的特定行的代码。 Where my main problem originated (=starts).我的主要问题起源于哪里(=开始)。

Person class include personId and name and family property and there Getter and Setter method. Person class 包括 personId 和 name 和家庭财产,还有 Getter 和 Setter 方法。 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();
        }
    }
}

This is the displayAllRows() that going to invoke getRow(int rowNumber) for 1 to end as rowNumber.这是 displayAllRows(),它将调用 getRow(int rowNumber) for 1 以作为 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这是人 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.但不要忘记所有这一切的主要问题和主要目标。

I expect to retrieve all of data but instead i retrieve defected data and Row not found exception message.我希望检索所有数据,但我检索有缺陷的数据和 Row not found 异常消息。

I expect this:我期望这个:

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

But instead i get this:但相反,我得到了这个:

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

And 2 times "Row not found" Error.和 2 次“未找到行”错误。

The cause is here.原因就在这里。 You use rowNumber to search for personId.您使用 rowNumber 来搜索 personId。 You should not do any assumptions about what values have rowNumber.您不应该对哪些值具有 rowNumber 进行任何假设。 Instead, search by personId.相反,按 personId 搜索。

After lots of search i finally find my answer.经过大量搜索,我终于找到了答案。

You can use resultSet.absolute(row_number) method for this goal.您可以为此目标使用 resultSet.absolute(row_number) 方法。 Write your getRow() method body like this:像这样编写你的 getRow() 方法体:

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;
    }
}

Of course you will retrieve all of your database in this way.当然,您将以这种方式检索所有数据库。 but it work fine for data base as big as mine (;但它适用于像我这样大的数据库(;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM