PreparedStatement.addBatch()可用于SELECT查询吗? [英] Can PreparedStatement.addBatch() be used for SELECT queries?

查看:574
本文介绍了PreparedStatement.addBatch()可用于SELECT查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我有100个SELECT查询因一个输入而不同。 PreparedStatement可用于该值。

Imagine that I have 100 SELECT queries that differ by one input. A PreparedStatement can be used for the value.

我在Web上看到的所有文档都是批量插入/更新/删除。我从未见过用于选择语句的批次。

All the documentation I see on the Web is for batch insert/update/delete. I have never seen batches used for select statements.

可以这样做吗?如果是这样,请在下面的示例代码中帮助我。

Can this be done? If so, please help me when the below sample code.

我想这可以使用IN子句来完成,但我更喜欢使用批处理的select语句。

I suppose this can be done using an "IN" clause, but I would prefer to use batched select statements.

示例代码:


public void run(Connection db_conn, List value_list) {
    String sql = "SELECT * FROM DATA_TABLE WHERE ATTR = ?";
    PreparedStatement pstmt = db_conn.prepareStatement(sql);
    for (String value: value_list) {
        pstmt.clearParameters();
        pstmt.setObject(1, value);
        pstmt.addBatch();
    }
    // What do I call here?
    int[] result_array = pstmt.executeBatch()
    while (pstmt.getMoreResults()) {
        ResultSet result_set = pstmt.getResultSet();
        // do work here
    }
}

我想这可能也依赖于驱动程序行为。我正在使用他们的JDBC驱动程序编写针对IBM AS / 400 DB2数据库的查询。

I suppose this may also be driver-dependent behaviour. I am writing queries against IBM AS/400 DB2 database using their JDBC driver.

推荐答案

参见文档


此列表可能包含更新,插入或删除行的语句;它还可能包含DDL语句,如CREATE TABLE和DROP TABLE。但是,它不能包含将生成ResultSet对象的语句,例如SELECT语句。换句话说,列表只能包含产生更新计数的语句。

This list may contain statements for updating, inserting, or deleting a row; and it may also contain DDL statements such as CREATE TABLE and DROP TABLE. It cannot, however, contain a statement that would produce a ResultSet object, such as a SELECT statement. In other words, the list can contain only statements that produce an update count.

列表在创建时与Statement对象关联,最初为空。您可以使用addBatch方法将SQL命令添加到此列表中。

The list, which is associated with a Statement object at its creation, is initially empty. You can add SQL commands to this list with the method addBatch.

这篇关于PreparedStatement.addBatch()可用于SELECT查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆