jdbc Mysql嵌套的结果集

时间:2016-01-23 21:08:58

标签: java mysql resultset

见下文。 rs.getString("tags")是超过1行的子查询。我想迭代那个子查询(rs.getString(“tags”)----喜欢rs.next()

while (rs.next()) {

        emailDto emaildto = new emailDto();         

        emaildto.setMid(rs.getInt("id"));
        emaildto.setSub(rs.getString("sub"));
        emaildto.setMessage(rs.getString("message"));

         while(rs.getString("tags").next()){

            arrtags[i] = rs.getString(1);
         }

        emaildto.setTags(arrtags);

rs.getString("tags")不起作用 - 并且包含多于1行。如何提取它。有没有技术?

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

Array tagsArray = rs.getArray(“tags”); String [] tags =(String [])tagsArray.getArray();

答案 1 :(得分:0)

这不是答案。这是完整的代码

列出emails = new ArrayList();

    String listQuery = "select mid, sub, message, "
    + " (select emailid from sub_ids where sub_ids.messageid= sub_mail_list.mid )  //  this query fetch more than one row. 

作为refid"         +"来自sub_mail_list";

    PreparedStatement ps = null;
    ResultSet rs;
    try {

        ps = DatabaseConnectionUtil.getConnection().prepareStatement(
                listQuery);



         rs = ps.executeQuery(listQuery);

        while (rs.next()) {
            emailDto emaildto = new emailDto();         

            emaildto.setMid(rs.getInt("mid"));
            emaildto.setSub(rs.getString("sub"));
            emaildto.setMessage(rs.getString("message"));

            Array tagsArray =  rs.getArray("refid");

        List<vtbDto> vtbdtosvr = new ArrayList<vtbDto>();



        int[] tags = (int[])tagsArray.getArray();

            for (int i = 0; i < tags.length; i++) {


                vtbDto vtbdto = new vtbDto();



                vtbdto.setRefid(tags[i]);

                vtbdtosvr.add(vtbdto);

            }







            emaildto.setAr(tagsArray);

            emails.add(emaildto);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        DatabaseConnectionUtil.closeAll(ps);
    }

    return emails;

这是打印功能的代码

列出emailDtos = emaildao.getAllemails();

            for (emailDto emailDto2 : emailDtos) {

                  System.out.println( emailDto2.getMid());
                  System.out.println( emailDto2.getSub());
                  System.out.println( emailDto2.getMessage());



                  List<vtbDto> vtbdtos= emailDto2.getVtbdtolst();


                  for (vtbDto vtbdto2 : vtbdtos) {
                      System.out.print(vtbdto2.getRefid() + ", ");
                  }


               }

并且控制台打印&#34;子查询返回超过1行&#34 ;;