0

我正在使用以下 Java 代码从MySQL表中获取数据作为CSV文件。

使用此命令,我可以从MySQL获取数据作为 CSV文件

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

如何优化我的代码\算法?

我的代码是:

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();
        }
    }
}
4

2 回答 2

1

您可以使用 rs.getMetadata() 来获取 ResultSetMetaData 对象。这将告诉您有多少列,以及列名是什么。

使用该信息,您可以使用循环输出标题。

现在您知道了列数,您还可以使用循环输出每一行。

于 2012-08-21T13:11:50.223 回答
0

您无法消除该 while 循环,这是您执行时间的很大一部分。该循环基本上使您的过程成为 O(n) ,这非常好。由于您没有任何其他循环要消除,因此您已经处于非常好的位置。

PrintWriters在刷新到磁盘之前缓冲到内存,因此您不会因为等待写入磁盘完成而影响性能。虽然,您的代码没有提及任何有关刷新的内容,因此如果您使用非常大的数据集,您可能会遇到内存不足的问题。也许每千条记录执行一次 out.flush() (取决于最有效的方法)。

于 2012-08-21T13:49:24.317 回答