2

I have csv in file containing multiple rows.If first column value is nothing its is giving error and m not able to insert in database. ex If row is :130,1,datafile8.csv, 2007 ,17,List_date no problem in reading n inserting but if row is: ,0,datafile8.csv,Bihar,7,list_Left ,not able to read n insert .how to insert null in above row .so i can insert dis row in database.

String keyword = "celldescription.csv";
File makefile = new File(keyword);
  BufferedReader r2 = new BufferedReader(new FileReader(makefile));
  strLine1 = r2.readLine();
   System.out.println (strLine1);
   String r="0";int r1=0;

  while((strLine1=r2.readLine())!=null)
   {
  System.out.println (strLine1);

  StringTokenizer st2 = new StringTokenizer(strLine1, ",");
  // Print the content on the console


        String cellvalue = st2.nextToken();          


        String position = st2.nextToken();


        String Docid=st2.nextToken();


        String Word=st2.nextToken();


        String Count=st2.nextToken();

        String List_Entry=st2.nextToken();


        String tab3="insert into description(cellvalue,position,Docid,Word,Count,List_Entry) values(?,?,?,?,?,?)";
        ps = connection.prepareStatement(tab3);
        ps.setString (1,cellvalue );
        ps.setString (2,position );
        ps.setString (3,Docid);
        ps.setString (4,Word );
        ps.setString (5,Count );
        ps.setString (6,List_Entry );
        ps.executeUpdate();



 }//end of while



  r2.close();

 System.out.println("Data is inserted");

        }//try closed**
6
  • 3
    I haven't seen StringTokenizer in code for quite awhile. You may want to consider using String.split(",") instead. Commented Feb 15, 2013 at 10:09
  • Is it difficult to add a null checking? hmm.. Commented Feb 15, 2013 at 10:10
  • 1
    Also, CSV is quite more complicated that it seems (quoted values that include separators, and so on). I would suggest using a third party API, Apache has one that looks good. Commented Feb 15, 2013 at 10:10
  • I used a StringTokenizer just the other week, it was quaint. Commented Feb 15, 2013 at 10:12
  • And many databases have tools to directly import csv file. Commented Feb 15, 2013 at 10:12

2 Answers 2

2

When your String strLine1 starts with comma(,) StringTokenizer omit empty string if it is in start or end or even in between.

Ex - ,0,datafile8.csv,Bihar,7,list_Left

token -> "0" - "datafile8.csv" - "Bihar" - "7" and "list_Left"

better you split the string by comma(,). Ex -

String[] str = strLine1.split(",",-1);

str[] -> ["","datafile8.csv","Bihar","7" and "list_Left"]

Sign up to request clarification or add additional context in comments.

Comments

1

You may want to consider using a java library for your work with csv files. OpenCSV is one, it helped me a lot.

Some of its features:

  • Arbitrary numbers of values per line
  • Ignoring commas in quoted elements
  • Handling quoted entries with embedded carriage returns (ie entries that span multiple lines)
  • Configurable separator and quote characters (or use sensible defaults)
  • Read all the entries at once, or use an Iterator style model
  • Creating csv files from String[] (ie. automatic escaping of embedded quote chars)

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.