first, to determine whether the database table exists:
First you have to get the database connection conn, call DatabaseMetaData Dbmd = Conn.getdatameta (), and then call the following methods:
Copy Code code as follows:
/**
* Determine whether a database table exists based on the table name
* @param tablename
* @return true: The table exists, false: The table is not present
*/
public boolean hastable (String tablename) {
Init ();
Boolean result = false; Determine if a table exists
try{
ResultSet set = Dbmd.gettables (null, NULL, tablename, NULL); Get Find Results
while (Set.next ()) {//If the lookup result is not empty, the table is present
result = true; To set the return result to True
}
}catch (Exception e) {
E.printstacktrace ();
}
return result;
}
second, modify the table name:
First of all, still have to get the database connection conn and database Description object Dbmd and statement Object St, then call the following method
Copy Code code as follows:
/**
* Modify Table Name
* @param srctablename Source table name
* @param newtablename New table name
* @return true: Modify table name successfully, false: failed to modify table name
*/
public boolean renametable (String srctablename,string newtablename) {
Init ();
Boolean result = false;
StringBuffer sql = new StringBuffer ();
try{
String DatabaseType = Dbmd.getdatabaseproductname (); Get database type
if (("Microsoft SQL Server"). Equals (DatabaseType)) {//sqlserver
try{
Sql.append ("EXEC sp_rename" + "" +srctablename). Append (","). Append (Newtablename);
int temp = 0;
temp = St.executeupdate (sql.tostring ()); Perform an update operation and return the result
if (1==temp) {
result = true; Set the return value to True
}
}catch (Exception e) {
E.printstacktrace ();
}
}else if (("Hsql Database Engine"). Equals (DatabaseType) | | ("MySQL"). Equals (DatabaseType)) {//hsql and MySQL
try{
Sql.append ("ALTER TABLE" + "" +srctablename+ "" + "RENAME to" + "" +newtablename);
int temp = 1;
temp = St.executeupdate (sql.tostring ()); Perform an update operation and return the result
if (0==temp) {
result = true; Set the return value to True
}
}catch (Exception e) {
E.printstacktrace ();
}
}else{//has not yet implemented Oracle and DB2 judgments
}
}catch (Exception e) {
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN (result);
return result;
}