gpt4 book ai didi

java - 如何从 Resultset 构造数组数组

转载 作者:行者123 更新时间:2023-11-29 04:56:40 25 4
gpt4 key购买 nike

我正在研究 Google 图表 API,并且Google Visualization Candlestick Charts 期望数据是一个数组数组才能正常工作。

例如下面的格式

[
[ "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 ]
]

这是我从数据库中检索数据的 SQL

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");
}

你能告诉我如何构造数组的数组吗??

示例程序

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 ;

}

最佳答案

创建一个类,它将所有这些数据一起保存在一个对象中,就像这样,带有 getter 和 setter:

 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;
}

}

现在,将数据存储在您从 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);
}

读取数据可以这样写:

 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 += " ]";

现在,对我来说输出是:

[ [ "2011-08-01" , 136.65],[ "2011-08-02" , 135.26] ]

关于java - 如何从 Resultset 构造数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33545752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com