下面是一个将行加载到 Records 数组中的函数。
private static List<Record> LoadFromDb(String query, Connection connection, long take, long skip, Date minDate)
{
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Logger logger = null;
Record record = new Record();
ArrayList<Record> records = new ArrayList<Record>();
try
{
preparedStatement = connection.prepareStatement(query);
preparedStatement.setDate(1, new java.sql.Date(minDate.getTime()));
preparedStatement.setLong(2, take);
preparedStatement.setLong(3, skip);
resultSet = preparedStatement.executeQuery();
while (resultSet.next())
{
record.by = resultSet.getString(1);
record.category = resultSet.getString(2);
record.channel = resultSet.getString(3);
record.event_id = resultSet.getLong(4);
record.message = resultSet.getString(5);
record.package_type = resultSet.getString(6);
record.sensor_type = resultSet.getString(7);
record.serial_num = resultSet.getString(8);
record.stamp = resultSet.getTimestamp(9);
record.target = resultSet.getString(10);
record.tbname = resultSet.getString(11);
record.timezone = resultSet.getString(12);
record.uuid = UUID.fromString(resultSet.getString(13));
records.add(record);
}
}
catch (Exception ex)
{
logger = Logger.getLogger(DbLoadSave.class.getName());
logger.log(Level.SEVERE, ex.getMessage(), ex);
}
finally
{
try
{
if (resultSet != null)
{
resultSet.close();
}
if (preparedStatement != null)
{
preparedStatement.close();
}
}
catch (SQLException ex)
{
logger = Logger.getLogger(DbLoadSave.class.getName());
logger.log(Level.WARNING, ex.getMessage(), ex);
}
}
return records;
}
Record类如下
import java.sql.Timestamp;
import java.util.UUID;
final class Record
{
public String by;
public String category;
public String channel;
public long event_id;
public String message;
public String package_type;
public String sensor_type;
public String serial_num;
public Timestamp stamp;
public String target;
public String tbname;
public String timezone;
public UUID uuid;
public Record()
{
by = "";
category = "";
channel = "";
event_id = 0;
message = "";
package_type = "";
sensor_type = "";
serial_num = "";
stamp = null;
target = "";
tbname = "";
timezone = "";
uuid = null;
}
}
传递给 LoadFromDb() 的查询是
SELECT by, category, channel, event_id, message, package_type, sensor_type, serial, stamp, target, tbname, timezone, uuid FROM all_events WHERE stamp > ? LIMIT ? OFFSET ?;
当 LoadFromDb() 完成执行时,'records' 数组中的每个对象都完全相同。为什么是这样?您可以提供的任何帮助将不胜感激。