1

I am working on Google charts API and Google Visualization Candlestick Charts expects data to be an array of arrays for it to work .

For example the below format

[
[ "2011-08-01", 136.65, 136.96, 134.15, 136.49 ],
[ "2011-08-02", 135.26, 135.95, 131.50, 131.85 ],
[ "2011-08-05", 132.90, 135.27, 128.30, 135.25 ]
]

This is my SQL by which i am retrieving the data from database

String selectsql = "select current_day ,  open_val , high_val , low_val , close_val  from   historical_data where symbol_name = ?";
while(Rset.next())
            {
                String current_day =  SgxRset.getString("current_day");
                String open_val =  SgxRset.getString("open_val");
                String high_val =  SgxRset.getString("high_val");
                String low_val =  SgxRset.getString("low_val");
                String close_val =  SgxRset.getString("close_val");
            }

Could you please tell me how to construct how to construct an array of arrays ??

sample program

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

public class TestJSON {

    public static void main(String[] args) throws JSONException {
        Employee emp1 = new Employee();
        emp1.setName("Ravi");
        emp1.setName("20");
        Employee emp2 = new Employee();
        emp2.setName("Kiran");
        emp2.setName("20");
        List<Employee> histList = new ArrayList<Employee>();

         Object[] arrayResultData = histList.toArray();

    }
}


public class Employee {

    String name ;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    String age ;

}
7
  • what about using a multidimensional array? Commented Nov 5, 2015 at 13:27
  • I don't think this is an array of array. It has one string and others are float values. Commented Nov 5, 2015 at 13:28
  • You don't want an array of arrays if you don't know how many rows you will have, because you can't resize the array - you'd need an array list of arrays, or something similar. Commented Nov 5, 2015 at 13:29
  • What have you tried? What part are you stuck on? Return a JSON array of arrays, creating a data structure in Java? Something else? Commented Nov 5, 2015 at 13:32
  • creating a data structure in Java Commented Nov 5, 2015 at 13:33

3 Answers 3

1

Create a class which will held all these data together in an object like this with getters and setters:

 public class Data {
            private String current_day;
    private double open_val;
    private double high_val;
    private double low_val;
    private double close_val;

    public Data(String current_day, double open_val, double high_val,
            double low_val, double close_val) {
        this.current_day = current_day;
        this.open_val = open_val;
        this.high_val = high_val;
        this.low_val = low_val;
        this.close_val = close_val;
    }

    public String getCurrent_day() {
        return current_day;
    }

    public void setCurrent_day(String current_day) {
        this.current_day = current_day;
    }

    public double getOpen_val() {
        return open_val;
    }

    public void setOpen_val(double open_val) {
        this.open_val = open_val;
    }

    public double getHigh_val() {
        return high_val;
    }

    public void setHigh_val(double high_val) {
        this.high_val = high_val;
    }

    public double getLow_day() {
        return low_val;
    }

    public void setLow_day(double low_day) {
        this.low_val = low_day;
    }

    public double getClose_day() {
        return close_val;
    }

    public void setClose_day(double close_day) {
        this.close_val = close_day;
    }

    }

Now, store the data in array of objects which you are getting from SQL.

String selectsql = "select current_day ,  open_val , high_val , low_val ,close_val  from   historical_data where symbol_name = ?";
    List<Data> myList = new ArrayList(Data); 
    while(Rset.next())
                {
                    String current_day =  SgxRset.getString("current_day");
                    String open_val =  SgxRset.getString("open_val");
                    String high_val =  SgxRset.getString("high_val");
                    String low_val =  SgxRset.getString("low_val");
                    String close_val =  SgxRset.getString("close_val");

                    Data data = new Data(current_day,open_val,high_val,low_val,close_val);
                    myList.add(data);
                }

For reading data, you can write:

 String a = "[ ";
    for (int i = 0; i < myList.size(); i++) {
        String b = "[ \"" + myList.get(i).getCurrent_day() + "\" , "
                + myList.get(i).getOpen_val() + "],";
        if(i==myList.size() -1)
            a += b;
        else
            a += b+",";
    }
    a += " ]";

Now, for me the output is:

[ [ "2011-08-01" , 136.65],[ "2011-08-02" , 135.26] ]
Sign up to request clarification or add additional context in comments.

13 Comments

thanks but its same thing know what erguant has mentioned ?? how is it different ??
Yeah, it is same, but, while I was writing the answer, some one has already answered.
I think, for creating an array of arrays is a illution here. You need to create a string which looks like an array of arrays. I have personally worked in google visualization. I think the edited code above will help you.
Thanks! Please let me know what happens!
It created an empty array [ ]
|
1

I recommend a List. You don't know what your columns are and how many you have:

List<HistData> histList = new ArrayList<HistData>();
//.. query db
while(Rset.next()){
  HistData histData = new HistData();
  histData.setCurrentDay( SgxRset.getString("current_day"));
  histData.setOpenVal( SgxRset.getString("open_val"));
  histData.setHighVal( SgxRset.getString("high_val"));
  histData.setLowVal( SgxRset.getString("low_val"));
  histData.setCloseVal( SgxRset.getString("close_val"));
  histList.add(histData);
}

HistData is just a POJO, but you can change the datatypes in the bean and the SgxRset retrieval type, so you are not locked in to any array type. Using a List as opposed to an array provides more flexibility in case you are returning many results of unknown length.

public class HistData {
    public HistData(){}
    String currentDay;
    public setCurrentDay(String currentDay){
        this.currentDay = currentDay;
    }
    public getCurrentDay(){
        return currentDay;
    }
    // .. etc.
}

2 Comments

but still how to construct such type of array from List ??
please see my edited part of my question i have created a sample part , how can i display that in form of what google charts expect ??
1

Use a List and the toArray Method

String selectsql = "select current_day ,  open_val , high_val , low_val ,     close_val  from   historical_data where symbol_name = ?";

      List<String[]>resultData = new ArrayList<Stirng[]>();

        while(Rset.next())
                {
                    String[] array = new String[5];

                    array[0] =  SgxRset.getString("current_day");
                    array[1] =  SgxRset.getString("open_val");
                    array[2] =  SgxRset.getString("high_val");
                    array[3] =  SgxRset.getString("low_val");
                    array[4] =  SgxRset.getString("close_val");
                }

        Object[] arrayResultData = resultData.toArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.