如何使用Java将MySQL DB中的数据作为CSV文件获取

时间:2012-08-21 13:00:10

标签: java mysql csv

我正在使用以下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();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

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

在刷新到磁盘之前,

PrintWriters缓冲到内存,因此您不会因等待写入磁盘而导致性能下降。虽然,您的代码没有提及有关刷新的任何内容,因此如果您正在使用非常大的数据集,则可能会遇到内存不足问题。也许做一个out.flush()每千条记录(取决于什么效果最好)。