从ArrayList<object>获取数据并解析它

huangapple go评论93阅读模式
英文:

getting data from arraylist<object> and parse it

问题

我创建了一个ArrayList<Object[]>来存储值。
2.

  1. ArrayList&lt;Object[]&gt; custInfo = new ArrayList&lt;Object[]&gt;();
  2. while(rs.next()){
  3. String loginId = rs.getString("LOGIN_ID");
  4. String customerId = rs.getString("CUSTOMER_ID") ;
  5. String requestDate = rs.getString("REQUEST_DATE") ;
  6. String updateUser = rs.getString("UPDATE_USER") ;
  7. Object[] custInfo123 = {loginId, customerId, requestDate, updateUser };
  8. custInfo.add(custInfo123);
  9. }
  10. session.setAttribute("custInfo", custInfo);

现在,我在另一个jsp文件中使用了ArrayList:

  1. ArrayList&lt;String&gt; custInfo = (ArrayList&lt;String&gt;)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.

  1. ArrayList&lt;Object[]&gt; custInfo = new ArrayList&lt;Object[]&gt;();
  2. while(rs.next()){
  3. String loginId = rs.getString(&quot;LOGIN_ID&quot;);
  4. String customerId = rs.getString(&quot;CUSTOMER_ID&quot;) ;
  5. String requestDate = rs.getString(&quot;REQUEST_DATE&quot;) ;
  6. String updateUser = rs.getString(&quot;UPDATE_USER&quot;) ;
  7. Object[] custInfo123 = {loginId, customerId, requestDate, updateUser };
  8. custInfo.add(custInfo123);
  9. }
  10. session.setAttribute(&quot;custInfo&quot;, custInfo);

and now, Im using arraylist in another jsp

  1. ArrayList&lt;String&gt; custInfo = (ArrayList&lt;String&gt;)session.getAttribute(&quot;custInfo&quot;);

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,以该属性作为键,这样你只需对具有该字段值的对象进行迭代,或者直接为该键返回一个列表。

更好的解决方案是仅从数据库查询与这些属性匹配的行。

  1. public List<CustomerInformation> findCustomersById(String customerId) {
  2. String sql = "SELECT * FROM CUSTOMER WHERE LOGIN_ID = '" + customerId + "'";
  3. ArrayList<Object[]> custInfo = new ArrayList<Object[]>();
  4. while(rs.next()){
  5. String loginId = rs.getString("LOGIN_ID");
  6. String customerId = rs.getString("CUSTOMER_ID");
  7. String requestDate = rs.getString("REQUEST_DATE");
  8. String updateUser = rs.getString("UPDATE_USER");
  9. Object[] custInfo123 = {loginId, customerId, requestDate, updateUser};
  10. custInfo.add(custInfo123);
  11. }
  12. return custInfo;
  13. }
英文:

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.

  1. public List&lt;CustomerInformation&gt; findCustomersById(String customerId) {
  2. String sql = &quot;SELECT * FROM CUSTOMER WHERE LOGIN_ID = &#39;&quot; + customerId + &quot;&#39;&quot;;
  3. ArrayList&lt;Object[]&gt; custInfo = new ArrayList&lt;Object[]&gt;();
  4. while(rs.next()){
  5. String loginId = rs.getString(&quot;LOGIN_ID&quot;);
  6. String customerId = rs.getString(&quot;CUSTOMER_ID&quot;) ;
  7. String requestDate = rs.getString(&quot;REQUEST_DATE&quot;) ;
  8. String updateUser = rs.getString(&quot;UPDATE_USER&quot;) ;
  9. Object[] custInfo123 = {loginId, customerId, requestDate, updateUser };
  10. custInfo.add(custInfo123);
  11. }
  12. return custInfo;
  13. }

huangapple
  • 本文由 发表于 2020年9月29日 16:06:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64115383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定