微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将string数组转化为sql的in条件用sql查询

例如:我想将String[] str = {"4","5","6"}转化为“‘4',‘5',‘6'”字符串。这样我就可以用SQL查询:select * from tableName id in (字符串)了。 项目中实现的源码如下:
<div class="codetitle"><a style="CURSOR: pointer" data="12522" class="copybut" id="copybut12522" onclick="doCopy('code12522')"> 代码如下:

<div class="codebody" id="code12522">
StringBuffer idsstr = new StringBuffer();
for (int i = 0; i < ids.length; i++) {
if (i > 0) {
idsstr.append(",");
}
idsstr.append("'").append(ids[i]).append("'");
}

我自己想到的另一种方式实现如下:
<div class="codetitle"><a style="CURSOR: pointer" data="4236" class="copybut" id="copybut4236" onclick="doCopy('code4236')"> 代码如下:
<div class="codebody" id="code4236">
public static String stringArray2Strin(String[] str) { StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length; i++) {
sb.append("'").append(str[i]).append("'").append(",");
}
return sb.toString().substring(0,sb.length() - 1);
} public static void main(String[] args) {
String[] str = { "4","6" };
System.out.println(ItemGroupService.stringArray2String(str));
}

原文地址:https://www.jb51.cc/mssql/63268.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐