![](/img/trans.png)
[英]Is there any way to convert derby database table rows to SQL insert statements
[英]Is there any way to scroll all row in SQL after delete some rows in middle of my database?
我正在嘗試滾動我的所有數據庫(對於某些操作,例如顯示所有常量等),一切都很好,直到我刪除了 SQL 數據庫中間的一些行。 我使用個人 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.
但他們的邏輯是錯誤的,有些甚至行不通。
這是檢索 SQL 數據庫的特定行的代碼。 我的主要問題起源於哪里(=開始)。
Person class 包括 personId 和 name 和家庭財產,還有 Getter 和 Setter 方法。 您可以在頁面末尾看到它。
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(),它將調用 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);
}
}
這是人 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;}
}
我知道還有其他更好的方法來顯示數據。 但不要忘記所有這一切的主要問題和主要目標。
我希望檢索所有數據,但我檢索有缺陷的數據和 Row not found 異常消息。
我期望這個:
+-------------------------+
| Row | name | family |
+-------------------------+
| 1 | name1 | family1 |
| 2 | name3 | family3 |
| 3 | name5 | family5 |
| 4 | name6 | family6 |
+-------------------------+
但相反,我得到了這個:
+-------------------------+
| Row | name | family |
+-------------------------+
| 1 | name1 | family1 |
| 3 | name5 | family5 |
+-------------------------+
和 2 次“未找到行”錯誤。
原因就在這里。 您使用 rowNumber 來搜索 personId。 您不應該對哪些值具有 rowNumber 進行任何假設。 相反,按 personId 搜索。
經過大量搜索,我終於找到了答案。
您可以為此目標使用 resultSet.absolute(row_number) 方法。 像這樣編寫你的 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;
}
}
當然,您將以這種方式檢索所有數據庫。 但它適用於像我這樣大的數據庫(;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.