I am passing a parameter to a PreparedStatement like this :

我将参数传递给PreparedStatement,如下所示:

public void getNodes(String runId, File file, Connection conn) {
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        ps = conn.prepareStatement(Mat.queries.get("NettingNode.QUERY"));

        ps.setString(1, runId);
        ps.setFetchSize(10000);
        rs = ps.executeQuery();

        // etc.
    } catch (Exception e) {
        logger.error(e, e);
    } finally {
        close(rs, ps);
    }
}

And the query looks like this :

查询看起来像这样:

select * from table_1 where run_id = ?

Now I want to modify my query like this, and reuse the first parameter (both ? would use the runId parameter) :

现在我想像这样修改我的查询,并重用第一个参数(两个?将使用runId参数):

select * from table_1 where run_id = ?
union
select * from table_2 where run_id = ?

Is that possible without doing this :

没有这样做可能是这样的:

ps.setString(1, runId);
ps.setString(2, runId);

1 个解决方案

#1


9

This cannot be done with plain JDBC. You could instead use Spring's JDBCTemplate, which would support what you want with Named Parameters, and re-use the same name wherever it's needed in the statement.

使用普通JDBC无法做到这一点。您可以改为使用Spring的JDBCTemplate,它可以支持命名参数所需的内容,并在语句中的任何位置重用相同的名称。

See Named parameters in JDBC

请参阅JDBC中的命名参数

更多相关文章

  1. LINUX下用SHELL脚本执行带输入输出参数的ORACLE存储过程并得到结
  2. C#中操作Oracle时的SQL语句参数的用法
  3. 彻底理解初始化参数SERVICE_NAMES和客户端TNS中的SERVICE_NAME
  4. sql 存储过程参数为空则不作为条件
  5. mysql参数优化辅助工具之tuning-primer.sh
  6. MSSQL中类似MySQL的limit参数
  7. 求助,关于sql带入参数的写法问题。
  8. 参数化的Insert语句,事务抛出错误
  9. 即使提供了参数,过程也需要参数

随机推荐

  1. Android Studio 依赖添加
  2. Android 解码MediaCodec 播放H264 265
  3. 第五章 以数据为中心—数据存取(3)
  4. android获取应用名称,判断应用是否安装
  5. Android Partitions Explained: boot, sy
  6. Banner
  7. Android简单的下拉刷新(谷歌官方)
  8. ubuntu android交叉编译c程序 ndk
  9. Android 长按setOnItemLongClickListener
  10. Android电话录音