gpt4 book ai didi

java - 使用 JDBC 从 Postgres 检索的行都是相同的

转载 作者:行者123 更新时间:2023-12-02 03:42:35 24 4
gpt4 key购买 nike

下面是一个将行加载到记录数组中的函数。

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”数组中的每个对象都完全相同。为什么是这样?非常感谢您能提供的任何帮助。

最佳答案

您需要将 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);

关于java - 使用 JDBC 从 Postgres 检索的行都是相同的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36669676/

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