如何检查 java 中是否存在视图


DatabaseMetaData dbm = connection.getMetaData(); // connection is of type Connection (in JDBC)  
ResultSet tables = dbm.getTables(null, null, "table name", null); // check if table is there
if (tables.next()) {
    //Table exists
} else {
    //Table does not exist
}

我的问题是如何检查视图是否存在。上面的代码是如何检查表是否存在。我知道它与上面的代码相似,但我遇到了麻烦。

getTables() 方法可以返回所有类型的表(其中视图被视为表的类型(。最后一个参数可用于搜索特定类型。

在您的情况下,您可以使用 dbm.getTables(null, null, "viewname", new String[]{"VIEW"}); .javadocs很好地解释了这一点,所以你应该在问这里之前阅读它们。

我的问题是表名是用带有驼峰大小写的 java 代码编写的,这在检查表是否存在时会导致问题 - 但它在删除和创建时工作正常。所以我所做的只是

ResultSet tables = dbm.getTables(null, null, "table name".toLowerCase(), null);

最新更新