0

Today I have trouble to insert data into Postgres database using procedure. I have one procedure to insert record to it , but it is so slow with many record. Can help to fix my procedure.

void insertRecord(ArrayList<Product> pro)throws Exception{ Class.forName("org.postgresql.Driver"); con=DriverManager.getConnection("jdbc:postgresql://localhost:5433/dbStock?","rina","2001"); //con.setAutoCommit(false); CallableStatement stmt=con.prepareCall("{call MyInsert(?,?,?,?,?)}"); for(Product p:pro){ stmt.setInt(1, p.getId()); stmt.setString(2, p.getName()); stmt.setInt(3, p.getUnitprice()); stmt.setInt(4, p.getQty()); stmt.setString(5, p.getImportedDate()); //pstm.executeUpdate(); stmt.execute(); } //con.commit(); } //main Method public static void main(String[] args)throws Exception { ArrayList<Product>pro=new ArrayList<>(); for(int i=1;i<=1_00000;i++){ pro.add(new Product(i,"coca",12,12,"2014")); } System.out.println("Done"); Long start=System.currentTimeMillis(); new dbObjectDriver().insertRecord(pro); long stop=System.currentTimeMillis(); System.out.println(stop-start); } 
5
  • 2
    see this SO post stackoverflow.com/questions/3784197/… for batch insert. Commented Jun 11, 2016 at 13:41
  • why do you need a stored procedure for that? Commented Jun 11, 2016 at 14:13
  • yes you should indeed consider using a batch insert Commented Jun 11, 2016 at 14:14
  • also whatever you're doing in your procdure make sure theres no unnecessary full table scans in your queries. That can have an impact at higher volumes Commented Jun 11, 2016 at 14:29
  • i am not clear about addbatch Commented Jun 11, 2016 at 15:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.