Hi Lahiru,

I believe second method is better than the clone option. because
1) in clone option you are creating 11 JsonObject instances if you have 10
elements in resultset. (you are explicitly creating one instance before the
loop and in clone method there will be another 10 instances). But in second
option only 10 will be created (if you don't create new instance when you
define the reference).
2) So obliviously additional operations also invoked in clone option.
3) other than that second method is very easy to understand :)

Thanks,
Dinesh

On Fri, Dec 5, 2014 at 11:10 AM, Lahiru Cooray <[email protected]> wrote:

> Hi,
>
> I'm trying to execute a SQL and store the result in a Json Array.
>
> What would be the most efficient code block??
>
>
>
> JSONObject obj = new JSONObject();
>
> while (rs.next()) {
>
> obj.put("NAME", rs.getString("NAME"));
>
> jsonArr.add(obj.clone());
>
> }
>
> * Or*
>
> while (rs.next()) {
>
> obj = new JSONObject();
>
> obj.put("NAME", rs.getString("NAME"));
>
> jsonArr.add(obj);
>
> }
>
> On Fri, Dec 5, 2014 at 11:05 AM, Lahiru Cooray <[email protected]> wrote:
>
>>
>> Hi,
>>
>> I'm trying to execute a SQL and store the result in a Json Array.
>>
>> What would be the most efficient code block??
>>
>>
>>
>>  JSONObject obj = new JSONObject();
>>
>> while (rs.next()) {
>>
>> obj.put("NAME", rs.getString("NAME"));
>>
>> jsonArr.add(obj.clone());
>>
>> }
>>
>> * Or*
>>
>> while (rs.next()) {
>>
>> JSONObject obj = new JSONObject();
>>
>> obj.put("NAME", rs.getString("NAME"));
>>
>> jsonArr.add(obj);
>>
>> }
>>
>>
>> --
>> *Lahiru Cooray*
>> Software Engineer
>> WSO2, Inc.;http://wso2.com/
>> lean.enterprise.middleware
>>
>> Mobile: +94 715 654154
>>
>
>
>
> --
> *Lahiru Cooray*
> Software Engineer
> WSO2, Inc.;http://wso2.com/
> lean.enterprise.middleware
>
> Mobile: +94 715 654154
>
> _______________________________________________
> Dev mailing list
> [email protected]
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>


-- 

*Dinesh J. Weerakkody*
Software Engineer
WSO2 Inc.
lean | enterprise | middleware
M : +94 727 361788 | E : [email protected] | W : www.wso2.com
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to