【发布时间】:2016-02-19 22:24:14
【问题描述】:
我有一个JdbcTemplate 查询,它使用queryForList 从数据库表中获取所有记录。我有一个主要在ArrayLists 上运行的类,并且想将列表转换为ArrayList<Customer>。在调试器中,它可以很好地检索记录,但我无法弄清楚如何将Object 转换为Customer 并创建ArrayList 类型的Customer。这是我的代码:
public static ArrayList<Customer> getCustomers(){
String query = "SELECT customerId,lastName,firstName,email,address,city,state,zip FROM " +
"Customers";
List customerList = jdbcTemplate.queryForList(query);
ArrayList<Customer> customerArrayList = new ArrayList(customerList);
return customerArrayList;
}
【问题讨论】:
-
queryForList()是什么样的?能否提供更多代码? -
您的 "在 ArrayLists 上运行的类" 是否有任何理由只能在 ArrayLists 上运行?为什么它不适用于任意类型的
List<>?也就是说,与其尝试从queryForList强制一个ArrayList,不如让你的其他类在任何列表实现上工作。
标签: java arraylist casting jdbctemplate