英文:
getting data from arraylist<object> and parse it
问题
我创建了一个ArrayList<Object[]>来存储值。
2.
ArrayList<Object[]> custInfo = new ArrayList<Object[]>();
while(rs.next()){
String loginId = rs.getString("LOGIN_ID");
String customerId = rs.getString("CUSTOMER_ID") ;
String requestDate = rs.getString("REQUEST_DATE") ;
String updateUser = rs.getString("UPDATE_USER") ;
Object[] custInfo123 = {loginId, customerId, requestDate, updateUser };
custInfo.add(custInfo123);
}
session.setAttribute("custInfo", custInfo);
现在,我在另一个jsp文件中使用了ArrayList:
ArrayList<String> custInfo = (ArrayList<String>)session.getAttribute("custInfo");
现在我想从这个ArrayList<Object>中获取数据,但我不知道如何做到。
我想要的值类似于:
customer1-loginid,customerid,requestdate,updateuser
customer2-loginid,customerid,requestdate,updateuser
customer3-loginid,customerid,requestdate,updateuser
我该如何做到这一点?
英文:
I craet arraylist<object[]> for storing values
2.
ArrayList<Object[]> custInfo = new ArrayList<Object[]>();
while(rs.next()){
String loginId = rs.getString("LOGIN_ID");
String customerId = rs.getString("CUSTOMER_ID") ;
String requestDate = rs.getString("REQUEST_DATE") ;
String updateUser = rs.getString("UPDATE_USER") ;
Object[] custInfo123 = {loginId, customerId, requestDate, updateUser };
custInfo.add(custInfo123);
}
session.setAttribute("custInfo", custInfo);
and now, Im using arraylist in another jsp
ArrayList<String> custInfo = (ArrayList<String>)session.getAttribute("custInfo");
Now I want to get data from this arraylist<object> but i dont know how to do that.
I want value like
customer1-loginid, customerid, requestdate, updateuser
customer2- loginid, customerid, requestdate, updateuser
customer3- loginid, customerid, requestdate, updateuser
how can i do that.
答案1
得分: 1
你可以对每个对象进行迭代,以找到具有正确属性的对象,但更好的方法是使用带有类型化对象的行映射器,例如 CustomerInformation
,就像这个例子一样:https://mkyong.com/spring/spring-jdbctemplate-querying-examples/
如果你想要通过特定属性访问它,我建议使用一个 HashMap,以该属性作为键,这样你只需对具有该字段值的对象进行迭代,或者直接为该键返回一个列表。
更好的解决方案是仅从数据库查询与这些属性匹配的行。
public List<CustomerInformation> findCustomersById(String customerId) {
String sql = "SELECT * FROM CUSTOMER WHERE LOGIN_ID = '" + customerId + "'";
ArrayList<Object[]> custInfo = new ArrayList<Object[]>();
while(rs.next()){
String loginId = rs.getString("LOGIN_ID");
String customerId = rs.getString("CUSTOMER_ID");
String requestDate = rs.getString("REQUEST_DATE");
String updateUser = rs.getString("UPDATE_USER");
Object[] custInfo123 = {loginId, customerId, requestDate, updateUser};
custInfo.add(custInfo123);
}
return custInfo;
}
英文:
You can iterate on each object to find the one with the right attribute but a better approach is to use a rowmapper with typed objects e.g. CustomerInformation
like this example : https://mkyong.com/spring/spring-jdbctemplate-querying-examples/
and if you want to access it with a specific property I would rather use a HashMap with this property as key so you'll have to iterate only on objects whitches have that field value or directly return a list for this key.
An even better solution is to query from database only rows matching theises properties.
public List<CustomerInformation> findCustomersById(String customerId) {
String sql = "SELECT * FROM CUSTOMER WHERE LOGIN_ID = '" + customerId + "'";
ArrayList<Object[]> custInfo = new ArrayList<Object[]>();
while(rs.next()){
String loginId = rs.getString("LOGIN_ID");
String customerId = rs.getString("CUSTOMER_ID") ;
String requestDate = rs.getString("REQUEST_DATE") ;
String updateUser = rs.getString("UPDATE_USER") ;
Object[] custInfo123 = {loginId, customerId, requestDate, updateUser };
custInfo.add(custInfo123);
}
return custInfo;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论