如何使用WHERE x IN子句为PreparedStatement编写SQL? [英] How do you write the SQL for a PreparedStatement using a WHERE x IN clause?

查看:175
本文介绍了如何使用WHERE x IN子句为PreparedStatement编写SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下查询:

SELECT last_name,
       first_name,
       middle_initial
FROM   names
WHERE  last_name IN ('smith', 'jones', 'brown')

我需要能够在IN子句中对列表进行参数化,以将其写为JDBC PreparedStatement.该列表中可以包含任意数量的名称.

I need to be able to parameterize the list in the IN clause to write it as a JDBC PreparedStatement. This list could contain any number of names in it.

这是正确的方法:

SELECT last_name,
       first_name,
       middle_initial
FROM   names
WHERE  last_name IN (?)

然后建立参数列表?还是有更好(更正确)的方法来做到这一点?

and then build a list of parameters? Or is there a better (more correct) way to do that?

推荐答案

简而言之,您不能开箱即用.但是,使用Spring,您可以做自己想做的事情.参见如何在( ...)" Spring JdbcTemplate通过sql列表?

In short, you can't out of the box. However, with Spring you can do what you want. See How to generate a dynamic "in (...)" sql list through Spring JdbcTemplate?

这篇关于如何使用WHERE x IN子句为PreparedStatement编写SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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