• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Execute sql in a loop

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

We are using Spring frmaework and NamedParameterJdbcDaoSupport for all data access operations. I need to execute a select statement in a loop multiple times supplying it different values each time.

I am aware of how to do this with plain jdbc, taken from : http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html

PreparedStatement updateSales;
String updateString = "update COFFEES " +
"set SALES = ? where COF_NAME like ?";
updateSales = con.prepareStatement(updateString);
int [] salesForWeek = {175, 150, 60, 155, 90};
String [] coffees = {"Colombian", "French_Roast", "Espresso",
"Colombian_Decaf", "French_Roast_Decaf"};
int len = coffees.length;
for(int i = 0; i < len; i++) {
updateSales.setInt(1, salesForWeek[i]);
updateSales.setString(2, coffees[i]);
updateSales.executeUpdate();
}


However with the Spring framework all the connection etc is taken care of. So I am leaning towards using the method below in a for loop?

public Object execute(String sql,Map paramMap, PreparedStatementCallback action)

I am not too sure how this will work:

sql - is my select statement
parmap - i have:

for(int i=0; i<= priData.size(); i ++){
Map<String,String> paramMap = new HashMap<String,String>();
paramMap.put("PROD_TYPE",priData.get(i).getProdTypeDescTxt());
paramMap.put("CALL_DATE",String.valueOf(priData.get(i).getPurchDttm()));
}

these are my variable values which i am setting in a loop.

I am not too sure about PreparedStatementCallback action. Has anyone used NamedParamter in a loop? If so please let me know how.

Thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic