java Apache Derby - 检查数据库是否已创建?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/584497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:57:08  来源:igfitidea点击:

Apache Derby - Check Database Already Created?

javadatabasederby

提问by Evan

Using Apache Derby with Java (J2ME, but I don't think that makes a difference) is there any way of checking if a database already exists and contains a table?

将 Apache Derby 与 Java(J2ME,但我认为这没有区别)一起使用有什么方法可以检查数据库是否已经存在并包含表?

回答by Adeel Ansari

I know of none, except few work around, unlike MySQL where we have that facility of IF EXIST.

我知道没有,除了很少的工作,不像 MySQL,我们有 IF EXIST 的设施。

What you do is, try to connect to the database, if couldn't its likely its not there. And after a successful connection, you can do a simple select, like SELECT count(*) FROM TABLE_NAME, to know whether the table exist or not. You would be depending on the exception. Even in an official example from Sun, I have seen the similar work around.

你要做的是,尝试连接到数据库,如果不能,它可能不存在。并且连接成功后,可以做一个简单的选择,比如SELECT count(*) FROM TABLE_NAME,来知道表是否存在。您将取决于例外情况。即使在 Sun 的官方示例中,我也看到了类似的工作。

In Oracle we have dictionary tables to know about the database objects. I doubt if we have anything like that in Derby.

在 Oracle 中,我们有字典表来了解数据库对象。我怀疑我们在德比是否有这样的事情。

[Edited]

[编辑]

Well, I found that there is a way to know if the table exist. Try, SELECT tablename FROM SYSTABLES. It is for checking the existence of a table, for checking database you may need to do similar thing, I explained above.

好吧,我发现有一种方法可以知道该表是否存在。尝试,从 SYSTABLES 中选择表名。它用于检查表是否存在,为了检查数据库,您可能需要做类似的事情,我在上面解释过。

回答by Evan

Adeel, you could also use Connection.getMetaDatato return a DatabaseMetaDataobject, then use the getTables, once you have the connection to the database of course. This has the advantage of working for any database with a JDBC driver worth it's salt.

Adeel,您还可以使用Connection.getMetaData返回一个DatabaseMetaData对象,然后使用getTables,当然,一旦您连接到数据库。这具有适用于任何具有 JDBC 驱动程序的数据库的优势。

For checking if the database exists, if you are using Derby in the embedded way, or the server is on the same machine, you could check if the folder for the database exists. Is a bit kludgy though. I would do as Adeel suggests and try to connect, catching the exception if it's not there.

检查数据库是否存在,如果您使用嵌入式方式使用Derby,或者服务器在同一台机器上,您可以检查数据库的文件夹是否存在。虽然有点笨拙。我会按照 Adeel 的建议去做并尝试连接,如果异常不存在,则捕获异常。

回答by Evan

I would suggest getting the DatabaseMetaData object, then using the getTables(null, null, null, new String[]{"TABLE"}) method from it, which returns a ResultSet. Use the next() method of the ResultSet, which returns a boolean, to test if any tables exist. If it tests true, you have tables in existence. False, and the database is empty.

我建议获取 DatabaseMetaData 对象,然后使用它的 getTables(null, null, null, new String[]{"TABLE"}) 方法,它返回一个 ResultSet。使用返回布尔值的 ResultSet 的 next() 方法来测试是否存在任何表。如果它测试为真,则您有表存在。错误,并且数据库为空。