判斷數(shù)據(jù)庫(kù)表是否存在以及修改表名的方法
更新時(shí)間:2013年09月09日 11:04:39 作者:
本文為大家詳細(xì)介紹下如何判斷數(shù)據(jù)庫(kù)表是否存在以及修改表名,感興趣的朋友可以參考下,希望對(duì)大家有所幫助
一、判斷數(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é)果不為空,則說(shuō)明存在該表
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ù)類(lèi)型
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é)果不為空,則說(shuō)明存在該表
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ù)類(lèi)型
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;
}
相關(guān)文章
SQL Server 數(shù)據(jù)庫(kù)分離與附加 就這么簡(jiǎn)單!
這篇文章主要介紹了SQL Server 數(shù)據(jù)庫(kù)分離與附加,很簡(jiǎn)單的圖文教程,感興趣的小伙伴們可以參考一下2016-08-08
淺談SQLServer的ISNULL函數(shù)與Mysql的IFNULL函數(shù)用法詳解
本篇文章是對(duì)SQLServer的ISNULL函數(shù)與Mysql的IFNULL函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
sqlserver 文件數(shù)據(jù)庫(kù)和關(guān)系數(shù)據(jù)庫(kù)的比較
本文概要地從數(shù)據(jù)格式、數(shù)據(jù)庫(kù)結(jié)構(gòu)和WEB發(fā)布數(shù)據(jù)三個(gè)方面比較了文件數(shù)據(jù)庫(kù)和關(guān)系數(shù)據(jù)庫(kù)的異同,同時(shí)差別了文件數(shù)據(jù)庫(kù)和過(guò)去存儲(chǔ)數(shù)據(jù)的文件系統(tǒng)的不同2011-10-10
SQL窗口函數(shù)之聚合窗口函數(shù)的使用(count,max,min,sum)
許多常見(jiàn)的聚合函數(shù)也可以作為窗口函數(shù)使用,包括AVG()、SUM()、COUNT()、MAX()以及MIN()等函數(shù),本文就詳細(xì)的介紹了SQL窗口函數(shù)之聚合窗口函數(shù)的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
SQL語(yǔ)句優(yōu)化提高數(shù)據(jù)庫(kù)性能
為了獲得穩(wěn)定的執(zhí)行性能,SQL語(yǔ)句越簡(jiǎn)單越好。對(duì)復(fù)雜的SQL語(yǔ)句,要設(shè)法對(duì)之進(jìn)行簡(jiǎn)化,本文給大家介紹優(yōu)化SQL語(yǔ)句提高數(shù)據(jù)庫(kù)性能,對(duì)sql語(yǔ)句優(yōu)化性能優(yōu)化相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01
mybatis動(dòng)態(tài)sql常用場(chǎng)景總結(jié)
在平時(shí)開(kāi)發(fā)中針對(duì)動(dòng)態(tài)sql經(jīng)常會(huì)使用到,為了加深對(duì)動(dòng)態(tài)sql的熟練度,小編給大家分享一篇教程關(guān)于mybatis動(dòng)態(tài)sql常用場(chǎng)景總結(jié),需要的朋友可以參考下2021-08-08
SqlServer備份數(shù)據(jù)庫(kù)的4種方式介紹
這篇文章主要介紹了SqlServer備份數(shù)據(jù)庫(kù)的4種方式介紹,本文講解了用sqlserver的維護(hù)計(jì)劃、通過(guò)腳本+作業(yè)的方式備份數(shù)據(jù)庫(kù)(非xp_cmdshell和xp_cmdshell)、用powershell調(diào)用sqlcmd來(lái)執(zhí)行備份命令幾種方式,需要的朋友可以參考下2015-02-02

