判斷數(shù)據(jù)庫表是否存在以及修改表名的方法建站
導(dǎo)讀:1建站知識(shí)本文為大家詳細(xì)介紹下如何判斷數(shù)據(jù)庫表是否存在以及修改表名,感興趣的朋友可以參考下,希望對(duì)大家有所幫助網(wǎng)站seo優(yōu)化培訓(xùn)seo網(wǎng)站優(yōu)化培訓(xùn)。
一、判斷數(shù)據(jù)庫表是否存在: 首先要拿到數(shù)據(jù)庫連接conn,調(diào)用DatabaseMetaData dbmd = conn.getDataMeta();之后調(diào)用如下方法:
復(fù)制代碼 代碼如下:
/**網(wǎng)站seo優(yōu)化課程 * 根據(jù)表名,判斷數(shù)據(jù)庫表是否存在 * @param tableName * @return true:存在該表,false:不存在該表 */ public boolean hasTable(String tableName) { Init(); boolean result = false; //判斷某一個(gè)表是否存在 try{ ResultSet set = dbmd.getTables (null, null, tableName, null); //獲取查找結(jié)果 while (set.next()) { //如果查找結(jié)果不為空,則說明存在該表 result = true; //將返回結(jié)果置為true } }catch(Exception e){ e.printStackTrace(); } return result; }
二、修改表名: 首先依然要拿到數(shù)據(jù)庫連接conn和數(shù)據(jù)庫描述對(duì)象dbmd以及Statement對(duì)象st,之后調(diào)用如下方法復(fù)制代碼 代碼如下:
/** * 修改表名 * @param srcTableName 源表名 * @param newTableName 新表名 * @return true:修改表名成功,false:修改表名失敗 */ public boolean renameTable(String srcTableName,String ne網(wǎng)站優(yōu)化seo培訓(xùn)wTableName){ Init(); boolean result = false; StringBuffer sql = new StringBuffer(); try{ String dataBaseType = dbmd.getDatabaseProductName(); //獲取數(shù)據(jù)庫類型 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()); //執(zhí)行更新操作,返回結(jié)果 if(1==temp){ result = true; //將返回值設(shè)為true } }catch(Exception e){ e.printStackTrace(); } }else if(("HSQL Database Engine").equals(dataBaseType)||("MySQL").equals(dataBaseType)){ //hsql和mysql try{ sql.append("ALTER TABLE"+" "+srcTableName+" "+"RENAME TO"+" "+newTableName); int temp = 1; temp = st.executeUpdate(sql.toString()); //執(zhí)行更新操作,返回結(jié)果 if(0==temp){ result = true; //將返回值設(shè)為true } }catch(Exception e){ e.printStackTrace(); } }else{ //尚未實(shí)現(xiàn)對(duì)oracle和db2判斷 } }catch(Exception e){ e.printStackTrace(); } //System.out.println(rseo網(wǎng)站優(yōu)化esult); return result; }
相關(guān)網(wǎng)站seo優(yōu)化培訓(xùn)seo網(wǎng)站優(yōu)化培訓(xùn)。聲明: 本文由我的SEOUC技術(shù)文章主頁發(fā)布于:2023-05-22 ,文章判斷數(shù)據(jù)庫表是否存在以及修改表名的方法建站主要講述是否存在,標(biāo)簽,判斷數(shù)據(jù)庫表是否存在以及修改表網(wǎng)站建設(shè)源碼以及服務(wù)器配置搭建相關(guān)技術(shù)文章。轉(zhuǎn)載請保留鏈接: http://www.bifwcx.com/article/web_4876.html
為你推薦與判斷數(shù)據(jù)庫表是否存在以及修改表名的方法建站相關(guān)的文章
-
通王TWCMS 2.0.3網(wǎng)站模板程序下載
(126)人喜歡 2024-01-15 -
Windows官方原版在哪里下載
(175)人喜歡 2024-01-15 -
WordPress網(wǎng)站模板發(fā)帖標(biāo)題顏色設(shè)置
(131)人喜歡 2024-01-07 -
修改discuz論壇帖子標(biāo)題80字符的長度限制
(249)人喜歡 2024-01-07 -
wordpress程序調(diào)用不帶超鏈接的Tag標(biāo)簽
(234)人喜歡 2024-01-05 -
網(wǎng)站在不同時(shí)期需調(diào)整內(nèi)容更新的方向
(112)人喜歡 2023-08-12