Solved

Simple Algorithm\Logic

Posted on 2003-11-14
10
241 Views
Last Modified: 2010-03-31
The database query would fetch more than 1000 records as resultset.

Now, I have to loop through each record and extract values from records and each record would be considered as a client request. Each client request is formed as a XML and written into a file. Limitation is each file should contain ONLY 200 requests (records). How should I dynamically create the XML objects for every 20 records and put them in a file.

I.e. The 1000 records should result in 5 files with 200 records (XML requests) each. Could somebody help me in doing this?

Thanks.
0
Comment
Question by:innumonenu
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9747529
What code do you have at the moment?

Is this homework? ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9747562
Just create a new xml insert every 200 reads:

int numRecords = 0;
StringBuffer xmlInsert = new StringBuffer();
while(resultSet.next()) {
    ++numRecords;
    xmlInsert.append("<root>");
    xmlInsert.append("<node1>");
    xmlInsert.append(rs.getString(1));
    xmlInsert.append("</node1>");
    if (numRecords == 200) {
       xmlInsert.append("</root>");
       doInsert(xmlInsert.toString();
       xmlInsert.setLength(0);
    }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9747566
>>Is this homework?

I hope not!
0
 
LVL 15

Expert Comment

by:JakobA
ID: 9751508
Assuming your database is SQL driven i would use the LIMIT clause in the select statement

     SELECT whatever
     FROM whereve
     WHERE conditions
     ORDER BY sequence
     LIMIT   first, number

before limit is your normal request. producing 1000 records
Limit then is a way of aying: but only send the ones from nr 'first' and 'number' forvard
    Limit 0, 5      // the first five
    Limit 5, 5      // the next 5
...

You wil be doing several queries, but each return only records enough for one XML file

regards JakobA
0
 
LVL 92

Accepted Solution

by:
objects earned 40 total points
ID: 9752925
Storing the entire file contents in a string is unnecessary and could use lots of memory.
Instead simply write the xml directly to the file, and switch files every 200 records.

FileOutputStream out = null;
int n = 0;
while(resultSet.next())
{
    if (n%200==0)
    {
       if (out!=null)
       {
          // Close currently open file
          out.close();
       }
       // Open next file
       out = openNextFile();
    }

    // write xml to out as required
    ....

    n++;
}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:innumonenu
ID: 9753881
Thanks all for the response. It is not a home work :-)

The code I currently have is creation of XML and put into a file for records less than 200.
Only logic missing is - if more than 200 records then split them into different files containing only 200 records. Also, restricting in the SQL query is not desirable for us our requirments. We have to fetch all existing records and split them in the java program.

I would try the other options listed on monday and update you all. Thanks again.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9753963
>>Only logic missing

This should cope with any number:

int numRecords = 0;
StringBuffer xmlInsert = new StringBuffer();
while(resultSet.next()) {
      ++numRecords;
      xmlInsert.append("<root>");
      xmlInsert.append("<node1>");
      xmlInsert.append(rs.getString(1));
      xmlInsert.append("</node1>");
      if (numRecords == 200) {
            writeToFile(closeRoot, xmlInsert);
      }
      if (xmlInsert.getLength() > 0)
            writeToFile(closeRoot, xmlInsert);
      }
}

public void writeToFile(String closeRoot, StringBuffer xmlInsert) {
      xmlInsert.append(closeRoot);
      // Do the file writing, calling xmlInsert.toString for the contents
      xmlInsert.setLength(0);
}
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 60 total points
ID: 9754098
Oops - just spotted an error:

>>if (numRecords == 200) {

should be

if (numRecords % 200 == 0) {
0
 

Author Comment

by:innumonenu
ID: 9776090
Thanks all for the help
0
 
LVL 92

Expert Comment

by:objects
ID: 9776120
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Receive file in Servlet 1 36
Safari, Firefox, IE, Java, Plugin, Java Security error 13 40
firstChar challenge 13 84
JAVA part two 5 41
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now