爪哇 - prepared语句和数组 [英] Java - Prepared statements and arrays

查看:130
本文介绍了爪哇 - prepared语句和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何处理在prepared声明数组?即,我想做一个查询,我得到的一个参数是一个字符串数组,我想在查询中使用(不要选择有一个字段,其阵列中的行)?

How can I handle an array in a prepared statement? i.e, I want to do a query and one of the parameters I get is an array of strings which I want to use in the query (Don't select rows that have a field that's in the array)?

推荐答案

一些JDBC驱动程序可能已经(前JDBC 4)包含支持数组类型参数prepared陈述专有扩展 - 你需要用API咨询为了这。这意味着,你必须使用和操纵SQL类似数组的类型。

Some JDBC drivers may already (before JDBC 4) contain proprietary extensions that support array-type parameters in prepared statements - you would need to consult with API for this. This would mean that you have to use and manipulate array-like type in SQL.

一个解决将使用临时表。这是元步骤这样的解决方案:

One work around would be using temporary tables. These are meta-steps for such solution:


  1. BEGIN TRANSACTION(这个,如果你在里面事务是自动的
    方法 - EJB或Spring);

  2. 使用JDBC批量插入与prepared语句来创建和填充arrary元素的临时表(临时表必须有交易范围 - 这也是专有的数据库,但甲骨文至少支持);

  3. 构造所需的SQL,其中包括一个加盟临时表使用数组值(可以是明确的内部或外部JOIN或暗示的加入,例如使用EXISTS等);

  4. 提交(或应用程序异常回滚)的交易(这应该销毁临时表,并发事务应该有临时表的同名没有冲突)。

例如:在Ex pression被替换JOIN来临时表

Example: IN expression gets replaced with JOIN to temporary table.

这篇关于爪哇 - prepared语句和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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