Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simple Algorithm\Logic

Posted on 2003-11-14
10
Medium Priority
?
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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 160 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 240 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

722 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