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

📄 basedao.java

📁 一个基于Java的新闻发布系统
💻 JAVA
字号:
/*
 * Title: The  System of kelamayi Downhole Company [PMIP]
 * 
 * Copyright: Copyright 2005 
 * 
 * Company: hope Co., Ltd
 * 
 * All right reserved.
 * 
 * Created on 2005-12-10
 * 
 * JDK version used	:1.4.1
 * 
 * Modification history:
 * 
 * Struts.com.hope.common.dao.BaseDao Author :LiuHongDe(刘宏德)
 * 
 * Struts.com.hope.common.dao.BaseDao Version 1.0
 */
package com.hope.common.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.hope.common.exception.BaseException;
import com.hope.common.util.db.DBHelper;
import com.hope.common.util.parseVO.DynamicTVO;

/**
 * 
 * @version Version 1.0 
 * @author 刘宏德
 */
public class BaseDao {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(BaseDao.class);

	/**
	 * 
	 * 
	 * @return
	 * 处理逻辑:<br>
	 * 无
	 * <br>
	 * int
	 * Method Name:com.hope.common.dao.runSqlUpDate<br>
	 * BaseDao.java Version 1.0<br>
	 * BaseDao<br>
	 */
	protected static int runSqlUpDate(String sql) throws BaseException {
		if (sql == null || sql.equalsIgnoreCase("")) {
			logger.info("sql语句为空!!!");
			throw new SqlNullPointerException("sql is NULL or sql is lang 0 !!!");
		}
		logger.info("sql语句为=" + sql);
		Connection conn = null;
		PreparedStatement pstm = null;
		try {
			conn = DBHelper.getConnection();
			pstm = conn.prepareStatement(sql);
			return pstm.executeUpdate();
		} catch (SQLException e) {
			/**  Aoto catch cake<br>
			 * 说明:<br>
			 */
			logger.error(e);
			e.printStackTrace();
		} finally {
			//关闭连接
			DBHelper.release(conn, pstm);
		}
		return 0;
	}

	/**
	 * 处理查询数据业务(select)
	 * 
	 * @param sql 执行的sql语句
	 * @return
	 * @throws BaseException
	 * 处理逻辑:<br>
	 * 无
	 * <br>
	 * ResultSet
	 * Method Name:com.hope.common.dao.runSqlQuery<br>
	 * BaseDao.java Version 1.0<br>
	 * BaseDao<br>
	 */
	protected static List runSqlQuery(String sql, Object vo)
		throws BaseException {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			conn = DBHelper.getConnection();
			pstm = conn.prepareStatement(sql);
			rs = pstm.executeQuery();
			return DynamicTVO.rsTovo(rs, vo);
		} catch (SQLException e) {
			/**  Aoto catch cake<br>
			 * 说明:<br>
			 */
			e.printStackTrace();
		} finally {
			//关闭连接
			DBHelper.release(conn, pstm, rs);
		}
		return null;
	}

	/**
	 * 
	 * 
	 * @param sql
	 * @return
	 * @throws BaseException
	 * 处理逻辑:<br>
	 * 无
	 * <br>
	 * ResultSet
	 * Method Name:com.hope.common.dao.runSqlQuery<br>
	 * BaseDao.java Version 1.0<br>
	 * BaseDao<br>
	 */
	protected static ResultSet runSqlQuery(String sql) throws BaseException {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			conn = DBHelper.getConnection();
			pstm = conn.prepareStatement(sql);
			rs = pstm.executeQuery();
			return rs;
		} catch (SQLException e) {
			/**  Aoto catch cake<br>
			 * 说明:<br>
			 */
			e.printStackTrace();
		} finally {
			//关闭连接
			DBHelper.release(conn, pstm, rs);
		}
		return null;
	}

	/**
	 * 
	 * 
	 * @param sql
	 * @return
	 * @throws BaseException
	 * 处理逻辑:<br>
	 * 无
	 * <br>
	 * String
	 * Method Name:com.hope.common.dao.runSqlGetPK<br>
	 * BaseDao.java Version 1.0<br>
	 * BaseDao<br>
	 */
	protected static String runSqlGetPK(String tableName, String bMCode)
		throws BaseException {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		if (tableName == null || tableName.length() < 0) {
			return null;
		}
		if (bMCode == null)
			bMCode = "";
		try {
			conn = DBHelper.getConnection();
			CallableStatement callSt = conn.prepareCall(
			  "{call GetID(?,?,?)}");
			callSt.setString(1, tableName);
			callSt.setString(2, bMCode);
			callSt.setString(3, "");
			callSt.registerOutParameter(3, 12);
			callSt.execute();
			String PID = callSt.getString(3);
			if (PID!=null) {
				return PID;
			} else {
				return null;
			}
		} catch (SQLException e) {
			/**  Aoto catch cake<br>
			 * 说明:<br>
			 */
			e.printStackTrace();
		} finally {
			//关闭连接
			DBHelper.release(conn, pstm, rs);
		}
		return null;
	}
	/**
	 * 处理更新数据业务(add,del,deit)
	 * 
	 * @param sql   执行的sql语句
	 * @return  return 0 执行 成功 1 执行失败
	 * 处理逻辑:<br>
	 * 无
	 * <br>
	 * int
	 * Method Name:com.hope.common.dao.update<br>
	 * BaseDao.java Version 1.0<br>
	 * BaseDao<br>
	 */
	public int update(String sql) throws BaseException {
		return runSqlUpDate(sql);
	}
	/**
	 * 处理查询数据业务(select)
	 * 
	 * @param sql 执行的sql语句
	 * @param vo	封装对象
	 * @return list结构记录集
	 * @throws BaseException
	 * 处理逻辑:<br>
	 * 无
	 * <br>
	 * List
	 * Method Name:com.hope.common.dao.query<br>
	 * BaseDao.java Version 1.0<br>
	 * BaseDao<br>
	 */
	public List query(String sql, Object vo) throws BaseException {
		return runSqlQuery(sql, vo);
	}
	public static void main(String[] args) {
	}
}

⌨️ 快捷键说明

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