so for my school project we will be working with an SQL database and have to show the values on a JFrame panel.
For data to show in a JTable we have to require next syntaxt: (def)
JTable table = new JTable(String[][] data , String[] header);
Since I don't know how many queries I will have to handle we will read them into a ArrayList<String[]> . Every time I press the button this method parseJSON will be called and I will create a new 2D-ArrayList consisting of String arrays aka String[] elements
public class DBTest {
public ArrayList<String[]> dataArr = new ArrayList<>();
public String[] varArr = new String[3];
public ArrayList<String[]> parseJSON(String jsonString){
try {
JSONArray array = new JSONArray(jsonString);
dataArr = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
JSONObject curObject = array.getJSONObject(i);
varArr[0] = curObject.getString("nameSensor");
varArr[1] = curObject.getString("value");
varArr[2] = curObject.getString("unitSensor");
dataArr.add(varArr);
System.out.println(Arrays.toString(varArr));
}
for (int i = 0; i < array.length(); i++) {
System.out.println(dataArr.get(i));
}
return dataArr;
}
catch (JSONException e){
e.printStackTrace();
}
return null;
}
}
For the first print: I will get the proper Values e.g.
Output
["Weight","500","g"]
["Height", "3", "m"]
But once I add these String[] arrays to the ArrayList and print it I will get:
Output
["Height", "3", "m"]
["Height", "3", "m"]