I'm updating more than one table at once and, as you will see, it uses a lot of similar looking code. Is there a more efficient way of doing this, perhaps some way of combining the queries?
For context: A user submits a small form containing information about their age, gender and favorite genre of games.
String sex, age;
String[] checkboxes = request.getParameterValues("genre");
sex = request.getParameter("sex");
age = request.getParameter("age");
try(Connection con = ds.getConnection()){
PreparedStatement updateSex = null;
PreparedStatement updateAge = null;
PreparedStatement updateGenre = null;
String updateSexString = "UPDATE sex SET count=count+1 WHERE name=?";
String updateAgeString = "UPDATE age SET count=count+1 WHERE id=?";
String updateGenreString = "";
for(int i = 0; i < checkboxes.length; i++){
updateGenreString = "UPDATE genre SET count=count+1 WHERE id=?";
updateGenre = con.prepareStatement(updateGenreString);
updateGenre.setString(i+1, checkboxes[i].toString());
updateGenre.executeUpdate();
updateGenre.close();
}
updateSex = con.prepareStatement(updateSexString);
updateSex.setString(1, sex);
updateSex.executeUpdate();
updateSex.close();
updateAge = con.prepareStatement(updateAgeString);
updateAge.setString(1, age);
updateAge.executeUpdate();
updateAge.close();
} catch(SQLException e){
e.printStackTrace();
}
checkboxes
! – Vogel612 16 hours ago