Is it good practice to use foreach for multiple database entries?

I have a message and many recipients that I want to know when I want to save it. Is it good to use Statement.execute () for each recipient that has a message, or can it be considered "difficult" -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 for reading.

+4
source share
1 answer

foreach ok, but the best practice of JDBC here is to use the package.

+2
source

Source: https://habr.com/ru/post/1682260/


All Articles