如何将 ArrayList 绑定到 Oracle 中的PreparedStatement?
我想知道是否有一种方法可以将 ArrayList(或任何类型的 List)绑定到最终将用于访问 Oracle 数据库的PreparedStatement。 我发现:
这看起来很相似到我的问题,但这个问题更具体:我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法将列表绑定到准备好的语句中的单个参数。
使用列表中每个元素的参数标记生成 SQL,例如:
即使您将为每个查询生成一个新语句,我仍然建议使用
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:
Even though you'll generate a new statement for each query, I'd still recommend using a
PreparedStatement
. If your list containsString
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 cachePreparedStatements
, 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.你不能直接绑定它。 有一种方法可以将数组作为参数传递。 我不知道你想在数据库方面用它做什么,所以这可能对你没有帮助。
基本上,您必须在数据库中创建嵌套表类型; 基于该类型构建一个 Java 对象,其中包含数组中的数据; 并将其作为参数传递。
如果您在数据库中创建了这些对象:
那么您可以像这样编写 Java 代码:
Oracle 中的结果如下所示:
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:
Then you can write Java code like this:
The results in Oracle look like this:
好吧,从这个问题的答案来看,尤其是对我在该问题中的错误答案的评论,你不能。
请参阅 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.