Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert values from csv into database in java

Tags:

java

csv

I have tried the following code to split the csv values and now how do insert it in to DB? Do I have save the values in to separate variables to match column names? I am unable to figure out.

Note: I don't want to use any csv parser right now. I just want to do it manually

public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
        String name;
        String email;
        String phone;
        String ID;

        Connection con = OracleDBConnection.getConnection();
        String query = "Insert into NEWSTUDENT values(?,?,?,?)";
                PreparedStatement st = con.prepareStatement();
                st.executeUpdate(query);        

        try {
            BufferedReader bReader = new BufferedReader(new FileReader("1000rows.csv"));

            while (bReader != null) {
                String read;
                try {
                    read = bReader.readLine();
                    if (read != null) 
                    {
                        String[] array = read.split(",+");
                        for(String result:array)
                        {
                            System.out.println(result);
                        }
                    } 
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                finally
                {
                   if (bReader == null) 
                    {
                        bReader.close();
                    }
                }
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
    }
}

output:

1Kiriti
[email protected]
880789939

Column names in Database:

Name Email Phone ID
like image 956
kittu Avatar asked Jun 09 '15 08:06

kittu


People also ask

How import data from Excel to database in Java?

table_daily_report(); Vector dataHolder = read(fileName); saveToDatabase(dataHolder); } public static Vector read(String fileName) { Vector cellVectorHolder = new Vector(); try { FileInputStream myInput = new FileInputStream(fileName); POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); HSSFWorkbook myWorkBook ...

How do I import CSV file into MySQL database?

In the Format list, select CSV. Changing format-specific options. If the csv file is delimited by a character other than a comma or if there are other specifications to the csv files, we can change it in this portion. Click Go to start importing the csv file and the data will be successfully imported into MySQL.


1 Answers

Use Parepared statement and build a query in while Loop and execute it. For more on Prepared Statement please check Check this link

String sql = " INSERT INTO TABLE_(name,email,phone,id) VALUES(?,?,?,?) ";


try { 
        BufferedReader bReader = new BufferedReader(new FileReader("1000rows.csv"));
        String line = ""; 
        while ((line = bReader.readLine()) != null) {
            try {

                if (line != null) 
                {
                    String[] array = line.split(",+");
                    for(String result:array)
                    {
                        System.out.println(result);
 //Create preparedStatement here and set them and excute them
                PreparedStatement ps = yourConnecionObject.createPreparedStatement(sql);
                 ps.setString(1,str[0]);
                 ps.setString(2,str[1]);
                 ps.setString(3,str[2]);
                 ps.setString(4,strp[3])
                 ps.excuteUpdate();
                 ps. close()
   //Assuming that your line from file after split will folllow that sequence

                    }
                } 
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            finally
            {
               if (bReader == null) 
                {
                    bReader.close();
                }
            }
        }
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    }
like image 133
Rookie007 Avatar answered Oct 04 '22 20:10

Rookie007