Determine if a database table exists and how to modify the table name _mssql

Source: Internet
Author: User
Tags microsoft sql server stringbuffer
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.