Skip to main content

Is there a more efficient way of executing multiple sqlSQL prepared statements at once?

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();
}

Is there a more efficient way of executing multiple sql prepared statements at once?

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();
}

Is there a more efficient way of executing multiple SQL prepared statements at once?

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();
}
added 600 characters in body
Source Link
crmepham
  • 657
  • 2
  • 9
  • 16

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();
}

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?

String sex, age;

sex = request.getParameter("sex");
age = request.getParameter("age");

try(Connection con = ds.getConnection()){
    PreparedStatement updateSex = null;
    PreparedStatement updateAge = null;
    
    String updateSexString = "UPDATE sex SET count=count+1 WHERE name=?";
    String updateAgeString = "UPDATE age SET count=count+1 WHERE id=?";
    
    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();
}

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();
}
edited tags
Link
Heslacher
  • 50.8k
  • 5
  • 82
  • 177
Source Link
crmepham
  • 657
  • 2
  • 9
  • 16
Loading