如何将 ArrayList 绑定到 Oracle 中的PreparedStatement?

发布于 2024-07-08 19:48:19 字数 341 浏览 5 评论 0原文

我想知道是否有一种方法可以将 ArrayList(或任何类型的 List)绑定到最终将用于访问 Oracle 数据库的PreparedStatement。 我发现:

PreparedStatement IN 子句替代方案?

这看起来很相似到我的问题,但这个问题更具体:我想将 ArrayList 绑定到要在 Oracle 中使用的PreparedStatement,如果可能的话,这是如何完成的?

I was wondering if there was a way to bind an ArrayList (or any kind of List, for that matter) to a PreparedStatement which will eventually be used to access an Oracle database. I found:

PreparedStatement IN clause alternatives?

And that seems similar to my issue, but this question is more specific: I'd like to bind an ArrayList to a PreparedStatement to be used in Oracle, if it is possible, how is this accomplished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必你懂 2024-07-15 19:48:19

您无法将列表绑定到准备好的语句中的单个参数。

使用列表中每个元素的参数标记生成 SQL,例如:

SELECT NAME FROM ITEM WHERE ID IN (?, ?, ?, ?)

即使您将为每个查询生成一个新语句,我仍然建议使用 PreparedStatement。 如果您的列表包含 String 实例,您将获得必要的转义以防止 SQL 注入。

但即使它是安全类型,例如 Integer 对象,某些驱动程序或中间件也可以缓存 PreparedStatements,并在请求相同表单时返回缓存的实例。 当然,一些测试是必要的。 如果您的列表大小差异很大,您将有许多不同的语句,而实施不当的缓存可能无法处理这么多语句。

You can't bind a List to a single parameter in a prepared statement.

Generate SQL with the a parameter marker for each element in the list, for example:

SELECT NAME FROM ITEM WHERE ID IN (?, ?, ?, ?)

Even though you'll generate a new statement for each query, I'd still recommend using a PreparedStatement. If your list contains String instances, you'll get the necessary escaping to protect from SQL injection.

But even if it's a safe type, like Integer objects, some drivers or middleware can cache PreparedStatements, and return a cached instance if the same form is requested. Of course, some testing would be necessary. If your lists vary widely in size, you'll have many different statements, and a poorly-implemented cache might not be prepared to handle so many.

寂寞花火° 2024-07-15 19:48:19

你不能直接绑定它。 有一种方法可以将数组作为参数传递。 我不知道你想在数据库方面用它做什么,所以这可能对你没有帮助。

基本上,您必须在数据库中创建嵌套表类型; 基于该类型构建一个 Java 对象,其中包含数组中的数据; 并将其作为参数传递。

如果您在数据库中创建了这些对象:

CREATE OR REPLACE TYPE my_nested_table IS TABLE OF VARCHAR2(20);
CREATE TABLE my_table (a  my_nested_table) NESTED TABLE a STORE AS my_table_a;

那么您可以像这样编写 Java 代码:

String[] insertvalues = { "a", "b", "c" };
PreparedStatement p = conn.prepareStatement("INSERT INTO my_table VALUES( ? )");
ARRAY insertParameter = new ARRAY( a_desc, conn, insertvalues );
p.setArray( 1, insertParameter );
p.execute();

Oracle 中的结果如下所示:

dev> select * from my_table;

A
--------------------------------------------------------------------------------
MY_NESTED_TABLE('a', 'b', 'c')

You can't bind it directly. There is a way to pass an array as a parameter. I have no idea what you want to do with it on the database side so this may not help you.

Basically, you have to create a nested table type in the database; build a Java object based on that type, containing the data from your array; and pass that as the parameter.

If you created these objects in the database:

CREATE OR REPLACE TYPE my_nested_table IS TABLE OF VARCHAR2(20);
CREATE TABLE my_table (a  my_nested_table) NESTED TABLE a STORE AS my_table_a;

Then you can write Java code like this:

String[] insertvalues = { "a", "b", "c" };
PreparedStatement p = conn.prepareStatement("INSERT INTO my_table VALUES( ? )");
ARRAY insertParameter = new ARRAY( a_desc, conn, insertvalues );
p.setArray( 1, insertParameter );
p.execute();

The results in Oracle look like this:

dev> select * from my_table;

A
--------------------------------------------------------------------------------
MY_NESTED_TABLE('a', 'b', 'c')
别挽留 2024-07-15 19:48:19

好吧,从这个问题的答案来看,尤其是对我在该问题中的错误答案的评论,你不能。

请参阅 https://docs.oracle .com/javase/1.5.0/docs/guide/jdbc/getstart/mapping.html#996857

Well, judging by the answer to that one, especially the comments to my wrong answer in that question, you can't.

See https://docs.oracle.com/javase/1.5.0/docs/guide/jdbc/getstart/mapping.html#996857.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文