⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basejdbcdao.java

📁 自己总结及编写调试好的程序,可直接应用 CutStringUtil截取字符串,能正确处理汉字定长处理 XMLProcessUtil 能处理XML读写文件的中文问题处理 服务器及客户端socket
💻 JAVA
字号:
package com.channelsoft.qframe.dao.jdbc;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.channelsoft.qframe.dao.IBaseJdbcDao;
import com.channelsoft.qframe.exception.DaoException;

/**
 * <dd>Description:基于JDBC的DAO基类,对于需要批量处理,或者涉及到大数据量的操作,使用此基类。
 */
public class BaseJdbcDao extends JdbcDaoSupport implements IBaseJdbcDao {
	protected final Log logger = LogFactory.getLog(getClass());

	/**
	 * 
	 */
	public BaseJdbcDao() {
		super();
	}

	public int update(String sql) throws DaoException {
		if (logger.isDebugEnabled()) {
			logger.debug("执行更新SQL语句:" + sql);
		}
		try {
			return getJdbcTemplate().update(sql);
		} catch (DataAccessException e) {
			logger.warn(e.getMessage());
			throw new DaoException(e.getMessage(), e);
		}
	}

	public int update(String sql, Object[] args) throws DaoException {
		if (logger.isDebugEnabled()) {
			logger.debug("执行带参数的更新SQL语句:[" + sql + "]");
			for (Object a : args) {
				logger.debug("[" + a + "]");
			}
		}
		try {
			return getJdbcTemplate().update(sql, args);
		} catch (DataAccessException e) {
			logger.warn(e.getMessage());
			throw new DaoException(e.getMessage(), e);
		}
	}

	public int queryForInt(String sql) throws DaoException {
		if (logger.isDebugEnabled()) {
			logger.debug("执行查询SQL语句,返回int值,SQL=[" + sql + "]");
		}
		try {
			return getJdbcTemplate().queryForInt(sql);
		} catch (DataAccessException e) {
			logger.warn(e.getMessage());
			throw new DaoException(e.getMessage(), e);
		}
	}

	public int queryForInt(String sql, Object[] args) throws DaoException {
		if (logger.isDebugEnabled()) {
			logger.debug("执行查询带参数的SQL语句,返回int值,SQL=[" + sql + "]");
			for (Object a : args) {
				logger.debug("[" + a + "]");
			}
		}
		try {
			return getJdbcTemplate().queryForInt(sql, args);
		} catch (DataAccessException e) {
			logger.warn(e.getMessage());
			throw new DaoException(e.getMessage(), e);
		}
	}

	public long queryForLong(String sql) throws DaoException {
		if (logger.isDebugEnabled()) {
			logger.debug("执行查询SQL语句,返回long值,SQL=[" + sql + "]");
		}
		try {
			return getJdbcTemplate().queryForLong(sql);
		} catch (DataAccessException e) {
			logger.warn(e.getMessage());
			throw new DaoException(e.getMessage(), e);
		}
	}

	public long queryForLong(String sql, Object[] args) throws DaoException {
		if (logger.isDebugEnabled()) {
			logger.debug("执行查询带参数的SQL语句,返回long值,SQL=[" + sql + "]");
			for (Object a : args) {
				logger.debug("[" + a + "]");
			}
		}
		try {
			return getJdbcTemplate().queryForLong(sql, args);
		} catch (DataAccessException e) {
			logger.warn(e.getMessage());
			throw new DaoException(e.getMessage(), e);
		}
	}

	public Object queryForObject(String sql, Class objClass)
			throws DaoException {
		if (logger.isDebugEnabled()) {
			logger.debug("执行查询SQL语句,返回Class的对象,SQL=[" + sql + "]");
		}
		try {
			return getJdbcTemplate().queryForObject(sql, objClass);
		} catch (DataAccessException e) {
			logger.warn(e.getMessage());
			throw new DaoException(e.getMessage(), e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Map> queryForList(String sql) throws DaoException {
		try {
			List<Map> list = getJdbcTemplate().queryForList(sql);
			if (logger.isDebugEnabled()) {
				logger.debug("执行查询SQL语句,SQL=[" + sql + "]");
				logger.debug("返回结果列表:" + list.size());
			}
			return list;
		} catch (DataAccessException e) {
			logger.warn(e.getMessage());
			throw new DaoException(e.getMessage(), e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Map> queryForList(String sql, Object[] args)
			throws DaoException {
		long start = System.currentTimeMillis();
		try {
			List<Map> list = getJdbcTemplate().queryForList(sql, args);
			if (logger.isDebugEnabled()) {
				logger.debug("执行带参数的查询SQL语句,SQL=[" + sql + "]");
				for (Object a : args) {
					logger.debug("[" + a + "]");
				}
				logger.debug("返回结果列表:" + list.size() + ",耗时:"
						+ (System.currentTimeMillis() - start));
			}
			return list;
		} catch (DataAccessException e) {
			logger.warn(e.getMessage());
			throw new DaoException(e.getMessage(), e);
		}
	}

	public void execute(String sql) throws DaoException {
		if (logger.isDebugEnabled()) {
			logger.debug("执行SQL语句,SQL=[" + sql + "]");
		}
		try {
			getJdbcTemplate().execute(sql);
		} catch (DataAccessException e) {
			logger.warn(e.getMessage());
			throw new DaoException(e.getMessage(), e);
		}
	}

	/**
	 * 统计操作耗时
	 * 
	 * @param message
	 * @param startTime
	 */
	protected void logTime(String message, long startTime) {
		if (logger.isInfoEnabled()) {
			long timeCost = System.currentTimeMillis() - startTime;
			logger.info("操作耗时统计--" + message + (timeCost) + "毫秒");
		}
	}

	public String queryForString(String sql) throws DaoException {
		if (logger.isDebugEnabled()) {
			logger.debug("执行查询SQL语句,返回String值,SQL=[" + sql + "]");
		}
		try {
			return (String) getJdbcTemplate().queryForObject(sql, String.class);
		} catch (DataAccessException e) {
			logger.warn(e.getMessage());
			throw new DaoException(e.getMessage(), e);
		}
	}

	public String queryForString(String sql, Object[] args) throws DaoException {
		if (logger.isDebugEnabled()) {
			logger.debug("执行查询带参数的SQL语句,返回String值,SQL=[" + sql + "]");
			for (Object a : args) {
				logger.debug("[" + a + "]");
			}
		}
		try {
			return (String) getJdbcTemplate().queryForObject(sql, args,
					String.class);
		} catch (DataAccessException e) {
			logger.warn(e.getMessage());
			throw new DaoException(e.getMessage(), e);
		}
	}

	/**
	 * @see com.channelsoft.qframe.dao.IBaseJdbcDao#query(java.lang.String,
	 *      org.springframework.jdbc.core.RowCallbackHandler)
	 */
	public void query(String sql, RowCallbackHandler handler)
			throws DaoException {
		if (logger.isDebugEnabled()) {
			logger.debug("执行查询SQL语句,并逐条处理结果数据,SQL=[" + sql + "]");
		}
		try {
			getJdbcTemplate().query(sql, handler);
		} catch (DataAccessException e) {
			logger.warn(e.getMessage());
			throw new DaoException(e.getMessage(), e);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -