java - Is it a good practice to use foreach for multiple insertions on a database? -
i have message , many recipients, want know when want save it.. use statement.execute() each recipient message have or considered "hard-coding"
public void save(int id,string subject, string body, arraylist<string> emails){ string sq = "call savemessage(?,?)"; callablestatement st = this.connection.preparecall(sq); st.setint(1,id); st.setstring(2, subject); st.setstring(3, body); st.execute(); for(string e:emails){ query = "call saverecipientbymessage(?,?)"; callablestatement st2 = this.connection.preparecall(query); st2.setint(1,id); st2.setstring(2,e); st2.execute(); } }
thanks reading.
the foreach
ok better jdbc practice here use batch.
Comments
Post a Comment