Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1036625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:44:04+00:00 2026-05-16T14:44:04+00:00

A user has a few Lookup selections on a JSP page. After he’s selected

  • 0

A user has a few Lookup selections on a JSP page. After he’s selected what data he wants exported, the JSP calls my servlet which should do something like this:

  1. get request data
  2. generate SQL for the request data
  3. execute the SQL and write it to a XML file
  4. package the XML file to a ZIP file and return it as a response

Now, the first two points are trivial, but the second two are what I’m unsure of:

 //3. execute the SQL and write it to a XML file
    ConnectionManager cm = new ConnectionManager();
    Connection conn = null;
    ResultSet rs = null;
    Statement stmt = null;

    try {
        conn =cm.getConnection();
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
        rs = stmt.executeQuery(sqlQuery);
        // generates XML and writes it to a java.io.Writer
        OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream(Konstante.zipFolder + idVrsteSifarnika+ ".xml"), "UTF8");
        XMLWriter.writeXMLToWriter(rs,writer);
    } catch (Exception ex) {
        logger.error(ex);
    } finally {
        ConnectionManager.close(rs, stmt, conn); 
    }

//4. package the XML file to a ZIP file and return it as a response
    ZipEntry ze = new ZipEntry(idVrsteSifarnika + ".xml");
    BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
    ZipOutputStream zos = new ZipOutputStream(bos);
    zos.putNextEntry(ze);
    FileInputStream fis = new FileInputStream(new File(Konstante.zipFolder + idVrsteSifarnika+ ".xml"));
    final byte[] buffer = new byte[1024];
    int n;
    while ((n = fis.read(buffer)) != -1)
        zos.write(buffer, 0, n);
    zos.closeEntry();
    zos.flush();
    zos.close();
    fis.close();
    bos.close();

What I would like to know is if could optimize memory usage/execution speed by changing the way I do all this/changing the various streams?

P.S. This is the writeXMLToWriter method

//Set up XML
    DataWriter w = new DataWriter(writer);
    w.startDocument();
    w.setIndentStep(2);
    w.startElement(startingXMLElement);
    // Get the metadata
    ResultSetMetaData meta = rs.getMetaData();
    int count = meta.getColumnCount();
    // Iterate over the set
    while (rs.next()) {
        w.startElement(rowElement);
        for (int i = 0; i < count; i++) {
            Object ob = rs.getObject(i + 1);
            if (rs.wasNull()) {
                ob = null;
            }
            String colName = meta.getColumnLabel(i + 1);
            if (ob != null ) {
                if (ob instanceof Timestamp) {
                    w.dataElement(colName, Util.formatDate((Timestamp)ob, dateFormat)); 
                }
                else if (ob instanceof BigDecimal){
                    w.dataElement(colName, Util.transformToHTML(new Integer(((BigDecimal)ob).intValue())));
                }
                else {
                    w.dataElement(colName, ob.toString());
                }

            } else {
                w.emptyElement(colName);
            }
        }
        w.endElement(rowElement);
    }
    w.endElement(startingXMLElement);
    w.endDocument();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:44:05+00:00Added an answer on May 16, 2026 at 2:44 pm

    There’s no need to write the XML to disk at all. Simply create an OutputStreamWriter wrapping the ZipOutputStream and pass that to your writeXMLToWriter method:

    ZipOutputStream zos = new ZipOutputStream(bos);
    zos.putNextEntry(ze);
    OutputStreamWriter writer = new OutputStreamWriter(zos, "UTF-8");
    XMLWriter.writeXMLToWriter(rs,writer);
    writer.close();
    zos.closeEntry();
    zos.flush();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a MainActivity in which the user has a few app options, displayed
THIS USER HAS ALREADY ASKED THE EXACT DUPLICATE A FEW MINUTES AGO sql and
I have a user control that has a few public properties, one is an
User has many Tracks, through Favorite. Favorite has some extra per-user meta-data about the
I got a user control that has a few links, one of them is
I have a user model that has a few different types, i.e. there is
I have a few partial views that should render when the user has a
What's the best way to link back to the page that a user has
I have a few forms that the user has to fill in and I
I have a MySQL DB with a user that has access only a few

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.