使用JDBC从Postgres检索的行是完全相同的

时间:2016-04-16 20:58:50

标签: java jdbc jdbc-postgres

下面是一个将行加载到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()完成执行时,&#39;中的每个对象都会记录&#39;数组完全一样。为什么是这样?我们非常感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:2)

您需要将new Record()语句移到while循环中。

   // Record record = new Record();  // <-- REMOVE THIS
   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 record = new Record();  // <-- MOVE TO HERE
            record.by = resultSet.getString(1);
            record.category = resultSet.getString(2);