java - Insert values from arraylist into database -
i use addbatch()
, executebatch()
insert values arraylist
database.
but when run code error message appeared:
exception in thread "main" java.sql.batchupdateexception: duplicate entry '5' key 'primary' @ com.mysql.jdbc.preparedstatement.executebatchserially(preparedstatement.java:2020) @ com.mysql.jdbc.preparedstatement.executebatch(preparedstatement.java:1451)
this data:
1 r1
7 r1
2 b r1
3 c r1
4 b r1
8 r1
5 r1
9 c r1
6 c r1
this part of code:
public void insertdb(arraylist<eventlog> d) throws sqlexception { string insert = "insert eventlog values(?,?,?)"; preparedstatement ps = conn.preparestatement(insert); (int k = 0; k < d.size()-1; k++) { ps.setint(1, d.get(k).getevent_id()); // event id ps.setstring(3,d.get(k).getresources()); // resource ps.setstring(2,d.get(k).getactivity()); // activity ps.addbatch(); } ps.executebatch(); }
the code looks fine, make sure table truncated before run since looks more or less static set of fields.
to clarify further, primary key 5 exists in database , need delete before running code.
Comments
Post a Comment