5

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
2
  • Use PreparedStatement Commented Jun 9, 2015 at 8:43
  • Why no parser? Is it a homework? If not, just use a parser. Writing your own will hit you with bugs later. Commented Oct 8, 2018 at 13:02

3 Answers 3

8

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();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

My question how to put in the loop and match the columns with values in array when I am looping it
8

You can use Prepare Statement and set the value in parameter at each iteration:

Connection con = OracleDBConnection.getConnection();
String query = "Insert into NEWSTUDENT values(?,?,?,?)";
PreparedStatement ps=con.prepareStatement(query);
ps.setString(1,array[0]);
ps.setString(2,array[1]); // and so on
ps.executeUpdate();

If No of Rows are more you can also use Batch Processing :

String sql = "Insert into NEWSTUDENT values(?,?,?)";


PreparedStatement preparedStatement = null;
try{
    preparedStatement =
            connection.prepareStatement(sql);

    preparedStatement.setString(1, "Gary");
    preparedStatement.setString(2, "Larson");
    preparedStatement.setString (3, "Test");

    preparedStatement.addBatch();

    preparedStatement.setString(1, "Stan");
    preparedStatement.setString(2, "Lee");
    preparedStatement.setString (3, 456);

    preparedStatement.addBatch();

    int[] affectedRecords = preparedStatement.executeBatch();

}finally {
    if(preparedStatement != null) {
        preparedStatement.close();
    }
}

2 Comments

I have about 1000 rows
Then you can use Batch Processing !
2

You can store your data in an array and bind them to your statement:

 String query = "Insert into NEWSTUDENT values(?,?,?,?)";

 PreparedStatement st = con.prepareStatement(query);
 st.setString(1,array [0]);
 st.setString(2,array[1]);
 ....
 st.executeUpdate();        

For more informations about prepared statements see the oracle documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.