English 中文(简体)
Java Struts Report
原标题:

I am writing an excel report using an action,controller, servlet struts framework. The report is very congested and has already around 10 separate queries. I need to add about 10 more queries due to a change in the report. Is there a way to do this without having to keep nesting the Result Sets? Below is the existing code.

public TotalSalesKPIContainer totalSalesKPIQuery(String franchiseId,    java.util.Date startDate, java.util.Date stopDate) {
    String getFranIds = null;
    ArrayList storeResultList = new ArrayList();
    ArrayList salesPersonResultList = new ArrayList();
    TotalSalesKPIContainer result = null;
    Connection conn = null;
    PreparedStatement pstmt1, pstmtLY, pstmtCountLY, pstmtTCount, pstmtTCountLY, pstmtCount = null;
    PreparedStatement pstmt, ps = null;
    ResultSet rs1, rsLY, rsCountLY, rsTCount, rsTCountLY, rsCount = null;
    ResultSet rs = null, rsSP = null, rsBySP, rsTCount2, rsTCountLY2 = null, rsBySPLY, rsCountLY2, rsCount2;
    java.util.Date prevStartDate;
    java.util.Date prevStopDate;
    try {

        conn = getConnection();
        if (franchiseId.equals("0")) {
            getFranIds = "Select id, name from vymac.organization";
            pstmt = conn.prepareStatement(getFranIds);
        } else {
            getFranIds = "Select id, name from vymac.organization where id = ? ";
            pstmt = conn.prepareStatement(getFranIds);
            pstmt.setString(1, franchiseId);
        }
        int franID = Integer.parseInt(franchiseId);
        rs = pstmt.executeQuery();
        while (rs.next()) {

            result = new TotalSalesKPIContainer(rs.getString(2), franID,startDate, stopDate, storeResultList,salesPersonResultList);
            /*
             * This section is for Salesperson total sales
             */
            pstmt1 = conn.prepareStatement(getTotalSalesBySP);
            pstmt1.setInt(1, rs.getInt(1));
            pstmt1.setDate(2, new java.sql.Date(startDate.getTime()));
            pstmt1.setDate(3, new java.sql.Date(stopDate.getTime()));
            rsBySP = pstmt1.executeQuery();

            while (rsBySP.next()) {
                ps = conn.prepareStatement(getSalesPersons);
                ps.setInt(1, rsBySP.getInt(1));
                rsSP = ps.executeQuery();
                if (rsSP.next()) {
                    ps = conn.prepareStatement(getPrevTotalSalesBySP);

                    // Get previous year date
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(startDate);
                    cal.add(Calendar.YEAR, -1);
                    prevStartDate = cal.getTime();
                    cal.setTime(stopDate);
                    cal.add(Calendar.YEAR, -1);
                    prevStopDate = cal.getTime();

                    ps.setString(1, rsBySP.getString(1));
                    ps.setDate(2,new java.sql.Date(prevStartDate.getTime()));
                    ps.setDate(3, new java.sql.Date(prevStopDate.getTime()));
                    rsBySPLY = ps.executeQuery();

                    if (rsBySPLY.next()) {
                        // Count Invoices for Last Year
                        ps = conn.prepareStatement(findCountBySP);
                        ps.setDate(1, new java.sql.Date(prevStartDate.getTime()));
                        ps.setDate(2, new java.sql.Date(prevStopDate.getTime()));
                        ps.setString(3, rsBySP.getString(1));

                        rsCountLY2 = ps.executeQuery();

                        if (rsCountLY2.next()) {
                            ps = conn.prepareStatement(findCountBySP);
                            ps.setDate(1, new java.sql.Date(startDate.getTime()));
                            ps.setDate(2, new java.sql.Date(stopDate.getTime()));
                            ps.setString(3, rsBySP.getString(1));

                            rsCount2 = ps.executeQuery();
                            if (rsCount2.next()) {
                                ps = conn.prepareStatement(trafficCountBySP);
                                ps.setDate(1, new java.sql.Date(startDate.getTime()));
                                ps.setDate(2, new java.sql.Date(stopDate.getTime()));
                                ps.setString(3, rsBySP.getString(1));
                                rsTCount2 = ps.executeQuery();

                                if (rsTCount2.next()) {
                                    ps = conn.prepareStatement(trafficCountBySP);
                                    ps.setDate(1, new java.sql.Date(prevStartDate.getTime()));
                                    ps.setDate(2, new java.sql.Date(prevStopDate.getTime()));
                                    ps.setString(3, rsBySP.getString(1));

                                    rsTCountLY2 = ps.executeQuery();

                                    if (rsTCountLY2.next()) {



                                        TotalSalesInformationBySalesPerson tbsp = new TotalSalesInformationBySalesPerson(rsSP.getString(1), rs.getString(2), rs.getInt(1),rsCount2.getInt(1), rsBySP.getDouble(2),rsBySPLY.getDouble(2),rsCountLY2.getInt(1),
                                                rsTCount2.getInt(1),rsTCountLY2.getInt(1));
                                        salesPersonResultList.add(tbsp);
                                    } else {
                                        TotalSalesInformationBySalesPerson tbsp = new TotalSalesInformationBySalesPerson(
                                                rsSP.getString(1), rs
                                                        .getString(2), rs
                                                        .getInt(1),
                                                rsCount2.getInt(1), rsBySP
                                                        .getDouble(2),
                                                rsBySPLY.getDouble(2),
                                                rsCountLY2.getInt(1),
                                                rsTCount2.getInt(1), 0);
                                        salesPersonResultList.add(tbsp);
                                    }
                                }
                            }
                        }
                    } else {
                        ps = conn.prepareStatement(findCountBySP);
                        ps.setDate(1,
                                new java.sql.Date(startDate.getTime()));
                        ps
                                .setDate(2, new java.sql.Date(stopDate
                                        .getTime()));
                        ps.setString(3, rsBySP.getString(1));
                        rsCount2 = ps.executeQuery();
                        if (rsCount2.next()) {
                            ps = conn.prepareStatement(trafficCountBySP);
                            ps.setDate(1, new java.sql.Date(startDate
                                    .getTime()));
                            ps.setDate(2, new java.sql.Date(stopDate
                                    .getTime()));
                            ps.setString(3, rsBySP.getString(1));
                            rsTCount2 = ps.executeQuery();
                            if (rsTCount2.next()) {

                                TotalSalesInformationBySalesPerson tbsp = new TotalSalesInformationBySalesPerson(
                                        rsSP.getString(1), rs.getString(2),
                                        rs.getInt(1), rsCount2.getInt(1),
                                        rsBySP.getDouble(2), 0.00, 0,
                                        rsTCount2.getInt(1), 0);
                                salesPersonResultList.add(tbsp);
                            } else {
                                TotalSalesInformationBySalesPerson tbsp = new TotalSalesInformationBySalesPerson(
                                        rsSP.getString(1), rs.getString(2),
                                        rs.getInt(1), rsCount2.getInt(1),
                                        rsBySP.getDouble(2), 0.00, 0,
                                        rsTCount2.getInt(1), 0);
                                salesPersonResultList.add(tbsp);
                            }
                        }
                    }
                }
            }
最佳回答

It seems like you are doing joins in the your code, that is performing a query and for each record returned, performing a query on some of that data. And then in fact nesting more such queries.

Usually it s possible to do more of the join work in the database, but I don t think we can help more without seeing the database schemas and understanding the meaning of the code.

I suggest that you take just one level of the problem, and consider whether it can be expressed as a single query, or UNION of some related queries. And then build from there.

As an aside, the Java you have could be reorganised and refactored to make it much easier to understand, pull out the sub queries to their own functions - but it is to be hoped that you will not need nested code like this.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签