我有一个sql语句:select count(*) from Cstcustom c, CstCustomManager cm where cm.userID = ? and cm.customID = c.ID and c.sourceid = ? and cm.customID not in (select cm2.customID from CstCustomManager cm2 where cm2.userID = ?)要求在find方法中传过来的是该sql和一个object[]参数,按以上语句,应该传过来的是一个含有三个元素的数组,分别代表三个?,但是在oracle中不能有null值,想对该语句进行处理,如果哪个参数是null的话,就针对某个对应的参数,将某个"= ?"替换为"is null"。请问代码应该如何实现。万分感谢。

解决方案 »

  1.   


    你把 
    cm.userID = ?
    做为 key 放到 map 中, obj[]相应的内容做为 value 放到 map中
      

  2.   

    StringBuilder sqlBuilder = new StringBuilder();
    sqlBuilder.append("select count(*) from Cstcustom c, CstCustomManager cm where cm.userID ");
    sqlBuilder.append(第一个参数 != null ? "= ? ": "is null ");
    sqlBuilder.append("and cm.customID = c.ID and c.sourceid ");
    sqlBuilder.append(第二个参数 != null ? "= ? ": "is null ");
    sqlBuilder.append("and cm.customID not in (select cm2.customID from CstCustomManager cm2 where cm2.userID ");
    sqlBuilder.append(第三个参数 != null ? "= ? ": "is null ");
    sqlBuilder.append(")");
    String sql = sql.toString();同样的  设参数的时候  也像 第一个参数 != null ? 参数值: "is null "
      应该是可以了
      

  3.   


    这个是一个方法啊,或者是你写一个if语句判断一下啊,楼主,你得到的参数,在方法当中对参数进入object[]里面的时候,进行判断一下,最多三个if语句就可搞定了,很简单的吧,
      

  4.   

    有参数就加到SQL里面,没参数就不加这个条件。把3个参数放到一个数组里就行了~!这个应该没有问题吧?肯定都是前台得到的参数。
      

  5.   


    public String find(String sql,Object[] args){
    StringBuilder result = new StringBuilder(sql);
    String regex = "=\\s*(\\?)";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(result);
    int n=0;//第几个
    while(m.find()){
    if(args != null && n < args.length&&args[n]!=null){
    result.delete(m.start(1), m.end(1));
    result.insert(m.start(1), args[n].toString());
    }else{
    result.delete(m.start(), m.end());
    result.insert(m.start(), "is null");
    }
    n++;
    }
    return result.toString();

    }
    ..........
    String sql = "select count(*) from Cstcustom c, CstCustomManager cm where cm.userID = ? and cm.customID = c.ID and c.sourceid = ? and cm.customID not in (select cm2.customID from CstCustomManager cm2 where cm2.userID = ?)";
    find(sql,new Object[]{1,2,null});
    .............................
      

  6.   

    楼上的方法,应该可以。 不能这样很麻烦,你还不如用object[]{} 直接拼成SQL 这样简单点。
      

  7.   


    语句我已自己写出来,但是没有用上,原因是我的考虑不全面。这个朋友写的方法和我写的差不多,但是还是有差别,前面可能我没有交待清楚,这个sql是会随意变动的,所以整体sql不能动,只能提取里面的“=?”,另其改为"is null"。
    谢谢各位的回复。
      

  8.   

    整体sql不能动是什么意思,不晓得还有什么要求,说详细点吧