How to Upload CSV file in JAVA

Read CSV file in JAVA and insert its content into Database

It is CSV file and we have to Upload data inside database  ....


 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.Statement;
 import java.sql.ResultSet;

 public class UploadCsv {

    public static void main(String[] args) throws Exception {
        Statement stmt = null;
        Connection con = null;
        ResultSet rs = null;
        String splitBy = ",";
        String cv = "C:/Users/Dheeraj/Desktop/pRACTICE/Book1.csv";

        BufferedReader br = new BufferedReader(new FileReader(cv));
        try {
            /* SQL Database connectivity */
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "root");
            stmt = con.createStatement();
            String line = br.readLine();
            while ((line = br.readLine()) != null) {
                String[] b = line.split(splitBy);

                String sql = "insert into mysql.insertion_data (Month,Day, Date)values('" + b[0] + "', '" + b[1] + "', '" + b[2] + "')";

                stmt.executeUpdate(sql);

                /* Print Message on console  */
                System.out.println(b[0] + " " + b[1] + " " + b[2]);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        br.close();

    }
 }

More Links :
How to read CSV file in JAVA 
Download data from mysql db to csv file using java
Export data from mysql db to csv file using java 

Comments

Popular posts from this blog

Secure Database Connectivity in node.js with mysql

Export data from mysql db to csv file using java

API (Application Programming Interface)