ResultSet.Next()是否跳过行?

时间:2015-03-01 14:05:42

标签: java sqlite

我正在使用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循环在退出之前只进行一次迭代。

有关为何忽略最后一个表的任何建议?

1 个答案:

答案 0 :(得分:0)

根据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中的所有表。