0

This is my code:

String insertQuery = "INSERT INTO Links(PageId, LinkId) values(?, ?)";
PreparedStatement preparedStatement = con.prepareStatement(insertQuery);
Vector<String> links = page.getLinks();    
for (String string : links) {       
    Concept c = dbpedia.findConceptbyTitle(string);                     
    if (c != null) {
        preparedStatement.setInt(1, Integer.parseInt(page.getID()));
        preparedStatement.setInt(2, c.id);
    }    
}
preparedStatement.executeUpdate();

Strangely when I set a breakpoint after executeUpdate, only the last row is inserted in the SQL table!

Andi Keikha
  • 1,246
  • 2
  • 16
  • 37
  • you need to create preparedStatement every time you want to insert into the loop. – SMA Mar 02 '15 at 17:20

2 Answers2

3

Add preparedStatement.addBatch(); before you leave your loop.

And replace preparedStatement.executeUpdate(); with preparedStatement.executeBatch();.

Vanleeuw
  • 46
  • 2
1

You overide your parameters every time only the last executes. Use batch- Statement : http://www.mkyong.com/jdbc/jdbc-preparedstatement-example-batch-update/

Bulk insert in Java using prepared statements batch update

Community
  • 1
  • 1
pL4Gu33
  • 2,045
  • 16
  • 38