简体   繁体   English

如何在jdbc中获取记录相同的列名称值?

[英]How to get records same column name values in jdbc?

I had retrieved all records from db which have fields name,email,id,manager and I need to get all data records which have same manager and save it to list how can I to do it? 我已经从db中检索了所有具有字段name,email,id,manager的记录,我需要获取具有相同管理器的所有数据记录并将其保存以列出如何执行该操作?

I had retrieved values from db has 我已经从数据库检索了值

 List<Map<String, String> dbData=new ArrayList<>();
while(query.next()){
 LinkedHashMap<String,String> rowData=new LinkedHashMal<>();
 for(columns=1;columns<total columns; columns++){
 rowData.put(columnNames,query.getString(columnNames.get(columns));
 }
 dbData.add(rowData);
}

Where columnNames is header list which contains all headers of the column. 其中columnNames是标题列表,其中包含该列的所有标题。

So I need to get all records of same manager and export it to separate Excel file 所以我需要获取同一管理者的所有记录并将其导出到单独的Excel文件中

If you want to filter by a certain manager you have to include the manager as a WHERE clause of your query. 如果要按某个管理器进行过滤,则必须将该管理器作为查询的WHERE子句包括在内。 Something like: 就像是:

SELECT name, email, id, manager FROM Table WHERE manager = 'manager1'

Once you have all records you wanted you can iterate through your ResultSet and add them too your list. 拥有所有想要的记录后,您可以遍历ResultSet并将它们也添加到列表中。

For more info you can check this link 有关更多信息,您可以检查此链接

First, you read all the rows from the database into Java objects. 首先,您将数据库中的所有行读入Java对象。

Assuming you read the data into a List<Record> , you can then group the data using Java 8 streams: 假设您将数据读入List<Record> ,则可以使用Java 8流对数据进行分组:

List<Record> rows = ...
Map<String, List<Record>> byManager = rows.stream()
        .collect(Collectors.groupingBy(Record::getManager, Collectors.toList()));

UPDATE 更新

If the record is a Map<String, String> mapping column name to stringified value, the code would be: 如果记录是Map<String, String>将列名映射到字符串化值,则代码为:

List<Map<String, String>> rows = ...
Map<String, List<Map<String, String>>> byManager = rows.stream()
        .collect(Collectors.groupingBy(row -> row.get("manager"), Collectors.toList()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM