是否可以在 java 中传递结果集?

我正在尝试创建一个命令,我可以将不同的查询发送到 SQLite / Mysql DB,并将结果集返回给正在调用的任何函数。它需要能够处理是 2 列还是 15 列。


以下不起作用 - 大概是因为它关闭了结果集/连接,但我不知道该怎么做。


想法?


public static ResultSet queryDB(String query) {

    try {

        Connection connection = DriverManager.getConnection("jdbc:sqlite:" + Settings.SQLITE_DB_PATH);

        Statement statement = connection.createStatement();

        ResultSet resultSet = statement.executeQuery(query);

        resultSet.close();

        statement.close();

        connection.close();

        return resultSet;

    } catch (SQLException ex) {

        Logger.getLogger(SQLInterp.class.getName()).log(Level.SEVERE, null, ex);

    }

    return null;

}


慕无忌1623718
浏览 66回答 1
1回答

慕斯709654

你基本上有3个选择:不要关闭方法中的ResultSet,Statement和Connection,将执行此操作的责任移交给调用者。不推荐,因为它容易出错,并且会破坏格式良好的代码结构范例。按照Jacob G.的建议,传入具有处理数据所需逻辑的对象。例如使用 Java 8+&nbsp;Consumer:public static void queryDB(String query, Consumer<ResultSet> processor) {&nbsp; &nbsp; try (&nbsp; &nbsp; &nbsp; &nbsp; Connection connection = DriverManager.getConnection("jdbc:sqlite:" + Settings.SQLITE_DB_PATH);&nbsp; &nbsp; &nbsp; &nbsp; Statement statement = connection.createStatement();&nbsp; &nbsp; &nbsp; &nbsp; ResultSet resultSet = statement.executeQuery(query);&nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; processor.accept(resultSet);&nbsp; &nbsp; } catch (SQLException ex) {&nbsp; &nbsp; &nbsp; &nbsp; Logger.getLogger(SQLInterp.class.getName()).log(Level.SEVERE, null, ex);&nbsp; &nbsp; }}然后像这样调用它:SQLInterp.queryDB("SELECT * FROM foo", rs -> {&nbsp; &nbsp; while (rs.next()) {&nbsp; &nbsp; &nbsp; &nbsp; // process data here&nbsp; &nbsp; }});以通用数据结构将所有数据读入内存,例如List<Map<String, Object>>:这当然假设查询对每一列都有很好的唯一名称。public static List<Map<String, Object>> queryDB2(String query) {&nbsp; &nbsp; try (&nbsp; &nbsp; &nbsp; &nbsp; Connection connection = DriverManager.getConnection("jdbc:sqlite:" + Settings.SQLITE_DB_PATH);&nbsp; &nbsp; &nbsp; &nbsp; Statement statement = connection.createStatement();&nbsp; &nbsp; &nbsp; &nbsp; ResultSet resultSet = statement.executeQuery(query);&nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; ResultSetMetaData metaData = resultSet.getMetaData();&nbsp; &nbsp; &nbsp; &nbsp; int columnCount = metaData.getColumnCount();&nbsp; &nbsp; &nbsp; &nbsp; String[] name = new String[columnCount];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < columnCount; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name[i] = metaData.getColumnLabel(i + 1);&nbsp; &nbsp; &nbsp; &nbsp; List<Map<String, Object>> rows = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; while (resultSet.next()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, Object> row = new LinkedHashMap<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < columnCount; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row.put(name[i], resultSet.getObject(i + 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rows.add(row);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return rows;&nbsp; &nbsp; } catch (SQLException ex) {&nbsp; &nbsp; &nbsp; &nbsp; Logger.getLogger(SQLInterp.class.getName()).log(Level.SEVERE, null, ex);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java