判斷數(shù)據(jù)庫(kù)表是否存在以及修改表名的方法
發(fā)布日期:2022-01-01 03:42 | 文章來(lái)源:源碼之家
一、判斷數(shù)據(jù)庫(kù)表是否存在:
首先要拿到數(shù)據(jù)庫(kù)連接conn,調(diào)用DatabaseMetaData dbmd = conn.getDataMeta();之后調(diào)用如下方法:
/**
* 根據(jù)表名,判斷數(shù)據(jù)庫(kù)表是否存在
* @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ù)庫(kù)連接conn和數(shù)據(jù)庫(kù)描述對(duì)象dbmd以及Statement對(duì)象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ù)庫(kù)類型
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(result);
return result;
}
首先要拿到數(shù)據(jù)庫(kù)連接conn,調(diào)用DatabaseMetaData dbmd = conn.getDataMeta();之后調(diào)用如下方法:
復(fù)制代碼 代碼如下:
/**
* 根據(jù)表名,判斷數(shù)據(jù)庫(kù)表是否存在
* @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ù)庫(kù)連接conn和數(shù)據(jù)庫(kù)描述對(duì)象dbmd以及Statement對(duì)象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ù)庫(kù)類型
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(result);
return result;
}
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信