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

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

getting data from arraylist<object> and parse it

问题

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

   ArrayList&lt;Object[]&gt; custInfo = new ArrayList&lt;Object[]&gt;(); 
   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&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.

   ArrayList&lt;Object[]&gt; custInfo = new ArrayList&lt;Object[]&gt;(); 
   while(rs.next()){
				String loginId = rs.getString(&quot;LOGIN_ID&quot;);
		    	String  customerId = rs.getString(&quot;CUSTOMER_ID&quot;) ;
		    	String  requestDate = rs.getString(&quot;REQUEST_DATE&quot;) ;
		    	String updateUser  = rs.getString(&quot;UPDATE_USER&quot;) ;
		    	
		    	 Object[] custInfo123 = {loginId, customerId, requestDate, updateUser };
		    	 custInfo.add(custInfo123);
		    }  
   session.setAttribute(&quot;custInfo&quot;, custInfo);

and now, Im using arraylist in another jsp

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

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

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&lt;CustomerInformation&gt; findCustomersById(String customerId) {

    String sql = &quot;SELECT * FROM CUSTOMER WHERE LOGIN_ID = &#39;&quot; + customerId + &quot;&#39;&quot;;

    ArrayList&lt;Object[]&gt; custInfo = new ArrayList&lt;Object[]&gt;();
    while(rs.next()){
        String loginId = rs.getString(&quot;LOGIN_ID&quot;);
        String  customerId = rs.getString(&quot;CUSTOMER_ID&quot;) ;
        String  requestDate = rs.getString(&quot;REQUEST_DATE&quot;) ;
        String updateUser  = rs.getString(&quot;UPDATE_USER&quot;) ;

        Object[] custInfo123 = {loginId, customerId, requestDate, updateUser };
        custInfo.add(custInfo123);
    }
    return custInfo;
}

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:

确定