/*
 * MosP - Mind Open Source Project         http://www.mosp.jp/
 * Copyright (C) 1987-2009 MIND Co., Ltd.  http://www.e-mind.co.jp/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package jp.mosp.payroll.dao;

import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import jp.mosp.common.common.BaseDao;
import jp.mosp.common.common.MospException;

import jp.mosp.payroll.dto.MKintaiDto;

public class MKintaiDao extends BaseDao {

	// テーブル名及びキー列名
	public static final String TABLE  		= "M_KINTAI";
	public static final String KEY_1 		= "KCODE";		//社員コード
	public static final String KEY_2 		= "SIKYUYM";	//支給年月

	// 列名
	public static final String COL_KCODE  			= "KCODE";			//社員コード
	public static final String COL_SIKYUYM  		= "SIKYUYM";		//支給年月
	public static final String COL_SNEN  			= "SNEN";			//支給年
	public static final String COL_STUKI  			= "STUKI";			//支給月
	public static final String COL_SHI  			= "SHI";			//支給日
	public static final String COL_SNISSU  		= "SNISSU";			//出勤日数
	public static final String COL_SKAISU  		= "SKAISU";			//出勤回数
	public static final String COL_KINMUJI  		= "KINMUJI";		//勤務時間
	public static final String COL_KYUKEIJI  		= "KYUKEIJI";		//休憩時間
	public static final String COL_YUKYU  			= "YUKYU";			//有給
	public static final String COL_TOKUKYU  		= "TOKUKYU";		//特別休暇
	public static final String COL_KEKKIN  		= "KEKKIN";			//一般欠勤日数
	public static final String COL_SKEKKIN  		= "SKEKKIN";		//傷病欠勤日数
	public static final String COL_DAIKYU  		= "DAIKYU";			//代替休暇日数
	public static final String COL_FURIKAE  		= "FURIKAE";		//振替休暇日数
	public static final String COL_SONOTA  		= "SONOTA";			//その他休暇日数
	public static final String COL_TISOKAI  		= "TISOKAI";		//遅刻早退回数
	public static final String COL_TIJIKAN  		= "TIJIKAN";		//遅刻早退時間
	public static final String COL_ZAJIKAN  		= "ZAJIKAN";		//普通残業時間
	public static final String COL_KSJIKAN  		= "KSJIKAN";		//休日残業時間
	public static final String COL_SNJIKAN  		= "SNJIKAN";		//深夜勤務時間
	public static final String COL_KAIKIN  		= "KAIKIN";			//精皆勤手当支給
	public static final String COL_SYOKUJIKAISU  	= "SYOKUJIKAISU";	//食事手当支給回数
	public static final String COL_CYOKUKAISU1  	= "CYOKUKAISU1";	//1直手当支給回数
	public static final String COL_CYOKUKAISU2  	= "CYOKUKAISU2";	//2直手当支給回数
	public static final String COL_CYOKUKAISU3  	= "CYOKUKAISU3";	//3直手当支給回数
	public static final String COL_HOKAKAISU1  	= "HOKAKAISU1";		//その他支給回数1
	public static final String COL_HOKAKAISU2  	= "HOKAKAISU2";		//その他支給回数2
	public static final String COL_HOKAKAISU3  	= "HOKAKAISU3";		//その他支給回数3
	public static final String COL_HOKAKAISU4  	= "HOKAKAISU4";		//その他支給回数4
	public static final String COL_HOKAKAISU5  	= "HOKAKAISU5";		//その他支給回数5
	public static final String COL_SINSEIGAKU  	= "SINSEIGAKU";		//支払申請額
	public static final String COL_NIJIKYU  		= "NIJIKYU";		//二時間年休
	public static final String COL_WARIMASI1 		= "WARIMASI1";		//1.00割増時間

	/**
	 * コンストラクタ
	 */
	public MKintaiDao() {
		super();
	}

	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<MKintaiDto> mappingAll() throws SQLException {
		List<MKintaiDto> all = new ArrayList<MKintaiDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}

	/**
	 * 検索結果取得(DTO)
	 * @return m_kintaiDto m_kintaiレコード
	 * @throws SQLException
	 */
	private MKintaiDto mapping() throws SQLException {
		MKintaiDto dto = new MKintaiDto();
		dto.setKCode 		(rs.getString (COL_KCODE)		);	//社員コード
		dto.setSikyuYM 		(rs.getString (COL_SIKYUYM)		);	//支給年月
		dto.setSNen 		(rs.getInt    (COL_SNEN)		);	//支給年
		dto.setSTuki 		(rs.getInt    (COL_STUKI)		);	//支給月
		dto.setSHi 			(rs.getInt    (COL_SHI)			);	//支給日
		dto.setSNissu 		(rs.getDouble (COL_SNISSU)		);	//出勤日数
		dto.setSKaisu 		(rs.getInt    (COL_SKAISU)		);	//出勤回数
		dto.setKinmuJi 		(rs.getInt    (COL_KINMUJI)		);	//勤務時間
		dto.setKyukeiJi 	(rs.getInt    (COL_KYUKEIJI)	);	//休憩時間
		dto.setYukyu 		(rs.getDouble (COL_YUKYU)		);	//有給
		dto.setTokuKyu	 	(rs.getDouble (COL_TOKUKYU)		);	//特別休暇
		dto.setKekkin 		(rs.getDouble (COL_KEKKIN)		);	//一般欠勤日数
		dto.setSKekkin 		(rs.getDouble (COL_SKEKKIN)		);	//傷病欠勤日数
		dto.setDaiKyu 		(rs.getDouble (COL_DAIKYU)		);	//代替休暇日数
		dto.setFurikae 		(rs.getDouble (COL_FURIKAE)		);	//振替休暇日数
		dto.setSonota		(rs.getInt 	  (COL_SONOTA)		);	//その他休暇日数
		dto.setTiSoKai 		(rs.getInt    (COL_TISOKAI)		);	//遅刻早退回数
		dto.setTiJikan 		(rs.getInt    (COL_TIJIKAN)		);	//遅刻早退時間
		dto.setZaJikan 		(rs.getInt    (COL_ZAJIKAN)		);	//普通残業時間
		dto.setKsJikan 		(rs.getInt    (COL_KSJIKAN)		);	//休日残業時間
		dto.setSnJikan 		(rs.getInt    (COL_SNJIKAN)		);	//深夜勤務時間
		dto.setKaikin 		(rs.getInt    (COL_KAIKIN)		);	//精皆勤手当支給
		dto.setSyokujiKaisu (rs.getInt    (COL_SYOKUJIKAISU));	//食事手当支給回数
		dto.setCyokuKaisu1 	(rs.getInt    (COL_CYOKUKAISU1)	);	//1直手当支給回数
		dto.setCyokuKaisu2 	(rs.getInt    (COL_CYOKUKAISU2)	);	//2直手当支給回数
		dto.setCyokuKaisu3 	(rs.getInt    (COL_CYOKUKAISU3)	);	//3直手当支給回数
		dto.setHokaKaisu1 	(rs.getInt    (COL_HOKAKAISU1)	);	//その他支給回数1
		dto.setHokaKaisu2 	(rs.getInt    (COL_HOKAKAISU2)	);	//その他支給回数2
		dto.setHokaKaisu3 	(rs.getInt    (COL_HOKAKAISU3)	);	//その他支給回数3
		dto.setHokaKaisu4 	(rs.getInt    (COL_HOKAKAISU4)	);	//その他支給回数4
		dto.setHokaKaisu5 	(rs.getInt    (COL_HOKAKAISU5)	);	//その他支給回数5
		dto.setSinseiGaku 	(rs.getInt    (COL_SINSEIGAKU)	);	//支払申請額
		dto.setNijiKyu		(rs.getInt	  (COL_NIJIKYU)		);	//二時間年休
		dto.setWarimasi1	(rs.getInt	  (COL_WARIMASI1)	);	//1.00割増時間
		return dto;
	}

	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param kcode 検索社員コード
	 * @param sikyuym 検索支給年月
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List findForCondition(
			String kcode,
			String sikyuym
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_KCODE);
		sb.append(" LIKE ? AND ");
		sb.append(COL_SIKYUYM);
		sb.append(" LIKE ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   kcode  );
			setParam(index++,   sikyuym   + "%");
			executeQuery();
			List list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによるデータ取得
	 * @param kcode 社員コード(検索キー)
	 * @param sikyuym 支給年月(検索キー)
	 * @return dto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public MKintaiDto findForKey(
			String kcode,
			String sikyuym
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, kcode);
			setParam(index++, sikyuym);
			executeQuery();
			MKintaiDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによる行ロック取得
	 * @param kcode 社員コード(検索キー)
	 * @param sikyuym 支給年月(検索キー)
	 * @return dto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public MKintaiDto findForUpdate(
			String kcode,
			String sikyuym
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, kcode);
			setParam(index++, sikyuym);
			executeQuery();
			MKintaiDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによる行ロック取得
	 * @param sikyuym 支給年月(検索キー)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public void findForUpdate(
			String sikyuym
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_SIKYUYM);
			sb.append(" = ? ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getForUpdate());
			setParam(index++, sikyuym);
			executeQuery();
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param sikyuym 検索支給年月
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MKintaiDto> findForExport(
			String sikyuym
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_SIKYUYM);
		sb.append(" = ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, sikyuym);
			executeQuery();
			List<MKintaiDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * データ挿入
	 * @param dto 挿入対象DTO
	 * @return int 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int insert(MKintaiDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getInsertQuery(getClass()));
			setParams(dto,true);
			executeUpdate();
			chkInsert(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * データ挿入
	 * @param 	aryDto 挿入対象DTO
	 * @return int 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int insert(MKintaiDto[] aryDto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("INSERT INTO ");
			sb.append(TABLE);
			sb.append("(");
			Field[] fields = getClass().getFields();
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];
				String name = field.getName();
				if (name.indexOf("COL_") == 0) {
					sb.append(field.get(null));
					sb.append(", ");
				}
			}
			if (!INSERT_DATE.equals("")) {
				sb.append(INSERT_DATE + ", ");
			}
			if (!INSERT_USER.equals("")) {
				sb.append(INSERT_USER + ", ");
			}
			if (!UPDATE_DATE.equals("")) {
				sb.append(UPDATE_DATE + ", ");
			}
			if (!UPDATE_USER.equals("")) {
				sb.append(UPDATE_USER + ", ");
			}
			sb.delete(sb.length() - 2, sb.length() - 1);
			sb.append(") VALUES");
			for (int a = 0; a < aryDto.length; a++) {
				sb.append("(");
				for (int i = 0; i < fields.length; i++) {
					Field field = fields[i];
					String name = field.getName();
					if (name.indexOf("COL_") == 0) {
						sb.append("?, ");
					}
				}
				if (!INSERT_DATE.equals("")) {
					sb.append("?, ");
				}
				if (!INSERT_USER.equals("")) {
					sb.append("?, ");
				}
				if (!UPDATE_DATE.equals("")) {
					sb.append("?, ");
				}
				if (!UPDATE_USER.equals("")) {
					sb.append("?, ");
				}
				sb.delete(sb.length() - 2, sb.length());
				sb.append("),");
			}
			sb.delete(sb.length() - 1, sb.length());
			prepareStatement(sb.toString());
			for (int d = 0; d < aryDto.length; d++) {
				setParams(aryDto[d], true);
			}
			executeUpdate();
			chkInsert(aryDto.length);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * データ更新
	 * @param dto 更新対象DTO
	 * @return int 更新結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int update(MKintaiDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getKCode());
			setParam(index++, dto.getSikyuYM());
			executeUpdate();
			chkUpdate(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * データ削除
	 * @param dto 更新対象DTO
	 * @return int 削除結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int delete(MKintaiDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getKCode());
			setParam(index++, dto.getSikyuYM());
			executeUpdate();
			chkDelete(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * データ削除
	 * @param aryDto 更新対象DTO
	 * @return int 削除結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int delete(MKintaiDto[] aryDto, String sikyuym) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("DELETE FROM ");
			sb.append(TABLE);
			sb.append(" WHERE (");
			for (int i = 0; i < aryDto.length; i++) {
				sb.append(COL_KCODE);
				sb.append(" = ? OR ");
			}
			sb.delete(sb.length() - 4, sb.length());
			sb.append(") AND ");
			sb.append(COL_SIKYUYM);
			sb.append(" = ?");
			prepareStatement(sb.toString());
			for (int d = 0; d < aryDto.length; d++) {
				setParam(index++, aryDto[d].getKCode());
			}
			setParam(index++, sikyuym);
			executeUpdate();
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * データ削除
	 * @param 	sikyuym	更新対象年月
	 * @return int 削除結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int delete(String sikyuym) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("DELETE FROM ");
			sb.append(TABLE);
			sb.append(" ");
			sb.append("WHERE ");
			sb.append(COL_SIKYUYM);
			sb.append(" = ? ");
			prepareStatement(sb.toString());
			setParam(index++, sikyuym);
			executeUpdate();
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @throws SQLException
	 */
	private void setParams(MKintaiDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getKCode()		);	//社員コード
		setParam(index++, dto.getSikyuYM()		);	//支給年月
		setParam(index++, dto.getSNen()			);	//支給年
		setParam(index++, dto.getSTuki()		);	//支給月
		setParam(index++, dto.getSHi()			);	//支給日
		setParam(index++, dto.getSNissu()		);	//出勤日数
		setParam(index++, dto.getSKaisu()		);	//出勤回数
		setParam(index++, dto.getKinmuJi()		);	//勤務時間
		setParam(index++, dto.getKyukeiJi()		);	//休憩時間
		setParam(index++, dto.getYukyu()		);	//有給
		setParam(index++, dto.getTokuKyu()		);	//特別休暇
		setParam(index++, dto.getKekkin()		);	//一般欠勤日数
		setParam(index++, dto.getSKekkin()		);	//傷病欠勤日数
		setParam(index++, dto.getDaiKyu()		);	//代替休暇日数
		setParam(index++, dto.getFurikae()		);	//振替休暇日数
		setParam(index++, dto.getSonota()		);	//その他休暇日数
		setParam(index++, dto.getTiSoKai()		);	//遅刻早退回数
		setParam(index++, dto.getTiJikan()		);	//遅刻早退時間
		setParam(index++, dto.getZaJikan()		);	//普通残業時間
		setParam(index++, dto.getKsJikan()		);	//休日残業時間
		setParam(index++, dto.getSnJikan()		);	//深夜勤務時間
		setParam(index++, dto.getKaikin()		);	//精皆勤手当支給
		setParam(index++, dto.getSyokujiKaisu()	);	//食事手当支給回数
		setParam(index++, dto.getCyokuKaisu1()	);	//1直手当支給回数
		setParam(index++, dto.getCyokuKaisu2()	);	//2直手当支給回数
		setParam(index++, dto.getCyokuKaisu3()	);	//3直手当支給回数
		setParam(index++, dto.getHokaKaisu1()	);	//その他支給回数1
		setParam(index++, dto.getHokaKaisu2()	);	//その他支給回数2
		setParam(index++, dto.getHokaKaisu3()	);	//その他支給回数3
		setParam(index++, dto.getHokaKaisu4()	);	//その他支給回数4
		setParam(index++, dto.getHokaKaisu5()	);	//その他支給回数5
		setParam(index++, dto.getSinseiGaku()	);	//支払申請額
		setParam(index++, dto.getNijiKyu()		);	//二時間年休
		setParam(index++, dto.getWarimasi1()	);	//1.00割増時間
		setCommonParams(isInsert);
	}

	/**
	 * テーブルロック
	 * @param 	sikyuYM	更新対象年月
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public void tableLock(String sikyuYM) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			if (conn.getMetaData().getDatabaseProductName().equals("MySQL")) {
				// DBがMySQLの場合は行ロック
				findForUpdate(sikyuYM);
			} else {
				// DBがPostgreSQLの場合はテーブルロック
				StringBuffer sb = new StringBuffer();
				sb.append("LOCK TABLE ");
				sb.append(TABLE);
				prepareStatement(sb.toString());
				executeUpdate();
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * テーブルロック
	 * @param	kCode	社員コード
	 * @param 	sikyuYM	更新対象年月
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public void tableLock(String kCode, String sikyuYM) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			if (conn.getMetaData().getDatabaseProductName().equals("MySQL")) {
				// DBがMySQLの場合は行ロック
				findForUpdate(kCode, sikyuYM);
			} else {
				// DBがPostgreSQLの場合はテーブルロック
				StringBuffer sb = new StringBuffer();
				sb.append("LOCK TABLE ");
				sb.append(TABLE);
				prepareStatement(sb.toString());
				executeUpdate();
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * テーブルロック
	 * @param	listMKintai	DTO
	 * @param 	sikyuYM		更新対象年月
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public void tableLock(List<MKintaiDto> listMKintai, String sikyuYM) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			if (conn.getMetaData().getDatabaseProductName().equals("MySQL")) {
				// DBがMySQLの場合は行ロック
				for (Iterator iter = listMKintai.iterator(); iter.hasNext();) {
					MKintaiDto dto = (MKintaiDto)iter.next();
					findForUpdate(dto.getKCode(), sikyuYM);
				}
			} else {
				// DBがPostgreSQLの場合はテーブルロック
				StringBuffer sb = new StringBuffer();
				sb.append("LOCK TABLE ");
				sb.append(TABLE);
				prepareStatement(sb.toString());
				executeUpdate();
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

}
