简体   繁体   English

消除mysql n java中预准备语句中的引号

[英]Eliminating quotes in prepared statement in mysql n java

I have a prepared statement like this 我有这样一份准备好的声明

stmt = select * from table_name where id IN (?);

Once I pass the parameters the stmt looks like way 一旦我传递参数,stmt看起来就像

stmt = select * from table_name where id IN ('1,2,3');

There is no error while executing the query. 执行查询时没有错误。 However the resultset is returned only for the id=1 . 但是,仅为id=1返回resultset Is there some way I can eliminate the quotes / get the resultset for all these id's. 有什么方法可以消除引号/得到所有这些id的resultset

stmt = select * from table_name where id IN (?);

select GROUP_CONCAT(id) id from table ;

if(rs.next()){

    stmt.setString(1,rs.getString("id"));

    stmt.executeQuery();
}

Thanks in advance. 提前致谢。

It's not clear what the ID type is, but I believe you should actually be preparing a statement with each possible value as a separate parameter: 目前还不清楚ID类型是什么,但我相信你应该准备一个声明,将每个可能的值作为一个单独的参数:

select * from table_name where id IN (?, ?, ?)

Then add the three values for the three parameters. 然后为这三个参数添加三个值。 It's a common problem with parameterized SQL - when you want to be able to specify a variable number of values, you need to vary the SQL. 这是参数化SQL的常见问题 - 当您希望能够指定可变数量的值时,您需要改变SQL。 There may be a MySQL-specific way of coping with this (like table-valued parameters in SQL Server 2008 ) but I don't believe there's a generic JDBC way of doing this. 可能有一种特定于MySQL的方法来处理这种情况(比如SQL Server 2008中的表值参数 ),但我不相信有一种通用的JDBC方法可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM