使用JDBC查询是否存在某表或视图,按月动态生成表

本文介绍了一种根据特定日期动态创建数据库表的方法,并在表存在时直接保存数据,不存在时先创建表再保存数据的技术方案。

查询数据库是否有某表的存在,主要用的就是Connection对象对元数据的操作,代码很简单,贴出来大家参考

 

Java代码    收藏代码
  1. /** 
  2.  * 查询数据库是否有某表 
  3.  * @param cnn 
  4.  * @param tableName 
  5.  * @return 
  6.  * @throws Exception 
  7.  */  
  8. @SuppressWarnings("unchecked")  
  9. public boolean getAllTableName(String tableName) throws Exception {  
  10.     Connection conn = jdbcTemplate.getDataSource().getConnection();  
  11.     ResultSet tabs = null;  
  12.     try {  
  13.         DatabaseMetaData dbMetaData = conn.getMetaData();  
  14.         String[]   types   =   { "TABLE" };  
  15.         tabs = dbMetaData.getTables(nullnull, tableName, types);  
  16.         if (tabs.next()) {  
  17.             return true;  
  18.         }  
  19.     } catch (Exception e) {  
  20.         e.printStackTrace();  
  21.     }finally{  
  22.         tabs.close();  
  23.         conn.close();  
  24.     }  
  25.     return false;  
  26. }  

 

然后判断是有某表,如果没有,调用创建

Java代码    收藏代码
  1. /** 
  2.  * 根据表名称创建一张表 
  3.  * @param tableName 
  4.  */  
  5. public int createTable(String tableName){  
  6.     StringBuffer sb = new StringBuffer("");  
  7.     sb.append("CREATE TABLE `" + tableName + "` (");  
  8.     sb.append("`id`  int(11) NOT NULL AUTO_INCREMENT ,");  
  9.     sb.append("`alertId`  int(11) NULL DEFAULT NULL ,");  
  10.     sb.append("`alertTime`  int(11) NULL DEFAULT NULL ,");  
  11.     sb.append("`alertLevel`  int(11) NULL DEFAULT NULL ,");  
  12.     sb.append("`deviceMark`  int(11) NULL DEFAULT NULL ,");  
  13.     sb.append("`carNo`  int(11) NULL DEFAULT NULL ,");  
  14.     sb.append("`updateTime`  varchar(255) DEFAULT NULL ,");  
  15.     sb.append("PRIMARY KEY (`id`)");  
  16.     sb.append(") CHARACTER SET=utf8 COLLATE=utf8_general_ci;");  
  17.     try {  
  18.         jdbcTemplate.update(sb.toString());  
  19.         return 1;  
  20.     } catch (Exception e) {  
  21.         e.printStackTrace();  
  22.     }  
  23.     return 0;  
  24. }  

 

这两个方法可以公用,至于是按月还是按天还是按周,取决于你对表名称的生成

Java代码    收藏代码
  1. /** 
  2.  * 保存 
  3.  */  
  4. @Override  
  5. public int saveAlertMessLog(AlertMessLog alertMessLog) {  
  6.     SimpleDateFormat format = new SimpleDateFormat("yyyy_MM");  
  7.     String tableName = "nm_alertmesslog_" + format.format(new Date());  
  8.     try {  
  9.         boolean isHave = getAllTableName(tableName);  
  10.         if(isHave){  
  11.             return saveObject(alertMessLog,tableName);  
  12.         }else{  
  13.             if(createTable(tableName) == 1){  
  14.                 return saveObject(alertMessLog,tableName);  
  15.             }  
  16.         }  
  17.     } catch (Exception e) {  
  18.         e.printStackTrace();  
  19.     }         
  20.     return 0;  
  21. }  

 

我的格式化方法决定了是按照月来进行生成,如果有直接保存,如果没有,先生成再保存!

<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值