感谢迄今为止在我用 MySQL 学习 Java 的史诗般的战斗中的所有支持。我想要做的是检查表是否存在。当前发生的情况是,如果创建了数据库并且也创建了表。但是如果我运行相同的代码,我会得到一个表已经存在的错误。
最大的问题是我将如何检查表是否存在?这是我处理过的一些代码
if (tabCrea.createTable(dbDef, con, preStatement, tabStruct.getAgentDetail(), "Agent Details"))
System.out.println("Passed Here");
else
System.out.println("Failed Here");
调用以下内容
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;
}
}
并且表在这里定义
private String agentDetail = "CREATE TABLE AgentDetail ("
+ "AgentDetailIdNo INT(64) NOT NULL AUTO_INCREMENT,"
+ "Initials VARCHAR(2),"
+ "AgentDetailDate DATE,"
+ "AgentDetailCount INT(64),"
+ "PRIMARY KEY(AgentDetailIdNo)"
+ ")";
一个不起眼的黑客将不胜感激所提供的任何帮助。