How to insert csv file content in Java -
i trying insert csv file data table. using opencsv-2.3.jar this. have class csvreaderwriter.java ie
public class csvreaderwriter { private static final string parse_field_name = "\\$\\{keys\\}"; private static final string parse_field_value = "\\$\\{values\\}"; private static final string sql_insert_query = "insert ${table}(${keys}) values(${values})"; private static final string parse_table_name = "\\$\\{table\\}"; private final connection con; private char seprator; public csvreaderwriter(connection connection) { this.con = connection; this.seprator = ','; // it's default separator } public char getseprator() { return seprator; } public void setseprator(char seprator) { this.seprator = seprator; } public void readwritecsv(string csvfilename, string dbtablename,boolean deletetabledatabeforeload) throws exception { au.com.bytecode.opencsv.csvreader csvreader = null; if (null == this.con) { throw new exception("not valid database connection."); } try { csvreader = new au.com.bytecode.opencsv.csvreader(new filereader(csvfilename),this.seprator); } catch (exception e) { throw new exception("error occured while executing file. "+ e.getmessage()); } string[] headerrow = csvreader.readnext(); if (null == headerrow) { throw new filenotfoundexception("no header column found in given csv file." + "please modify csv file format , provide header column."); } string questionmarks = stringutils.repeat("?,", headerrow.length); questionmarks = (string) questionmarks.subsequence(0,questionmarks.length() - 1); string query = sql_insert_query.replacefirst(parse_table_name,dbtablename); query = query.replacefirst(parse_field_name,stringutils.join(headerrow, ",")); query = query.replacefirst(parse_field_value, questionmarks); string[] nextline; connection con = null; preparedstatement pstmt = null; try { con = this.con; con.setautocommit(false); pstmt = con.preparestatement(query); /** * deleting data existing table before loading csv * file, if boolean value passed true. */ if (deletetabledatabeforeload) { con.createstatement().execute("delete " + dbtablename); } final int batchsize = 50; int count = 0; //date date = null; //double dou = 0.0; while ((nextline = csvreader.readnext()) != null) { if (null != nextline) { int index = 1; (string string : nextline) { if (string.isempty()) { pstmt.setstring(index++,"null"); } else { pstmt.setstring(index++, string); } } pstmt.addbatch(); } if (++count % batchsize == 0) { pstmt.executebatch(); } } // insertion of remaining records pstmt.executebatch(); con.commit(); } catch (exception e) { con.rollback(); throw new exception("error during loading data csv file database table."+ e.getmessage()); } { if (null != pstmt) pstmt.close(); if (null != con) con.close(); csvreader.close(); } } } i calling class code as
csvreaderwriter csvreaderwriter = new csvreaderwriter(con); csvreaderwriter.readwritecsv("c:\abc.csv", "abc.csv", true);
the issue in chrome , firefox not able filepath. shows fakepath. there other way insert data of csv file database
Comments
Post a Comment