我在 Oracle 数据库中有一些用户,假设 UserOne、UserTwo 和 UserTree 具有相同且非空的表,名称为“tableExample”。
就我而言,我需要通过 getColumns() 方法初始化 ResultSet:
DatabaseMetaData md = conn.getMetaData();
ResultSet r = md.getColumns(null, "UserTwo", "tableExample", null);
while(r.next())
{
//Do something;
}
在上面的示例中,r.next() 为 true,但当我使用 UserOne 或 UserTree 时,r.next() 为 false。
如何解决这个问题,以便无论我选择哪个用户,都能获得该表的结果集?
*如果我使用:
ResultSet r = md.getColumns(null, null, "tableExample", null);
我会将所有用户的结果接收到数据库中,但我需要使用此方法中的第二个参数动态具体化用户。
最佳答案
来自apidocs :
schemaPattern
- a schema name pattern; must match the schema name as it is stored in the database; [...]
所以这是一种模式,但不幸的是,没有提示该方法期望什么类型的模式,但您可能只是尝试一下:
ResultSet r = md.getColumns(null, "User*", "tableExample", null);
ResultSet r = md.getColumns(null, "User.+", "tableExample", null);
<小时/>
或者,您可以直接使用 oracle 的系统表(apidoc 对于 10g):
String sql = "SELECT * FROM SYS.ALL_TAB_COLUMNS WHERE OWNER LIKE 'User%'";
ResultSet r = conn.createStatement().executeQuery(sql);
关于Java DatabaseMetaData.getColumns() 方法并不适用于所有用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47442929/