【问题标题】:How to extract the value from a resultset of two statements that are union如何从两个联合语句的结果集中提取值
【发布时间】:2011-04-16 14:14:01
【问题描述】:

我在 dao 类中有一个名为 "getDetails" 的方法。在这种方法中,我将两个表中的两个 select 语句合并在一起,这两个表具有几乎相同的字段,分别称为 "main shop" & "sub shop" 并将这些查询放入preparedstatement。然后我把preparedStatement放到resultSet中。

这个 "getDetials" 方法返回 "details",我将在另一个方法中使用它调用 "writefile" 的中介,以便在 microsoft word 中打印值,例如 "writefile(details)"。在"writefile" 方法中,有值的字符串,并将“details”的值放到相应的字符串中。并为每个值附加 "outputString"

在输入画面,用户可以勾选“子店铺”复选框,并填写“子店铺”检查后。如果他们不检查,他们只需要填写“主要详细信息”,而不是“子商店” .如果他们检查 "main shop",我只需要打印一封信。如果他们检查“子商店”并填写“子商店数量”,我需要打印字母编号等于“分店数量” + 一(主字母)的字母。字母格式完全相同,只是改变了“店铺名称”(主或子店铺)、“店铺银行代码”字段的值"(主店或子店)根据用户的选择。

在中介中,我根据“子商店”的数量循环“writefile(details)”

问题是不同字母的数据是相同的(只显示“总店”数据)虽然字母数量是正确的(例如,如果有两个“子商店”,它打印三个字母)。在“main shop”字母之后的后续字母中如何获取“subshops”的值被打印出来了。

这是“writefile”方法

if(flag){
this.getHeading();
StringBuffer outputString = new StringBuffer();
Date date = new Date();
 SimpleDateFormat formatter=new SimpleDateFormat("dd MMMMM,yyyy");
          String dateString=formatter.format(date);
          details.sysDate =dateString;
          String date1=(String)details.getSysDate();

if(details.getNo() != null){
no=details.getNo();
}else {no=" ";}
if(details.bankName() != null){
bname=details.getBankName();
}else {bname = " ";}

outputString.append('\"');
outputString.append(date1);
outputString.append('\"');
outputString.append(",");
outputString.append('\"');
outputString.append(no);
outputString.append('\"');
outputString.append(",");
outputString.append('\"');
outputString.append(bname);
outputString.append('\"');
outputString.append(",");
outputString.append('\"');

dos.writeBytes(outputString.toString());
dos.flush();
dos.close();
fos.close();
}

查询样本是

    public Details getDetails (String No, String LoginID, String LetterID)
    {
        connection = DBConnection.getConnection();
                StringBuffer query = new StringBuffer();
                query.append("select TO_CHAR(TRUNC(SYSDATE),'DD MONTH,YYYY'),a.no, b.bank_name");
                query.append("from t_newappl a,t_newappl_bank b where b.no = a.no and a.no=(select no from t_newappl whereno=?) and rownum=1 and status = 'PEND'");
                query.append(" union all ");
                query.append("select TO_CHAR(TRUNC(SYSDATE),'DD MONTH,YYYY'),a.no,b.bank_name");
                query.append("from t_newappl a,t_newappl_bank b, newappl_sub c, t_newappl_sub_bank d where a.no = c.no and c.sub_id= d.sub_id and a.no=(select no from t_newappl where no=?) and rownum=1 and d.status = 'SPEND'");
                PreparedStatement preparedStatement = connection.prepareStatement(query.toString());
                preparedStatement.setString(1,ApplicationNo);
                preparedStatement.setString(2,ApplicationNo);
                ResultSet resultSet = preparedStatement.executeQuery();
                while (resultSet.next()){
                   Details = new BankDetails();
                    Details.No= Util.Trim(resultSet.getString("NO"));
                    Details.BankName= Util.Trim(resultSet.getString("BANK_NAME"));          

                }

                resultSet.close();
                preparedStatement.close();

        return Details;
}

循环打印字母是

    int retail = Outletht.size();
                                            int ctr = 0;
                                             for (int i = 1; i <= subshop.size ; i++ ){
                                                  ctr++;
                                                  Letter Letter = new Letter(lmediator);
                                                 BankDetails Details = Letter.printLetter(applicationNumber);
                                                  if (Details!=null){
                                                    if (ctr == 1) {
                                                        if ((Details.getNo() == null)
                                                                && (Details.getLetterID() == null)
                                                                && ((Details.getLoginID() == null) || (Details.getLoginID().equals("")))) {
                                                        } else {
                                                            Letter.databaseUpdate(Details);
                                                        }
                                                    }

                                                                     Letter.writefile(Details);                                            
                                                  Letter.callfile(Details);
                                                  context.showMessage("I01015");                                                     

                                                    }                                         
                                                else
                                                {
                                                    context.showMessage("I04004");
                                                }
                        }

【问题讨论】:

  • 你能不能也给一些示例代码......它会更容易看到......很可能你将错误的数据传递给writefile方法......

标签: java mysql resultset


【解决方案1】:

您的问题有点难以理解。一些示例查询输出可能有助于澄清。

如果您无法区分主记录和子记录之间的差异,我建议您在联合之前在查询中添加一列。

从 T1 中选择 A、B、'main' 作为 xtype 联合所有 从 T2 中选择 A、B、'sub' 作为 xtype

A    B    xtype
---  ---  ---
aaa  bbb  main
bbb  ccc  sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    相关资源
    最近更新 更多