Java mysql 如果表存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19498520/
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
Java mysql if Table Exists
提问by K Spriggs
Thanks for all the support so far in my epic battle for my learning Java with MySQL. What I want to do is check if the table exists. What currently happens is that if the database is created and the tables are created as well. But if I run the same code, I get an error that the table already exists.
感谢迄今为止在我用 MySQL 学习 Java 的史诗般的战斗中的所有支持。我想要做的是检查表是否存在。当前发生的情况是,如果创建了数据库并创建了表。但是,如果我运行相同的代码,则会收到表已存在的错误。
The big question is how would I check of the table exists? Here is some code that I have worked on
最大的问题是我将如何检查表是否存在?这是我研究过的一些代码
if (tabCrea.createTable(dbDef, con, preStatement, tabStruct.getAgentDetail(), "Agent Details"))
System.out.println("Passed Here");
else
System.out.println("Failed Here");
Which calls the following
其中调用以下
protected boolean createTable(DataBaseDefaults dbDef, Connection con, Statement statement, String myTableName, String tableName) {
try {
Class.forName(dbDef.getJdbcDriver());
con = DriverManager.getConnection(dbDef.getDbAddress() + dbDef.getPortAddress() + dbDef.getDbName(),
dbDef.getDbUserName(), dbDef.getDbPassword());
statement = con.createStatement();
statement.executeUpdate(myTableName);
System.out.println("Table Sucessfully Created : " + tableName);
return true;
}
catch (SQLException e ) {
//Problem is caught here;
System.out.println("An error has occured on Table Creation with Table " + tableName);
return false;
}
catch (ClassNotFoundException e) {
System.out.println("There was no Mysql drivers found");
return false;
}
}
and the table is defined here
表在这里定义
private String agentDetail = "CREATE TABLE AgentDetail ("
+ "AgentDetailIdNo INT(64) NOT NULL AUTO_INCREMENT,"
+ "Initials VARCHAR(2),"
+ "AgentDetailDate DATE,"
+ "AgentDetailCount INT(64),"
+ "PRIMARY KEY(AgentDetailIdNo)"
+ ")";
A humble hack would appreciate any help that is on offer.
一个谦虚的黑客会感谢提供的任何帮助。
采纳答案by SheetJS
There are two approaches:
有两种方法:
1) if you want to preserve the table if it does exist, use IF NOT EXISTS
:
1)如果您想保留该表(如果它确实存在),请使用IF NOT EXISTS
:
private String agentDetail = "CREATE TABLE IF NOT EXISTS AgentDetail (" ...
2) if you want to delete the table and start anew, delete the table first with the DROP TABLE
statement. To do that, run the statement
2)如果要删除表重新开始,先用DROP TABLE
语句删除表。为此,请运行语句
DROP TABLE IF EXISTS AgentDetail
before the create table query (note that this will delete all of the existing data in the table)
在创建表查询之前(请注意,这将删除表中的所有现有数据)
回答by Alfredo Osorio
You can use CREATE TABLE IF NOT EXISTS TBL_NAME
您可以使用CREATE TABLE IF NOT EXISTS TBL_NAME
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
[partition_options]
回答by Thusitha Thilina Dayaratne
you can use
您可以使用
SHOW TABLES LIKE '$this_table' or
SHOW TABLES FROM $dbname
and check whether table is already exist
并检查表是否已经存在
回答by Jess Balint
The DatabaseMetaDataAPI, part of the JDBC spec, is the way to do this in Java. You'll specifically want to check out the `getTables' method. This is a good class to be familiar with.
该DatabaseMetaData的API,在JDBC规范的一部分,是在Java中做到这一点的方式。您将特别想查看`getTables' 方法。这是一个需要熟悉的好类。