Get the App
SLTechnology News&Howtos  ›  Database  › 

How to judge the existence of the database table and the method of changing the table name

Shulou Source: shulou.com Published: 2022-05-31 18:07:49 10月04日 Update

This article mainly explains "how to judge whether a database table exists and how to change the table name". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to determine whether a database table exists and how to change the table name".

1. Determine whether the database table exists: first, get the database connection conn, call DatabaseMetaData dbmd = conn.getDataMeta (), and then call the following method:

The copy code is as follows:

/ * * determine whether the database table exists according to the table name * @ param tableName * @ return true: the table exists, false: the table * / public boolean hasTable (String tableName) {Init (); boolean result = false; / / determines whether a table exists try {ResultSet set = dbmd.getTables (null, null, tableName, null) / / get the search result while (set.next ()) {/ / if the search result is not empty, the table result = true; / / sets the returned result to true} catch (Exception e) {e.printStackTrace ();} return result;}

Modify the table name: first, you still need to get the database connection conn, the database description object dbmd and the Statement object st, and then call the following method

The copy code is as follows:

/ * modify table name * @ param srcTableName source table name * @ param newTableName new table name * @ return true: table name modified 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 the 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 the 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 the update operation and return the result if (0==temp) {result = true / / set the return value to true}} catch (Exception e) {e.printStackTrace ();}} else {/ / not yet realized to judge oracle and db2} catch (Exception e) {e.printStackTrace ();} / / System.out.println (result); return result;} so far, I believe you have a deeper understanding of "how to determine whether a database table exists and the method of changing the table name". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Data databases methods results code content objects learning updating practical deeper successful interested practical practical not yet easy to operate more friends types Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia Linux OPPO Reno macOS