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

  • Home
  • SEARCH
  • 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 8477371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:26:56+00:00 2026-06-10T18:26:56+00:00

I’m using following Java code for getting data from MySQL table as CSV file.

  • 0

I’m using following Java code for getting data from MySQL table as CSV file.

Using this command I can get data from MySQL as CSV file:

mysql -uuname -ppassword BR_RNCM -e'select * from Inward where inwarddate between "2012-08-21 00:00:00" and "2012-08-21 23:59:59"'>/home/praveen/downloadfile.csv

How can I optimize my code\algorithm?

My code is:

package com.mypackage.CAF.APController;

import com.rajsoft.CAF.util.DB2Connection;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InwardDataDownload extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        Connection con = null;
        ResultSet rs = null;
        Statement st = null;

        String date1 = null;
        String date2 = null;

        date1 = request.getParameter("fromdate");
        date2 = request.getParameter("todate");

        System.out.println("Date1 : " + date1 + "Date2 : " + date2);

        try {
            response.setContentType("text/csv");

            out.write("CAF");
            out.write(',');
            out.write("MDN");
            out.write(',');
            out.write("CRT CODE");
            out.write(',');
            out.write("DISTRIBUTOR NAME");
            out.write(',');
            out.write("CLUSTER");
            out.write(',');
            out.write("MNP");
            out.write(',');
            out.write("LOT NO");
            out.write(',');
            out.write("INDEX NO");
            out.write(',');
            out.write("BOX NO");
            out.write(',');
            out.write("USER");
            out.write(',');
            out.write("INWARD DATE");
            out.write('\n');

            query = "select * from Inward where inwarddate between '" + date1 + "' and '" + date2 + "';";
            con = (Connection) new DB2Connection().getDatabaseConnection();
            st = con.createStatement();
            rs = st.executeQuery(query);
            while (rs.next()) {
                out.write(rs.getString(1));
                out.write(',');
                out.write(rs.getString(2));
                out.write(',');
                out.write(rs.getString(3));
                out.write(',');
                out.write(rs.getString(4));
                out.write(',');
                out.write(rs.getString(5));
                out.write(',');
                out.write(rs.getString(6));
                out.write(',');
                out.write(rs.getString(7));
                out.write(',');
                out.write(rs.getString(8));
                out.write(',');
                out.write(rs.getString(9));
                out.write(',');
                out.write(rs.getString(10));
                out.write(',');
                out.write(rs.getString(11));
                out.write('\n');
            }

            response.setContentType("application/download");
            response.setHeader("Content-disposition", "attachment; filename =InwardData.csv");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }
}
  • 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-06-10T18:26:58+00:00Added an answer on June 10, 2026 at 6:26 pm

    There’s no way for you to eliminate that while loop and that’s a large chunk of your execution time. That loop basically makes your process a O(n) which is pretty good. Since you don’t have any other loops to eliminate, you’re already in a pretty good position.

    PrintWriters buffer to memory before flushing to disk so you’re not taking a performance hit by waiting for the write to disk to complete. Although, your code doesn’t mention anything about flushing so you may run into out of memory issues if you’re working with a really large dataset. Maybe do an out.flush() every thousand records (depending on what works best).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to

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.