java - Reading from a CSV file -
try { bufferedreader br = new bufferedreader(new inputstreamreader(item.getinputstream())); string strline = ""; stringtokenizer st = null; while ((strline = br.readline()) != null) { st = new stringtokenizer(strline, "\t"); while (st.hasmoretokens()) { urlcnt = st.nexttoken(); srccnt = st.nexttoken(); contenttype = st.nexttoken(); verticle = st.nexttoken(); timeframe = st.nexttoken(); } if (con == null) { sqlconnection.seturl("jdbc:sqlserver://192.168.2.53\\sql2005;user=sa;password=365media;databasename=ln_adweek"); con = sqlconnection.getnewconnection(); stmt = con.createstatement(); } try { resultset rs; boolean hasrows = false; rs = stmt.executequery("select url urls_temp url='"+urlcnt+"'"); while (rs.next()) { hasrows=true; i++; } if (!hasrows) { j++; preparedstatement inserturlstatement = con.preparestatement("insert urls_temp(url, source_name, is_active, is_periodic, link_type, new_entry, verticle, periodic_timeframe) values(?, ?, ?, ?, ?, ?, ?, ?)"); if (timeframe.equalsignorecase("daily")) { inserturlstatement.setstring(1, urlcnt); inserturlstatement.setstring(2, srccnt); inserturlstatement.setint(3, 1); inserturlstatement.setint(4, 0); inserturlstatement.setstring(5, contenttype); inserturlstatement.setint(6, 1); inserturlstatement.setstring(7, verticle); inserturlstatement.setstring(8, timeframe); inserturlstatement.executeupdate(); inserturlstatement.close(); } else { inserturlstatement.setstring(1, urlcnt); inserturlstatement.setstring(2, srccnt); inserturlstatement.setint(3, 1); inserturlstatement.setint(4, 1); inserturlstatement.setstring(5, contenttype); inserturlstatement.setint(6, 1); inserturlstatement.setstring(7, verticle); inserturlstatement.setstring(8, timeframe); inserturlstatement.executeupdate(); } } } } }
the above code used uploading details database given tab separated in csv file.
sample format of csv file follows , works fine:
http://avb.com(tab space)asdf(tab space)asdf(tab space)asdd(tab space)asdf
http://anything.com(tab space)asdf(tab space)asdf(tab space)asdfasd(tab space)asdfsadf
sometimes may need null
values inserted database csv file follows:
http://asdf.com(tab space)(tab space)aasddf(tab space)(tab space)asdfsad
but not working, nothing getting inserted database.
what modification has done above program inserting null
values in second , fourth (srccnt
& verticle
) columns of table?
stringtokenizer treats consecutive delimiters single delimiter. input contains [tab space] delimiters, rest of code doesn't seem expecting spaces so, without more information, i'm going guess input delimited tabs (not [tab space]), , adjacent delimiters being ignored, last nexttoken()
throws exception ignoring.
the answer here rewrite using split()
, recommended in javadoc
stringtokenizer legacy class retained compatibility reasons although use discouraged in new code. recommended seeking functionality use split method of string or java.util.regex package instead.
that said, should @ of existing csv libraries out there (google java csv).
Comments
Post a Comment