从 Java 中的列表中提取特定值

Pulling specific values from a List in Java

我正在从数据库中提取数据并将其存储在列表中,现在当我使用 list.get(index) 从列表中提取条目时,我得到的输出是 {hours_worked=1.5}.

我只想从数据库中获取 1.5 而不是工作时间。

这是我正在使用的检索列表的方法:

    public List totalHoursByActivity(int custId, int activityId) throws ClassNotFoundException, SQLException {
    this.openConnection();

    List totalHours = new ArrayList();

    String sqlStmt = "SELECT hours_worked\n"
            + "from work_entry\n"
            + "JOIN activity\n"
            + "ON work_entry.activity_id = activity.activity_id\n"
            + "JOIN customer\n"
            + "ON work_entry.customer_id = customer.customer_id\n"
            + "WHERE customer.customer_id = " + custId + " and activity.activity_id = " + activityId;

    totalHours = db.findRecords(sqlStmt);

    return totalHours;
}

有没有办法从列表中只取出双倍?或者提取数据并获得该值的最佳方法是什么?

编辑:这就是 findRecords 的作用:

    public List findRecords(String sqlStmt) throws SQLException {
    final List allRecords = new ArrayList();
    Map record = null;
    Statement stmt = null;
    ResultSet rs = null;
    ResultSetMetaData metaData = null;

    stmt = conn.createStatement();
    rs = stmt.executeQuery(sqlStmt);
    metaData = rs.getMetaData();
    int columnCount = metaData.getColumnCount();

    while (rs.next()) {
        record = new HashMap();
        for (int i = 1; i <= columnCount; i++) {
            record.put(metaData.getColumnName(i), rs.getObject(i));
        }
        allRecords.add(record);
    }

    return allRecords;
}

您的 findRecords returns 地图列表,每个地图都将列名作为键和相应的值。

在你的情况下,密钥是 'work_entry',如 sql 语句所建议的那样, 并且值为 1.5,{hours_worked=1.5} 是地图上 toString 方法的默认输出。

如果需要时间,需要调用totalHours.get(i).get("hours_worked").

如果您使用泛型,您会看到对象类型。