1. 程式人生 > >使用 JdbcTemplate 動態建立表並新增資料 動態連表查詢

使用 JdbcTemplate 動態建立表並新增資料 動態連表查詢

我寫了這樣一個公共方法,僅供參考!

這裡需要傳遞兩個時間間隔引數,根據時間間隔判斷相差的月數,然後從起始時間開始遞增月份,然後動態拼裝表的名稱,如果存在該表則標記需要查詢

所有的SQL通過 union all 來連線,最後增加分頁的引數,分頁只適合MySQL資料庫

當然這個示例也只是適合於按月份來儲存的情況

程式碼並不是很複雜,希望大家還是通過程式碼來理解一下:

package com;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @說明 進行測試
 * @author cuisuqiang
 * @version 1.0
 * @since
 */
public class DbTest {
	private static ApplicationContext context = null;

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		context = new ClassPathXmlApplicationContext("applicationContext.xml");
		String sql = "select * from :TABLE_NAME";
		String key = "nm_cpu_log";
		String beginTime = "2012-1-1 12:12:12";
		String endTime = "2014-12-1 12:12:12";
		String formatkey = "yyyy_MM";
		List list = getListFromEachTable(sql,key,beginTime,endTime,formatkey,1,100);
		if(null != list){
			System.out.println(list.size());
		}else{
			System.out.println("沒有資料!");
		}
	}

	/**
	 * 動態查詢表資料
	 * @param sqlTemplate 要執行的單條SQL 其中表名稱用 :TABLE_NAME 標識
	 * @param tableKey 表字首,不包含和時間中間的 下劃線
	 * @param dateBegin 開始時間 yyyy-MM-dd HH:mm:ss
	 * @param dateEnd 結束時間 yyyy-MM-dd HH:mm:ss
	 * @param dateFormat 時間格式化方式
	 * @param pageIndex 分頁索引 從 1 開始
	 * @param pageCount 每頁查詢數量 無限制
	 * @return 模版查詢直接返回
	 */
	@SuppressWarnings("unchecked")
	public synchronized static List getListFromEachTable(String sqlTemplate,
			String tableKey, String dateBegin, String dateEnd,
			String dateFormat, int pageIndex, int pageCount) {
		List list = null;
		try {
			// 一共有多少需要執行的SQL
			List<String> sqlList = new ArrayList<String>();
			JdbcTemplate jt = (JdbcTemplate) context.getBean("jdbcTemplate");
			// 轉為時間
			Date dbegin = StringToDateTime(dateBegin);
			Date dend = StringToDateTime(dateEnd);
			// 相差的月數
			int monthCount = calculateMonthIn(dend, dbegin);
			for (int i = 0; i <= monthCount; i++) {
				// 下 N 個月的時間
				Date d = getNextMonth(dbegin, i);
				SimpleDateFormat format = new SimpleDateFormat(dateFormat);
				// 動態表名
				String tableName = tableKey + "_" + format.format(d);
				// 如果有某表
				if (getAllTableName(jt, tableName)) {
					System.out.println("存在表:" + tableName);
					// 替換名稱獲得SQL
					String sql = sqlTemplate.replace(":TABLE_NAME", tableName);
					sqlList.add(sql);
				}
			}
			// 實際要執行的SQL
			StringBuffer sqlGo = new StringBuffer("");
			if (sqlList.size() > 0) {
				for (int i = 0; i < sqlList.size(); i++) {
					if (i == sqlList.size() - 1) {
						sqlGo.append(sqlList.get(i));
					} else {
						sqlGo.append(sqlList.get(i) + " union all ");
					}
				}
			}
			if (sqlList.size() > 0) {
				// 增加分頁
				int cindex = (pageIndex - 1) * pageCount;
				sqlGo.append(" limit " + cindex + "," + pageCount);
			}
			if (!"".equals(sqlGo.toString())) {
				System.out.println("執行的SQL:" + sqlGo.toString());
				// 執行查詢
				list = jt.queryForList(sqlGo.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
			list = null;
		}
		return list;
	}

	/**
	 * 將傳入的字串按yyyy-MM-dd HH:mm:ss格式轉換成對應的日期物件
	 * @param str 需要轉換的字串
	 */
	public synchronized static Date StringToDateTime(String str) {
		String _pattern = "yyyy-MM-dd HH:mm:ss";
		return StringToDate(str, _pattern);
	}

	/**
	 * 將插入的字串按格式轉換成對應的日期物件
	 * @param str 字串
	 * @param pattern 格式
	 */
	public synchronized static Date StringToDate(String str, String pattern) {
		Date dateTime = null;
		try {
			if (str != null && !str.equals("")) {
				SimpleDateFormat formater = new SimpleDateFormat(pattern);
				dateTime = formater.parse(str);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return dateTime;
	}

	/**
	 * 兩個時間相差的月數
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int calculateMonthIn(Date date1, Date date2) {
		Calendar cal1 = new GregorianCalendar();
		cal1.setTime(date1);
		Calendar cal2 = new GregorianCalendar();
		cal2.setTime(date2);
		int c = (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12
				+ cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
		return c;
	}

	/**
	 * 獲得下幾個月的時間物件
	 * @param date
	 * @param count
	 * @return
	 */
	public static Date getNextMonth(Date date, int count) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, count);
		return c.getTime();
	}

	/**
	 * 查詢資料庫是否有某表
	 * @param jt
	 * @param tableName
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static boolean getAllTableName(JdbcTemplate jt, String tableName)
			throws Exception {
		Connection conn = jt.getDataSource().getConnection();
		ResultSet tabs = null;
		try {
			DatabaseMetaData dbMetaData = conn.getMetaData();
			String[] types = { "TABLE" };
			tabs = dbMetaData.getTables(null, null, tableName, types);
			if (tabs.next()) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			tabs.close();
			conn.close();
		}
		return false;
	}
}