如题 我要查询一大堆东西 
String dept="select dept_name from dept; _";
String cn="select count(*) from emp where emp_dept=? and to_char(emp_joindate,'yyyy-mm')<'"+years+"-"+month+"'; _";
String ce="SELECT COUNT(*) FROM emp WHERE to_char(emp_joindate,'yyyy-mm') ='"+years+"-"+month+"' and emp_dept=?; _";
String cd="select count(*) from el where to_char(el_date,'yyyy-mm')='"+years+"-"+month+"' and emp_dept=?; _";
String cf="select count(*) from dr where to_char(dr_date,'yyyy-mm')='"+years+"-"+month+"' and dr_dept_a=?; _";//dr_depr_a是名称
String co="select count(*) from dr where to_char(dr_date,'yyyy-mm')='"+years+"-"+month+"' and dr_dept_f=?; _";//dr_depr_a是名称
String cgs="select count(*) from emp where to_char(emp_jiondate,'yyyy-mm')='"+years+"-"+month+"' and emp_dept=? and emp_education='研究生'; _";
String cuc="select count(*) from emp where to_char(emp_jiondate,'yyyy-mm')='"+years+"-"+month+"' and emp_dept=? and emp_education='本科生'; _";
String cjc="select count(*) from emp where to_char(emp_jiondate,'yyyy-mm')='"+years+"-"+month+"' and emp_dept=? and emp_education='大专生'; _";
String cshs="select count(*) from emp where to_char(emp_jiondate,'yyyy-mm')='"+years+"-"+month+"' and emp_dept=? and emp_education='高中级以下'";
String sql=dept+cn+ce+cd+cf+co+cgs+cuc+cjc+cshs;应该怎么连接 才能让这个包含所有sql语句的sql字符串顺利执行

解决方案 »

  1.   

    把这些sql放在字符串数组中 然后遍历执行
      

  2.   

    最好是能在这些表间建立关联 其他的嘛还可以用union all 
      

  3.   

    楼主 每句末尾加的"_"是为了split的吗? 如果是这样的话就全加 然后split分割。遍历执行。结果放在List或其他集合容器里。
      

  4.   

    遍历执行等于说还是一条一条的执行!   像这种多条的就算你放到sql里面他也是一条一条的来的!    因为没有必要的关联的关系,就算你能执行,那你的resultset怎么取值?   怎么去判断?    如果有关联的话还好说。
      

  5.   

    没有办法吧,就是一块传过去,最后sql还是逐条执行的
    更新系的倒是有executeBatch()的,
    查询系执行逐条执行,然后对每个返回集再add之类的。
      

  6.   

    union all 是需要select的列名和列数是一致的,
    感觉也没必要。
      

  7.   

    写一个存储过程,里面有多条sql语句,只要exec调用过程就可以解决问题了
      

  8.   

    查询出来resultset也是个问题,遍历的时候取值的类型也不同
      

  9.   

    取值我用rs=pstmt.getMoreResults()取就是需要逐条执行 但是又不想一个语句写一套 代码太多
      

  10.   

    楼主 其实如果你知道每条sql语句查的是什么,即知道第几条是什么。每次取值也尚可接受,到时候程序中来个顺序或者ArrayList按指定顺序取值。你遍历的时候除了第一个为dept_name外。剩下的直接放到List。就是一个个数,你也知道List中索引是xx的数是第几个sql查询出的。呵呵。虽然易用性很差,但是代码不多。
      

  11.   

    额 我就是这么想的 但是sql语句怎么连接不知道 所以做不出来啊
      

  12.   

    存储过程,返回一个array,搞定!
      

  13.   


    你那些sql字段基本一样的。多数都是参数传的不一样,完全可以通过一条sql分组得到你要的数据,这样写来,你的sql可以减少为5句各自执行 也没那么多了啊。
    如果部分表即使没有外键关联, 但是数据是关联关系数据的话,改成连接查询,估计能缩小到一两条sql!!!