Wednesday, 7 August 2013

Upload Data from .CSV Formate into Data Base


Hi!


In previous blog i show how to download data from database into Excel / .csv formate.
Now in this blog i show you how to Upload the data from .csv formate into database.

.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




Statement stmt;
    String query;    try {        Class.forName("com.mysql.jdbc.Driver");        Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/readmain", "root", "root");        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);        String dds = JOptionPane.showInputDialog(null, "Enter FileName To Load", "");        if (!dds.equals("")) {            String fn = "C:/" + dds + ".csv";            query = "LOAD DATA INFILE '" + fn + "' INTO TABLE salarydet FIELDS TERMINATED BY ',' (EmployeeId,Saldate,Basic,Additionalpay,Spl,Other,Leavem,Gross,Netsal,Laveded,Loanamt,Festiveladv,Traveladv,Salaryadv,Othersded,Totded,Insurance)";            stmt.executeUpdate(query);            JOptionPane.showMessageDialog(null, "Uploaded Sceeceefully");        } else {
            JOptionPane.showMessageDialog(null, "Check Your File Please");
        }
    } catch (Exception r) {
        JOptionPane.showMessageDialog(null, "Check Your File Please");
    }


Thank You!

Have A Happy Day..,

No comments:

Post a Comment