Solved

java iterator using list and map

Posted on 2011-03-16
5
470 Views
Last Modified: 2012-05-11
Below Im trying to put the resultset of the queryForList into the functions but its not working out the way I want. I know im doing something wrong. Where ever there is a al2 in the code thats where I want to put the resultset. The way I was trying to do it was with a arraylist (al2) then getting the indexed item and putting it into the method called. I tried  Iterator<Map> iterator=list.iterator(); but it tells me to go use jre1.5...which is wierd.      

List list= getJdbcTemplate().queryForList(mManifestISeriesQueries.getTerminalPrinterList(), PARAMS, Get_Term_Printer_List);
                  
                  
                  Map map = (Map) list.iterator();
                  while(((Iterator) map).hasNext())
                  {
                        ((ResultSet) map).next();
                        _printerDefaultsVO = new manifestTerminalPrinterDefaultsVO();
                        _printerDefaultsVO.setTermNum(Integer.parseInt(dto.getpTerminal()));
                        _printerDefaultsVO.setPrinterId((String) al2.get(1));
                        _printerDefaultsVO.setPrinterDesc((String) al2.get(2));
                        _printerDefaultsVO.setTerminalDefault(((String) al2.get(3)).charAt(0));
                        _printerList.add(_printerDefaultsVO);
                        
                  }

Any help would be appreciated.
0
Comment
Question by:Jtw549
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35148985
If you want to iterate through ResultSet, it is much easier.
You just say

while(resulSet.next()){
String s1 = resultSet.getString(1);
int num = resultSet.getInt(2);


then use the values  in any functyions
ettc.

} //end of loop through ResultSet

0
 

Author Comment

by:Jtw549
ID: 35149161
Is that even possible to do with Spring, without doing a resultSet Extractor?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35149487
As far as I can understand, it should be possible to do it all directly, once
you have access to ResultSet object - these are the basic properities of resultSet which we are using.

ResultSetExtractor is supposed to simplify your interactions with database and to deal better with
error messages, etc., see, e.g.
http://www.vogella.de/articles/SpringJDBC/article.html
 but the excerpt in my posting above which relies on basic properities of ResultSet
should still work.
Well, I don't have much experience wit Spring, perhaps we'll hear from someone
who is aware of some implications for other aprts of your spring web app.
0
 
LVL 35

Accepted Solution

by:
mccarl earned 250 total points
ID: 35152992
To answer the question at hand, we would need to know what Get_Term_Printer_List is in the first line of your code, ie. what type is that variable because that determines which overloaded queryForList method is called which changes what is in the resultant list.

Are you against using Spring RowMapper interfaces? I think it would make this a lot easier. It would look something like this...

 
List<manifestTerminalPrinterDefaultsVO> _printerList = getJdbcTemplate.query(mManifestISeriesQueries.getTerminalPrinterList(), PARAMS, new RowMapper<manifestTerminalPrinterDefaultsVO>() {
  @Override
  public manifestTerminalPrinterDefaultsVO mapRow(ResultSet rs, int rowNum) throws SQLException
  {
    manifestTerminalPrinterDefaultsVO _printerDefaultsVO = new manifestTerminalPrinterDefaultsVO();
    _printerDefaultsVO.setTermNum(Integer.parseInt(dto.getpTerminal()));
    _printerDefaultsVO.setPrinterId(rs.getString(1));
    _printerDefaultsVO.setPrinterDesc(rs.getString(2));
    _printerDefaultsVO.setTerminalDefault((rs.getString(3)).charAt(0));
    
    return _printerDefaultsVO;
  }
});

Open in new window


(Note: the above was written without testing, or even trying to compile, but it should be pretty close and give you the idea)

What is happening here is that for each row that the SQL returns, Spring passes the ResultSet for it to your mapRow method, where you create and return the object that represents that row. And then (under the covers), Spring creates an List and adds each of the object returned from mapRow to that list, and when all is finished, it passes the built list back to you.
0
 

Author Closing Comment

by:Jtw549
ID: 35156136
actually....that worked Mccarl...thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question