如何从两个并集语句的结果集中提取值

发布于 2024-09-24 13:55:40 字数 5759 浏览 4 评论 0原文

我在 dao 类中有一个名为 "getDetails" 的方法。在此方法中,我将两个表中的两个 select 语句合并起来,该表具有几乎相同的字段,称为“main shop” & “子商店”并将这些查询放入准备好的语句中。然后我将preparedStatement放入resultSet中。

这个“getDetials”方法返回“details”,我将把它用在中介器中名为<的另一个方法中strong>“writefile”,以便打印 Microsoft Word 中的值,例如“writefile(details)”。在“writefile”方法中,有一些值的字符串,并将“details”的值放入相应的字符串中。并为每个值附加“outputString”

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

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

问题是尽管字母数量正确(例如,如果有两个,但不同字母的数据相同(仅显示“主店”数据) “子商店”,它打印三个字母)。我应该如何获取“主店”字母后后续字母中的“子店”值被打印。

这是“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");
                                                }
                        }

I have a method in dao class called "getDetails". In this method, I union the two select statements from two tables with almost same field called "main shop" & "sub shops" and put those queries to preparedstatement. Then I put the preparedStatement to resultSet.

This "getDetials" method return "details" and I'll use it into another method in mediator called "writefile" in order to print the values in microsoft word like "writefile(details)". In the "writefile" method, there are strings of values and put the values of "details" to respective string. And append the "outputString" for each value.

On the input screen, user may check "sub shops" check box and will fill the details of "sub shops" after checking it. If they don't check, they need to fill only "main details" and not "sub shops". If they check "main shop", I need to print only one letter. If they check "sub shops" and fill the "quantity of sub shops", I need to print the letters where the letter number is equal to "quantity of subshops" + one(main letter). The letter format is totally same but only change the value of the fields of "shop name" (main or sub shop), "shop bank code" (main or sub shop) according to user's choice.

In the mediator, I loop the "writefile(details)" according to the quantity of "sub shops".

The problem is the data in different letters are same (only show the "main shop" data) though the letter quanity is correct (eg. if there are two "sub shops", it prints three letters). How should I do to get the value of "sub shops" in subsequent letters after "main shop" letter is printed.

This is "writefile" method

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();
}

The query sample is

    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;
}

The looping to print letter is

    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");
                                                }
                        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妳是的陽光 2024-10-01 13:55:40

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

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

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

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

Your question is a bit hard to follow. Some sample query output may help clarify.

If you're having trouble distinguishing the difference between main and sub records, i would suggest you add a column to your query before the union.

select A, B, 'main' as xtype from T1
union all
select A, B, 'sub' as xtype from T2

A    B    xtype
---  ---  ---
aaa  bbb  main
bbb  ccc  sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文