如何使用AbstractMap获取此元组?

时间:2016-08-23 09:49:19

标签: java postgresql collections key-value

我在Java中使用PostgreSQL,我正在执行查询,该查询从数据库中选择坐标。

之前,积分已保存在ArrayList中。现在我想用Point打印出Date。我希望通过AbstractMap获得一对日期和点数并一起打印出来。

但我不知道如何获取日期以及如何在一行中打印坐标和日期。

这是第一堂课的相关部分:

public ArrayList<AbstractMap.SimpleEntry<Point, Date>> getStartingPointCoordinates(String ab, String bis) {
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    ArrayList<AbstractMap.SimpleEntry<Point, Date>> list = new ArrayList<AbstractMap.SimpleEntry<Point,Date>>();

    String query = "select ST_Transform(target_cand_geom, 4326) as geom from fcd_osm_1day WHERE source_candidate_nr = ? and source_time between ? and ? ";

    try {
        // prepare statement
        connection = DatenbankAdapter.getInstance().getConnection();
        statement = connection.prepareStatement(query);
        statement.setInt(1, 1); // source_candiddate_nr = 1, weil dies immer dem Einstieg der Taxifahrt entspricht
        statement.setTimestamp(2, Timestamp.valueOf("2014-10-07 " + ab)); // hole alle daten von
        statement.setTimestamp(3, Timestamp.valueOf("2014-10-07 " + bis)); // hole alle daten bis

        resultSet = statement.executeQuery();

        /*
        * Add the geometry types to the connection. Note that you
        * must cast the connection to the pgsql-specific connection
        * implementation before calling the addDataType() method.
        */
        connection = ((DelegatingConnection) connection).getInnermostDelegate();
        ((org.postgresql.PGConnection) connection).addDataType("geometry", Class.forName("org.postgis.PGgeometry"));

        while (resultSet.next()) {
            Point geom = (Point) ((PGgeometry) resultSet.getObject(1)).getGeometry();
            list.add(geom);
        }

    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            if (resultSet != null)
                resultSet.close();
            if (statement != null)
                statement.close();
            if (connection != null)
                connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    return list;
}

这是测试类:

public class DatenbankAdapterTest {

@Test
public void testCoordinateQuery() {
    DatenbankAdapter datenbankAdapter = DatenbankAdapter.getInstance();
    ArrayList<SimpleEntry<Point,Date>> objects = datenbankAdapter.getStartingPointCoordinates("00:00:00", "24:00:00");

    for (SimpleEntry<Point, Date> list : objects)
        System.out.println(list.getKey() + ", " + list.getValue());

    Assert.assertTrue(!objects.isEmpty());
  }
}

1 个答案:

答案 0 :(得分:0)

将列source_time添加到结果集

String query = "SELECT ST_Transform(target_cand_geom, 4326) AS geom, "
        + "source_time "
        + "FROM fcd_osm_1day "
        + "WHERE source_candidate_nr = ? AND source_time BETWEEN ? AND ?";
...
Timestamp t  = resultSet.getTimestamp();
Date date = new Date(t.getTime()); // java.util.Date
list.add(new AbstractMap.SimpleEntry(geom, date));

这里隐藏着一个问题。 java.util.Date是日期时间类。 java.sql.Date是一个将时间部分归零的子类。因此,上述解决方案。

不需要告诉Map.Entry不好。更好地滥用来自javafx的Pair类:

List<Pair<Point, Date>> getStartingPointCoordinates(String ab, String bis) {
    List<Pair<Point, Date>> list = new ArrayList<>();
    ...
    return list;

BTW Pair<K,V>具有相同的吸气剂:K getKey()V getValue()