Java 如何创建一组字符串,其中每个字符串都有1000个要在Oracle查询表达式中使用的客户id
我的输入为客户编号CSR\U ID BigDecimal11,0。以下代码失败,因为Oracle查询表达式的限制为1000:Java 如何创建一组字符串,其中每个字符串都有1000个要在Oracle查询表达式中使用的客户id,java,oracle,Java,Oracle,我的输入为客户编号CSR\U ID BigDecimal11,0。以下代码失败,因为Oracle查询表达式的限制为1000: //Start of the code String cust_ids = null; int cnt_cust = 0; java.util.StringJoiner joiner = new java.util.StringJoiner(","," ( "," ) "); //This is a loop which gets CSR_ID as in
//Start of the code
String cust_ids = null;
int cnt_cust = 0;
java.util.StringJoiner joiner = new java.util.StringJoiner(","," ( "," ) ");
//This is a loop which gets CSR_ID as input:
joiner.add(row5.CSR_ID.toString());
cnt_cust++;
//End of the code
System.out.println(joiner); // (1,2,3,...,100045)
cust_ids = joiner.toString();
查询如下所示:
"select col1, col2 from customers where cust_id in " +
"(1,2,3,...,100045)";
我想将其拆分如下:
"select col1, col2 from customers where" +
" cust_id in (1,2,3...,1000)" +
" or cust_id in (1001,.....2000)" +
..... ;
如何以1000个customer-id的批量构建此查询表达式。
注意:客户id不是连续的,而是随机值 我认为您可以利用以下多列比较:
select col1, col2 from customers where cust_id in
(1,2,3,...,100045); -- This will fail
select col1, col2 from customers where (cust_id,1) in
((1,1),(2,1),(3,1),...,(100045,1)); -- This will work
干杯 一个没有优化的解决方案如下所示,我认为在Java8中使用Stream有更优雅的方法 其概念是将客户ID转换为列表,然后您可以使用所需的块大小划分列表
int blockSize = 1000;
List<String> cust_idsList = Arrays.asList(cust_ids.substring(1, cust_ids.length()-1).split(","));
System.out.println(cust_idsList.size());
StringBuilder sb = new StringBuilder();
sb.append("select col1, col2 from customers where cust_ids in (");
for (int i = 0; i < cust_idsList.size(); i++) {
sb.append(cust_idsList.get(i)).append(",");
if (i !=0 && i%blockSize == blockSize-1) {
sb.setLength(sb.length()-1);
if (i != cust_idsList.size()-1) {
sb.append(")\n or cust_ids in (");
}
}
}
sb.setLength(sb.length()-1);
sb.append(")");
System.out.println(sb.toString());
谢谢,我想用java构建这个字符串。如何做到这一点。不是Java专家。很抱歉不要使用字符串连接来构建查询,请使用参数化查询,如果需要传递值数组,请绑定或使用临时表。