国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > SQL Server > 正文

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

2024-08-31 00:45:09
字體:
供稿:網(wǎng)友
一、判斷數(shù)據(jù)庫表是否存在:
首先要拿到數(shù)據(jù)庫連接conn,調(diào)用DatabaseMetaData dbmd = conn.getDataMeta();之后調(diào)用如下方法:

復制代碼 代碼如下:


/**
* 根據(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)用如下方法

復制代碼 代碼如下:


/**
* 修改表名
* @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;
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 疏附县| 福鼎市| 高雄市| 内乡县| 甘孜| 慈溪市| 上犹县| 东乌珠穆沁旗| 望江县| 剑河县| 平安县| 岳西县| 荣成市| 云安县| 长春市| 金溪县| 南漳县| 辰溪县| 崇仁县| 甘德县| 来凤县| 宁都县| 台东市| 保靖县| 手游| 临沭县| 义马市| 禄丰县| 永靖县| 古田县| 郎溪县| 顺昌县| 怀安县| 东至县| 浮山县| 馆陶县| 孝昌县| 疏勒县| 四子王旗| 房产| 利川市|