校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > 判斷數(shù)據(jù)庫表是否存在以及修改表名的方法

判斷數(shù)據(jù)庫表是否存在以及修改表名的方法

熱門標簽:外呼系統(tǒng)的話術(shù) 貴州外呼回撥系統(tǒng)是什么 云南全自動外呼系統(tǒng)公司 智能語音車載電話機器人 衛(wèi)星地圖標注距離 陽光創(chuàng)信ai外呼獲客系統(tǒng)助力 智能外呼系統(tǒng)需要多少錢 安陽天音防封電銷卡 競圣地圖標注服務(wù)
一、判斷數(shù)據(jù)庫表是否存在:
首先要拿到數(shù)據(jù)庫連接conn,調(diào)用DatabaseMetaData dbmd = conn.getDataMeta();之后調(diào)用如下方法:
復(fù)制代碼 代碼如下:

/**
* 根據(jù)表名,判斷數(shù)據(jù)庫表是否存在
* @param tableName
* @return true:存在該表,false:不存在該表
*/
public boolean hasTable(String tableName) {
Init();
boolean result = false; //判斷某一個表是否存在
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ù)庫描述對象dbmd以及Statement對象st,之后調(diào)用如下方法
復(fù)制代碼 代碼如下:

/**
* 修改表名
* @param srcTableName 源表名
* @param newTableName 新表名
* @return true:修改表名成功,false:修改表名失敗
*/
public boolean renameTable(String srcTableName,String newTableName){
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{ //尚未實現(xiàn)對oracle和db2判斷
}
}catch(Exception e){
e.printStackTrace();
}
//System.out.println(result);
return result;
}

標簽:河源 欽州 營口 鄂爾多斯 寧夏 周口 預(yù)約服務(wù) 湘潭

巨人網(wǎng)絡(luò)通訊聲明:本文標題《判斷數(shù)據(jù)庫表是否存在以及修改表名的方法》,本文關(guān)鍵詞  判斷,數(shù)據(jù)庫,表,是否,存在,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《判斷數(shù)據(jù)庫表是否存在以及修改表名的方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于判斷數(shù)據(jù)庫表是否存在以及修改表名的方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 祁连县| 曲松县| 绥滨县| 彭泽县| 中牟县| 崇明县| 牡丹江市| 鹤山市| 新蔡县| 阿荣旗| 普兰店市| 松溪县| 南涧| 古蔺县| 北票市| 沐川县| 沈阳市| 浦北县| 天台县| 衡阳县| 屏东县| 罗山县| 嵩明县| 仙游县| 忻城县| 区。| 旬阳县| 武宁县| 邵阳市| 兴国县| 永和县| 祁阳县| 陆丰市| 赫章县| 井研县| 西林县| 长宁区| 安西县| 邹城市| 霍邱县| 华池县|