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
Solved

Simple Algorithm\Logic

Posted on 2003-11-14
10
268 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 60
null output 3 35
difference between sorce folder and folder in eclipise 3 28
mysql jsp example issue 32 48
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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

790 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