Solved

Simple Algorithm\Logic

Posted on 2003-11-14
10
274 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

713 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