I am optimizing a code written a long time ago by a developer that I never met. I came across a method that requires modification. The first thing that came to mind is to use stored procedure. Maybe there are better ways of achieving this hence this question. The code goes this way:
public void execute()
{
String query = "select a, b, e from table1";
....
ResultSet rs = stmt.executeQuery(query);
String query2 = null;
List<Integer> list1 = ....
List<Integer> list2 = ....
while(rs.next)
{
query2 = "select count(*) as rowcount from vw_view1 where f='" + rs.getString("a") + "' and d='" + rs.getString("b") + "'";
.....
ResultSet rs2 = stmt2.executeQuery(query2);
list1.add(rs2.getInt(rowcount));
query3 = "select count(*) as rowcount from vw_view1 where c='" + rs.getString("a") + "' and e='" + rs.getString("e") + "'";
.....
ResultSet rs3 = stmt3.executeQuery(query3);
list2.add(rs3.getInt(rowcount));
}
}
Apart from using a stored procedure, is there a better way of avoiding unnecessary trips to the database in this method.