java - com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: 'Mon Feb 11 00:00:00 IST 2013' for column 'INVOICE_DATE' at row 1 -
i've excel sheet book.xls
below:
_________________________________________________________________ | | | | invoice | invoice_date | |_________________________________________|______________________| | | | |c-edge/sbi//bho/(atm)-013/2012–2013 | 11-feb-2013 | |_________________________________________|______________________| |c-edge/sbi//bang/(atm)-013/2012–2013 | 13-aug-2014 | |_________________________________________|______________________|
i trying insert date excel mysql table below:
try { poifsfilesystem fs = new poifsfilesystem(input); hssfworkbook wb = new hssfworkbook(fs); hssfsheet sheet = wb.getsheetat(0); row row; (int = 1; <= sheet.getlastrownum(); i++) { row = sheet.getrow(i); richtextstring cedge_invoice_number = row.getcell(0).getrichstringcellvalue(); date invoice_date = row.getcell(1).getdatecellvalue(); system.out.println(invoice_date); string sql = "insert tablename values('" + cedge_invoice_number + "','" + invoice_date + "')"; pstm = (preparedstatement) con.preparestatement(sql); pstm.execute(); system.out.println("import rows " + i); } con.commit(); pstm.close(); con.close(); input.close(); system.out.println("success import excel mysql table"); } catch (classnotfoundexception e) { system.out.println(e); } catch (sqlexception ex) { system.out.println(ex); } catch (ioexception ioe) { system.out.println(ioe); }
however, got exception:
"com.mysql.jdbc.mysqldatatruncation: data truncation: incorrect datetime value: 'mon feb 11 00:00:00 ist 2013' column 'invoice_date' @ row 1".
how caused , how can solve it?
when using preparedstatement
, use ?
placeholders values; can use setdate
method of preparedstatemet
. e.g. in code:
string sql ="insert tablename values(?,?)"; pstm = (preparedstatement) con.preparestatement(sql); for(int i=1; i<=sheet.getlastrownum(); i++){ row = sheet.getrow(i); richtextstring cedge_invoice_number = row.getcell(0).getrichstringcellvalue(); string stringinvoicenumber = ... // convert richtextstring normal string date invoice_date = row.getcell(1).getdatecellvalue(); // have use java.sql.date instead of java.util.date in preparedstatement java.sql.date sqlinvoicedate = new java.sql.date(invoice_date.gettime()); pstm.setstring(1, stringinvoicenumber); pstm.setdate(2, sqlinvoicedate); pstm.execute(); ... }
see e.g. http://tutorials.jenkov.com/jdbc/preparedstatement.html more info.
Comments
Post a Comment