您好我在oracle中对表运行一个简单的select查询并获取结果集。比如select username, responsibility, project from mytable.
结果集包含用户详细信息。为每个用户名返回多行,其中包含责任和项目的不同值。
现在我想从这个结果集中获取一个列表,每个用户名都有一个List,不同的值以逗号分隔的字符串连接。
因此,如果Sam在结果集中有多个条目,那么我的操作输出应该给我:
UserList =
["Sam", "responsibility1,responsibility2,responsibility3...", "dept1,dept2,dept3.."],
[Some other User],
[and so on..]
稍后我会将其写入csv文件
出于兼容性原因,我无法在查询本身中执行此操作,我们将来必须支持多个数据库版本
我如何在java或groovy中这样做?
由于
答案 0 :(得分:1)
Java非常简单。 您需要一个类来为每个用户建模。 您需要用户名的地图。 每个用户都包含责任清单和部门清单。 然后迭代结果集,从每行的地图中找到用户,并将责任和部门添加到该用户
您需要代码还是足够好?
HTH
编辑:这是一些Java启动代码: (未检查语法或错误;])
public class User {
private final List<String> responsibility = new ArrayList<String>();
private final List<String> department = new ArrayList<String>();
...standard getters and setters
}
// Your code to do the read
public void executeRead() {
... obtain the resultset somehow here
Map<String, User> usernameToUser = new HashMap<String, User>():
while (rs.next) {
String username = rs.getString("username");
User user = usernameToUser.get(username);
if (user == null) {
user = new User(); // Create and remember a user the first time you see them
usernameToUser.put(username, user);
}
String responsiblity = rs.getString("responsiblity");
String department = rs.getString("department");
user.addResponsibility(responsibility);
user.addDepartment(department);
}
rs.close();
// Now you have the data structure of users in memory you can output
// it in whichever format you like e.g. HTML, CSV, etc
// Probably best to do this step in a totally separate place that can
// be switched out for a different output format in future.
}