判斷數(shù)據(jù)庫表是否存在以及修改表名的方法
更新時間:2013年09月09日 11:04:39 作者:
本文為大家詳細(xì)介紹下如何判斷數(shù)據(jù)庫表是否存在以及修改表名,感興趣的朋友可以參考下,希望對大家有所幫助
一、判斷數(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{ //尚未實(shí)現(xiàn)對oracle和db2判斷
}
}catch(Exception e){
e.printStackTrace();
}
//System.out.println(result);
return result;
}
首先要拿到數(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{ //尚未實(shí)現(xiàn)對oracle和db2判斷
}
}catch(Exception e){
e.printStackTrace();
}
//System.out.println(result);
return result;
}
相關(guān)文章
SQL Server 數(shù)據(jù)庫分離與附加 就這么簡單!
這篇文章主要介紹了SQL Server 數(shù)據(jù)庫分離與附加,很簡單的圖文教程,感興趣的小伙伴們可以參考一下2016-08-08
淺談SQLServer的ISNULL函數(shù)與Mysql的IFNULL函數(shù)用法詳解
本篇文章是對SQLServer的ISNULL函數(shù)與Mysql的IFNULL函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
sqlserver 文件數(shù)據(jù)庫和關(guān)系數(shù)據(jù)庫的比較
本文概要地從數(shù)據(jù)格式、數(shù)據(jù)庫結(jié)構(gòu)和WEB發(fā)布數(shù)據(jù)三個方面比較了文件數(shù)據(jù)庫和關(guān)系數(shù)據(jù)庫的異同,同時差別了文件數(shù)據(jù)庫和過去存儲數(shù)據(jù)的文件系統(tǒng)的不同2011-10-10
SQL窗口函數(shù)之聚合窗口函數(shù)的使用(count,max,min,sum)
許多常見的聚合函數(shù)也可以作為窗口函數(shù)使用,包括AVG()、SUM()、COUNT()、MAX()以及MIN()等函數(shù),本文就詳細(xì)的介紹了SQL窗口函數(shù)之聚合窗口函數(shù)的使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04
SqlServer備份數(shù)據(jù)庫的4種方式介紹
這篇文章主要介紹了SqlServer備份數(shù)據(jù)庫的4種方式介紹,本文講解了用sqlserver的維護(hù)計(jì)劃、通過腳本+作業(yè)的方式備份數(shù)據(jù)庫(非xp_cmdshell和xp_cmdshell)、用powershell調(diào)用sqlcmd來執(zhí)行備份命令幾種方式,需要的朋友可以參考下2015-02-02

