日期:2014-05-16  浏览次数:20527 次

自己封装的JDBC框架

?最近由于项目需求,开发都是偏报表种类多,而且考虑到项目移植性,感觉还是用jdbc访问比较稳妥,等到项目移植的时候只需要修改相应的sql,以下是最近刚封装的jdbc,主要是通过重构封装增删改查,向外提供包括查询分页,模糊查询的方式,只需要把需要的参数封装成hashmap即可,请大家给点意见,

?

package com.micon.base.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.ObjectUtils;
import com.micon.base.bean.Page;
import com.micon.base.bean.ParamMap;
import com.micon.base.bean.QueryBean;



public class JdbcUtils {

	private static JdbcUtils jdbcUtils = null;

	public static JdbcUtils getJdbcUtils() {
		if (jdbcUtils == null) {
			jdbcUtils = new JdbcUtils();
		}
		return jdbcUtils;
	}

	public Connection getConnection() throws ClassNotFoundException,
			SQLException {
		Connection con  = DataBase.getConnection();
		return con;
	}

	public int insert(String tableName, Map paramMap) throws Exception {
		String sql = genInsertSql(tableName, paramMap);
		//delete tab where id = ?, name = ?
		List param = new ArrayList();
		if (null != paramMap && !paramMap.isEmpty()) {
			Set set = paramMap.entrySet();
			for (Iterator it = set.iterator(); it.hasNext();) {
				Map.Entry entry = (Map.Entry) it.next();
				String key = ObjectUtils.toString(entry.getKey()).trim()
						.toLowerCase();
				if (null != key && !"".equals(key)) {
					param.add(entry.getValue());
				}
			}
		}
		return updateData(sql, param);
	}

	public int update(String tableName, Map paramMap) {
		return update(tableName,"id",paramMap);
	}
	
	public int update(String tableName, String primaryKey, Map paramMap) {
		
		String sql = genUpdateSql(tableName, primaryKey, paramMap);
		List param = new ArrayList();
		
		if (null != paramMap && !paramMap.isEmpty()) {
			Set set = paramMap.entrySet();
			String primary = "";
			
			for (Iterator it = set.iterator(); it.hasNext();) {
				Map.Entry entry = (Map.Entry) it.next();
				String key = ObjectUtils.toString(entry.getKey()).trim()
						.toLowerCase();
				if (null != key && !"".equals(key)) {
					if (null != entry.getValue()) {
						if (key.equals(primaryKey)) {
							primary = ObjectUtils.toString(entry.getValue())
									.trim();
						} else {
							param.add(entry.getValue());
						}
					}
				}
			}
			
			if (null != primary && !"".equals(primary)) {
				param.add(primary);
				try {
					return updateData(sql, param);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return 0;
	}
	

	public int delete(String tableName, String filter) throws Exception {
		
		StringBuffer sql = new StringBuffer(" delete from " + tableName + " ");
		if (filter != null && !filter.equals("")) {
			sql.append(" where " + filter);
		}
		
		return updateData(sql.toString(), null);
	}
	
	public int updateData(String sql) throws Exception {
		return updateData(sql,null);
	}
	
	public int updateData(String sql, List param) throws Exception {
		Connection conn = null;
		PreparedStatement ps = null;

		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);
			if (param != null && param.size() > 0){
				for (int i = 0; i < param.size(); i++) {
					ps.setObject(i + 1, param.get(i));
				}
			}
			
			System.out.println(com.micon.base.util.DateUtils.DATEFORMAT_yMd_hms.format(new Date())
					+" the update  sql = [ " + sql+" ]");
			
			return ps.executeUpdate();
		} catch (SQLException ex) {
			System.out.println("Cannot execute sql: " + sql + "\nvalues:"
					+ param + " error: " + ex.getMessage());
			throw new Exception("Cannot execute sql: " + sql + "\nvalues:"
					+ param + " error: " +