`
zhaohuafei
  • 浏览: 27637 次
文章分类
社区版块
存档分类
最新评论

数组作为查询的参数

 
阅读更多

数组作为sql查询的一个参数,这个时候可以考虑把数组分开并拼在一条sql语句中。

public static void main(String[] args) {
    String sql="select * from user where 1=1";//目的是数组即使空也没关系
    String[] str=new String[]{"xxx","yyy"};
    sql+=" and name in (";
    for (int i = 0; i <str.length-1; i++) {
        sql+="'"+str[i]+"',";
    }
    sql+="'"+str[str.length-1]+"')";
    System.out.println(sql);
}
这样拼出来SQL如下:
select * from user where 1=1 and name in ('xxx','yyy')


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics