Wednesday, 7 August 2013

Convert / Download Data from Data Base into .CSV Formate


Hi!


In this blog i show you how to convert the data from database into .csv formate.

.CSV formate is one of the secured formate to save data.

here i give an example code how to convert it.

i am using apache poi external jar for this process.

You can download from apache site.

simply i am calling the record from the MySQL Server, while if it is true then i pass those result-set through the poi


have to pass the header of your .csv column and align your column and the row


try {
        DateFormat format = new SimpleDateFormat("dd/MM/yyyy");        java.util.Date date = format.parse(new DatePicker(NewJFrame).setPickedDate());        java.sql.Date sqlD = new java.sql.Date(date.getTime());        Class.forName("com.mysql.jdbc.Driver");        Connection connn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/readmain", "root", "root");        PreparedStatement ps100 = connn.prepareStatement("select Saldate from salarydet where Saldate ='" + sqlD + "'");        ResultSet rs100 = ps100.executeQuery();        if (rs100.next()) {            try {                Class.forName("com.mysql.jdbc.Driver");                Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/readmain", "root", "root");                String mn = JOptionPane.showInputDialog(null, "Enter File Name", "");
                if (!mn.equals("")) {
                    String mnd = "C:/" + mn + ".CSV";
                    Object dd = gtempid.getSelectedItem();
                    PreparedStatement ps = conn.prepareStatement("SELECT * INTO OUTFILE '" + mnd + "' FIELDS TERMINATED BY ',' FROM salarydet where Saldate ='" + sqlD + "';");
                    ps.executeQuery();
                    conn.close();
                    JOptionPane.showMessageDialog(null, " CSV File Saved On c:/");
                } else {
                    JOptionPane.showMessageDialog(null, "Enter Valid File Name");
                }
            } catch (Exception r) {
                JOptionPane.showMessageDialog(null, "Process Is Canceled & Check Your File Name");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Select Correct Date Please");
        }
    } catch (Exception e) {
    }


Thank You!..,

No comments:

Post a Comment