簡體   English   中英

ResultSet.Next()是否跳過行?

[英]Does ResultSet.Next() skip rows?

我正在使用Java中的ResultSet.next()方法遇到問題。 以下代碼連接到SQLite數據庫文件,並嘗試讀取db中包含的所有表。

    // Set the connection up with jdbc and sqlite.
    Connection connection = DriverManager.getConnection("jdbc:sqlite:file.db");
    Statement statement = connection.createStatement();
    statement.setQueryTimeout(30);

    // Get all the tables in the DB so we can check that we have all we need.
    ResultSet trs = statement.executeQuery("SELECT name FROM sqlite_master  WHERE type='table';");
    while (trs.next()) {
        String tblname = trs.getString("name").toLowerCase();
        log.log(Level.INFO, "Found table: " + tblname);
    }

該數據庫包含2個表(通過在sqlite3客戶端中運行查詢進行驗證),但while循環僅在退出之前執行一次迭代。

關於為什么最后一個表被忽略的任何建議?

根據Holgers的建議(參見問題評論),我轉而使用API​​方法getTables()

// Set the connection up with jdbc and sqlite.
Connection connection = DriverManager.getConnection("jdbc:sqlite:file.db");

// Get the tables.
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet trs = dbmd.getTables(null, null, "%", null);
while (trs.next()) {
            String tblname = trs.getString("TABLE_NAME").toLowerCase();
            log.log(Level.INFO, "Found table: " + tblname);
}

這解決了我的問題,沒有得到ResultSet中的所有表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM