2012-08-03 85 views
1

以下是我的代碼。我正在查詢user_group表並將結果顯示在列表中。我想迭代列表。但得到例外如下。泛型 - 迭代ArrayList時的ClassCastException

List<Group> list= empDAO.getStudentList(); 
    for(Group o :list){ 
     System.out.println("NAME :"+ o.getFirstName()); 
    } 

這是我的DAO方法

public List<Group> getStudentList() { 
    System.out.println("INSIDE DAO"); 
    List<Group> groups = new ArrayList<Group>(); 

    List<Group> rows = jdbcTemplate.queryForList("select * from user_group"); 

    return rows; 
} 

Group.class

public class Group { 
    public Group() { 
     // TODO Auto-generated constructor stub 
    } 

    private String firstName; 

    public String getFirstName() { 
     return firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 
} 

我收到以下異常

Aug 3, 2012 9:22:47 PM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet mvc-dispatcher threw exception 
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.common.form.Group 
     at com.common.controller.HelloWorldController.helloWorld(HelloWorldController.java:28) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
     at java.lang.reflect.Method.invoke(Method.java:597) 
     at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:421) 
     at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:136) 
     at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:326) 
     at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:313) 
     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875) 
     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807) 
     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571) 
     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501) 
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) 
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) 
     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857) 
     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) 
     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
     at java.lang.Thread.run(Thread.java:619) 

誰能告訴如何解決這一問題錯誤?

+0

我通過遵循以下網址中的步驟固定的錯誤:) http://www.vogella.com/articles/SpringJDBC/article.html#jdbc_usage_dao – user1514499 2012-08-04 03:43:09

回答

7

jdbcTemplate.queryForList("select * from user_group");正在返回一個HashMap列表,而不是一個組列表項(您的IDE可能在該行上顯示警告)。

您可能想閱讀關於jdbcTemplate的Spring文檔,並且您可能希望使用RowMapper將每行都轉換爲Group對象。

我也認爲你想用你的用例的方法jdbcTemplate.query(String sql, RowMapper rm),檢查javadoc

3

jdbcTemplate.queryForList不返回List<Group>,它不知道Group是什麼。它返回一個List<Map<String,Object>>(行列表),每一行都是從列名稱到其值的映射。

所以做到這一點:

for (Map<String,Object> m : jdbcTemplate.queryForList("select * from user_group")) { 
    for (Map.Entry<String,Object> e : m.entrySet()) { 
     String columnName = e.key; 
     Object columnValue = e.value; 
     ...build a Group somehow?... 
    } 
} 
+0

我應該使用這裏給出的代碼? http://www.vogella.com/articles/SpringJDBC/article.html#jdbc_usage_dao – user1514499 2012-08-03 17:35:29

+0

我不知道任何有關DAO,但該代碼看起來不錯。 – 2012-08-03 17:58:34