0
点赞
收藏
分享

微信扫一扫

PreparedStatement设值where in语句

情景 :select * from table where id in(1,2,3);

程序实现:

String sql ="select * from table where id in (?)";
try{
PreparedStatement ps = conn.prepareStatement(sql);
}
ps.setString(1, ids);
ps.executeUpdate()

执行以上代码会发现得不到自己想要的结果,原因是什么呢?
ids=“1,2,3”
ps.setString 会将参数加入相应的单引号,sql语句为 select * from table where id in (‘1,2,3’);

解决方法:

一、最原始最简单的就是拼sql 语句:“select * from table where id in ( “+ids+”);”;

二、将值以数组的形势传入,再进行设置

for(int i=0;i<params.size();i++)
{
ps.setString(i+1, params.get(i).toString());
}


举报

相关推荐

0 条评论