感谢您对我在学习Java和MySQL方面的支持。我想要做的是检查表是否存在。目前的情况是,如果数据库已经创建并且表也已经创建,那么就不会出现问题。 但是如果我运行相同的代码,就会出现表已经存在的错误。
重要的问题是如何检查表是否存在? 以下是我编写的一些代码
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)"
+ ")";
一个谦虚的黑客会很感激任何提供的帮助。