简体   繁体   中英

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. i use person id as my primary key and trying to scroll data with that key. personId is also auto Incremental.

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. Where my main problem originated (=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();
        }
    }
}

This is the displayAllRows() that going to invoke getRow(int rowNumber) for 1 to end as 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.

I expect to retrieve all of data but instead i retrieve defected data and Row not found exception message.

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.

The cause is here. You use rowNumber to search for personId. You should not do any assumptions about what values have rowNumber. Instead, search by personId.

After lots of search i finally find my answer.

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

Of course you will retrieve all of your database in this way. but it work fine for data base as big as mine (;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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