I have an issue which I cannot sort out. I've been working on it for a few days but cannot manage to find the issue.
I am using MySQLWorkBench to both insert and retrieve/read data from. (Just to let you know where I'm getting the data from).
The issue is my rs.next()
call will grab the according data but will overwriting each index/entry in the array with the more recent entry. My toString
method in my object class also appears to be doing this.
Both these issues go hand in hand.
This is my object class.
public class SensorData {
String tagid;
String attemptstatus;
String timestamp;
public SensorData(String tagid, String attemptstatus, String timestamp) {
super();
this.tagid = tagid;
this.attemptstatus = attemptstatus;
this.timestamp = timestamp;
}
// Constructors depending on which parameters are known
public SensorData(String timestamp, String attemptstatus) {
super();
this.timestamp = timestamp;
this.attemptstatus = attemptstatus;
this.tagid = "unknown";
}
public SensorData(String attempstatus) {
super();
this.tagid = "unknown";
this.attemptstatus = attempstatus;
this.timestamp = "unknown";
}
public String getTagIdValue() {
return tagid;
}
public void setTagIdValue(String tagid) {
this.tagid = tagid;
}
public String getTimeStampValue() {
return timestamp;
}
public void setTimeStampValue(String timestamp) {
this.timestamp = timestamp;
}
public String getAttemptStatus() {
return attemptstatus;
}
public void setAttemptStatus(String attemptstatus) {
this.attemptstatus = attemptstatus;
}
@Override
public String toString() {
return "SensorData [tagid=" + tagid + ", + attemptStatus=" + attemptstatus + ", timestamp=" + timestamp + "]";
}
And this is my call to my database and trying to get all the relevant information.
`private String retrieveSensorData(String sensorname) {
String selectSQL = "select * from sensorusage where tagid='" +
sensorname + "' order by timestamp asc";
ResultSet rs;
ArrayList<SensorData> allSensors = new ArrayList<SensorData>();
try {
rs = stmt.executeQuery(selectSQL);
SensorData oneSensor = new SensorData("","","");
while (rs.next()) {
oneSensor.setTagIdValue(rs.getString("tagid"));
oneSensor.setAttemptStatus(rs.getString("attemptstatus"));
oneSensor.setTimeStampValue(rs.getString("timestamp"));
allSensors.add(oneSensor);
System.out.println(allSensors.toString());
}
} catch (SQLException ex) {
System.out.println("Error in SQL " + ex.getMessage());
}
String allSensorsJson = gson.toJson(allSensors);
System.out.println("-----------------");
System.out.println(allSensorsJson);
return allSensorsJson;
}`
Theoretically this should work, however, this is the output in my console.
This is the console output of the allSensors
array
[{"tagid":"Card3","attemptstatus":"Failed","timestamp":"2018-11-29 00:09:53.0"},{"tagid":"Card3","attemptstatus":"Failed","timestamp":"2018-11-29 00:09:53.0"},{"tagid":"Card3","attemptstatus":"Failed","timestamp":"2018-11-29 00:09:53.0"},{"tagid":"Card3","attemptstatus":"Failed","timestamp":"2018-11-29 00:09:53.0"},{"tagid":"Card3","attemptstatus":"Failed","timestamp":"2018-11-29 00:09:53.0"}]
And this is the output of my toString()
in my object class, notice how each entry (the timestamp key will help you to see this) gets overwritten with the latest entry:
[SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-28 23:43:26.0]]
[SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-28 23:53:05.0], SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-28 23:53:05.0]]
[SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-28 23:54:48.0], SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-28 23:54:48.0], SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-28 23:54:48.0]]
[SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-28 23:56:05.0], SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-28 23:56:05.0], SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-28 23:56:05.0], SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-28 23:56:05.0]]
[SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-29 00:09:53.0], SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-29 00:09:53.0], SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-29 00:09:53.0], SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-29 00:09:53.0], SensorData [tagid=Card3, + attemptStatus=Failed, timestamp=2018-11-29 00:09:53.0]]
In the end, I have an output on my webpage tat has the correct number of records but all with the same data.
The program does result the correct number of results e.g. the correct number of entries with a given tagid but it constantly appends the latest record for as many record there are i.e. if there are 10 records where the tagid = "card10" then I will get 10 records of the same data. The program will appends/make 10 instances of the latest information.
Does anyone know where the problem lies?
Thank you.