如何在 Java 中检测 SQL 表的存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/927807/
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
How can I detect a SQL table's existence in Java?
提问by soldier.moth
How can I detect if a certain table exists in a given SQL database in Java?
如何在 Java 中检测给定 SQL 数据库中是否存在某个表?
采纳答案by Ayman Hourieh
You can use DatabaseMetaData.getTables()to get information about existing tables.
您可以使用DatabaseMetaData.getTables()获取有关现有表的信息。
This method works transparently and is independent of the database engine. I think it queries information schema tables behind the scenes.
此方法透明地工作并且独立于数据库引擎。我认为它会在幕后查询信息模式表。
Edit:
编辑:
Here is an example that prints all existing table names.
DatabaseMetaData md = connection.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
System.out.println(rs.getString(3));
}
回答by cdonner
This is not a language-specific, but a database-specific problem. You'd query the metadata in the database for the existence of that particular object.
这不是特定于语言的问题,而是特定于数据库的问题。您将查询数据库中的元数据是否存在该特定对象。
In SQL Server for instance:
以 SQL Server 为例:
SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[table]')
AND type in (N'U')
回答by RichardOD
Write a query that queries the table/view that will list the tables (this is different depending on DB vendor). Call that from Java.
编写一个查询来查询将列出表的表/视图(这取决于数据库供应商)。从 Java 调用它。
Googling information_schema.tableswill help a lot.
谷歌搜索information_schema.tables会有很大帮助。
回答by belgariontheking
Depending on the DB, you can do (MySQL)
根据数据库,您可以执行(MySQL)
SHOW TABLES
or (Oracle)
或(甲骨文)
SELECT * FROM user_objects WHERE object_type = 'TABLE'
or another thing for SQL Server. Cycle through the results for MySQL or further filter on the Oracle one.
或 SQL Server 的另一件事。循环查看 MySQL 的结果或进一步筛选 Oracle 的结果。
回答by Bob
Why not just see if it is in sysobjects (for SQL Server)?
为什么不看看它是否在 sysobjects 中(对于 SQL Server)?
SELECT [name] FROM [sysobjects] WHERE type = 'U' AND [name] = 'TableName'
回答by Swiety
There is a JDBC feature, database vendor independent - see [java.sql.DatabaseMetaData#getTables()][1]
有一个 JDBC 特性,独立于数据库供应商 - 参见 [java.sql.DatabaseMetaData#getTables()][1]
You can get the DatabaseMetaData instance by calling java.sql.Connection#getMetaData()
您可以通过调用java.sql.Connection#getMetaData()来获取 DatabaseMetaData 实例
[1]: http://java.sun.com/javase/6/docs/api/java/sql/DatabaseMetaData.html#getTables(java.lang.String, java.lang.String, java.lang.String, java.lang.String[])
[1]:http: //java.sun.com/javase/6/docs/api/java/sql/DatabaseMetaData.html#getTables(java.lang.String, java.lang.String, java.lang.String, java.lang.String[])
回答by adrian.tarau
Use java.sql.DatabaseMetaData.getTables(null, null, YOUR_TABLE, null)
. If the table exists, you will get a ResultSet
with one record.
使用java.sql.DatabaseMetaData.getTables(null, null, YOUR_TABLE, null)
. 如果该表存在,您将获得ResultSet
一条记录。
回答by Arvo
For MS Access:
对于 MS 访问:
Select Count(*) From MSysObjects
Where type=1 And name='your_table_name_here'
回答by Jeff Meatball Yang
For ALL ANSI-compliant databases: (mySQL, SQL Server 2005/2008, Oracle, PostgreSQL, SQLLite, maybe others)
对于所有符合 ANSI 的数据库:(mySQL、SQL Server 2005/2008、Oracle、PostgreSQL、SQLLite,也许其他)
select 1 from information_schema.tables where table_name = @tableName
回答by PFrancisco
This is what worked for me for jdbc:derby
:
这对我有用jdbc:derby
:
//Create Staff table if it does not exist yet
String tableName = "STAFF";
boolean exists = conn.getMetaData().getTables(null, null, tableName, null).next();
if(!exists){
s = conn.createStatement();
s.execute("create table staff(lastname varchar(30), firstname varchar(30), position varchar(20),salary double,age int)");
System.out.println("Created table " + tableName);
}
Note that tableName
has to be all caps.
请注意,tableName
必须全部大写。