Java 从数据库中检索多个表中的数据
我在数据库中有一些表。他们有一些特殊的模式。例如,考虑我有TabeEngress,然后一些其他模式,如:Java 从数据库中检索多个表中的数据,java,jdbc,Java,Jdbc,我在数据库中有一些表。他们有一些特殊的模式。例如,考虑我有TabeEngress,然后一些其他模式,如: table 1:employee table 2:employee_X table 3:employee_Y 我想检查这些表是否包含数据,如果它们包含数据,那么我必须为每个表调用一些方法。我使用以下代码来检索 DatabaseMetaData meta = con.getMetaData(); ResultSet res = meta.getTables(null, null, "My_T
table 1:employee
table 2:employee_X
table 3:employee_Y
我想检查这些表是否包含数据,如果它们包含数据,那么我必须为每个表调用一些方法。我使用以下代码来检索
DatabaseMetaData meta = con.getMetaData();
ResultSet res = meta.getTables(null, null, "My_Table_Name", new String[] {"TABLE"});
while (res.next()) {
if(rs.getStrin(3).equals(employee)){
//my code to write data of this table to a file
}
if(rs.getString(3).equals(employee_X)){
//my code to write data to the same file
}
if(rs.getString(3).equals(employee_Y)){
//code to write data to the same file from this table
}
}
代码运行良好,但是我如何能够一次从所有这些表中检索数据,而不是使用三个检查。如果这些表中有任何一个包含数据,我想将其写入我的文件。如何以更少的代码行高效地执行此操作
如果有人能建议一种方法来检查这些表中的每个表是否包含数据,然后我可以调用我的代码将数据写入文件,那就太好了。您可以在复杂的查询中使用
UNION
语句。请检查示例:
SELECT id, name FROM employee WHERE id = ?
UNION
SELECT id, name FROM employee_x WHERE id = ?
UNION
...
Set<String> requiredTables = new HashSet<>();
// fill set with required tables for result query
requiredTables.add("employee");
ResultSet res = meta.getTables(null, null, "My_Table_Name",
new String[] {"TABLE"});
List<String> existentTables = new LinkedList<>();
while(res.next()) {
if (requiredTables.contains(res.getString(3)) {
existentTables.add(res.getString(3));
}
}
String query = existentTables.stream().map(table -> String.format("SELECT * FROM %s", table)).collect(Collectors.joinning(" UNION "));
您还可以使用UNION ALL
语句代替UNION
。主要区别是UNION
返回不重复的唯一结果集,UNION ALL
允许重复。请查看此链接以了解有关union
声明的详细说明
如果需要使用自定义筛选表创建UNION
查询,请检查示例:
SELECT id, name FROM employee WHERE id = ?
UNION
SELECT id, name FROM employee_x WHERE id = ?
UNION
...
Set<String> requiredTables = new HashSet<>();
// fill set with required tables for result query
requiredTables.add("employee");
ResultSet res = meta.getTables(null, null, "My_Table_Name",
new String[] {"TABLE"});
List<String> existentTables = new LinkedList<>();
while(res.next()) {
if (requiredTables.contains(res.getString(3)) {
existentTables.add(res.getString(3));
}
}
String query = existentTables.stream().map(table -> String.format("SELECT * FROM %s", table)).collect(Collectors.joinning(" UNION "));
Set requiredTables=new HashSet();
//用结果查询所需的表填充集合
所需表格。添加(“员工”);
ResultSet res=meta.getTables(null,null,“我的表名”,
新字符串[]{“表”});
List existentTables=new LinkedList();
while(res.next()){
if(requiredTables.contains(res.getString(3)){
existentTables.add(res.getString(3));
}
}
字符串查询=existentTables.stream().map(表->字符串.format(“从%s,表中选择*)).collect(收集器.连接(“联合”));
nops首先,我希望我的代码应该搜索数据库是否包含特定的表,如果包含,则从中删除数据。这就是为什么我必须再次使用if语句的原因。在这种情况下,您可以从数据库中获取可用的表,然后根据答案中的定义进行筛选并创建具有union的单个查询。这就是我要问的问题如何在一个GO中筛选特定模式的表例如,如果您在db中有所有表名的列表:列表表
,您可以生成sql查询:表.stream().filter(谓词).map(t->“select*from”+t”).collect(Collectors.joining(“UNION”)
。嘿,我没有得到您的代码。您能详细解释一下吗