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

📄 danganhe.java

📁 一个真实项目的源代码。有一个比较优秀的时间类
💻 JAVA
字号:
package com.zx.dangangl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.work.db.DbConnection;
import com.work.db.DbUtil;

public class DangAnHe {
	private static Log log = LogFactory.getLog(DangAnHe.class);

	/**
	 * 增加一个新的盒,首先根据“保管期限”来判断最大的盒号。然后将盒号+1就是新增加盒子的盒号。
	 * 
	 * @param bgqx
	 *            保管期限 不能为空
	 * @param qzh
	 *            全宗号
	 * @param jg
	 *            机构 不能为空
	 * @param nd
	 *            年度 不能为空
	 * @param qzjh
	 *            起至件号 (增加新盒的时候为空)
	 * @param hjhd
	 *            盒脊厚度
	 * @param cfwz
	 *            存放位置
	 * @param remark
	 *            备注
	 */
	public void addDangAnHe(String bgqx, String qzh, String jg, String nd,
			String qzjh, String hjhd, String cfwz, String remark) {
		if (nd == null || nd.trim().equals("")) {
			throw new NullPointerException("档案盒的年度不能为空!");
		}
		if (jg == null || jg.trim().equals("")) {
			throw new NullPointerException("档案盒的机构不能为空!");
		}
		if (bgqx == null || bgqx.trim().equals("")) {
			throw new NullPointerException("档案盒的保管期限不能为空!");
		}
		// 首先按照年度,机构,保管期限,查看档案盒的编号
		String maxHeHaoSql = " select max(hh) from zx_danganhe where jg=" + jg
				+ " and bgqx='" + bgqx + "' and nd=" + nd;
		// 如果得到的结果为null说明没有这一类别的档案盒
		log.debug(maxHeHaoSql);
		Object obj = null;
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rst = null;
		try {
			conn = DbConnection.getConn();
			pst = conn.prepareStatement(maxHeHaoSql);
			rst = pst.executeQuery();
			while (rst.next()) {
				obj = rst.getObject(1);
			}
			int maxHehao = 0;
			if (obj == null) {
				maxHehao = 1;
			} else {
				maxHehao = ((Integer) obj).intValue() + 1;
			}
			String insertSql = "insert into zx_danganhe(hh,bgqx, qzh,jg, nd, hjhd, cfwz, remark) values("
					+ maxHehao
					+ ",'"
					+ bgqx
					+ "','"
					+ qzh
					+ "',"
					+ jg
					+ ","
					+ nd + ",'" + hjhd + "','" + cfwz + "','" + remark + "')";
			log.debug(insertSql);
			pst = conn.prepareStatement(insertSql);
			pst.executeUpdate();
		} catch (SQLException e) {
			log.error(maxHeHaoSql + "语句查询出错!", e);
		} finally {
			DbUtil.closeAll(rst, pst, conn);
		}
	}

	// 增加方法,判断当前的盒号是不是本年度、本机构、保管期限中最大的盒号。主要目的是为了防止档案盒盒号中断

	/**
	 * 进行拆盒动作!只能倒序拆盒,即首先判断当前的盒号是不是最大的。是最大的才能够进行拆盒动作!
	 * 
	 * @param hId
	 *            档案盒的id,注意不是盒号!
	 * @return 返回true表示拆盒成功,否则返回false。
	 */
	public boolean chaiHe(String hId) {
		boolean result = false;
		// 首先根据id,查出当前记录,然后找到年度、机构、保管期限。同时起至件号不能为空字符串,按照这四个条件查询处最大的盒号。 
		// select max(b.hh) from zx_danganhe a ,zx_danganhe b
		// where a.nd=b.nd and a.jg=b.jg and a.bgqx=b.bgqx and a.id =2;
		String hhSql = " select hh from zx_danganhe where id="+hId;
		String sql = " select max(b.hh) from zx_danganhe a ,zx_danganhe b "
				+ " where a.nd=b.nd and a.jg=b.jg and a.bgqx=b.bgqx and b.qzjh<>'' and a.id="
				+ hId;
		int hh = DbUtil.getCount(hhSql);//传入的盒号
		int destHh = DbUtil.getCount(sql);  //1
		if (destHh == hh) {
			// 说明是最大盒号,首先进行拆盒动作,即首先要更新件库中对应的记录。然后进行删除当前的档案盒
			String jianSql = " update zx_jian set hh=0 where hh=" + hId;
			String updateDangAnHe = " update zx_danganhe set qzjh='' where id=" + hId;
			Connection conn = null;
			PreparedStatement pst = null;
			try {
				conn = DbConnection.getConn();
				conn.setAutoCommit(false);
				pst = conn.prepareStatement(jianSql);
				pst.executeUpdate();
				pst = conn.prepareStatement(updateDangAnHe);
				pst.executeUpdate();
				conn.commit();
				result = true;
			} catch (SQLException e) {
				result = false;
				try {
					conn.rollback();
				} catch (SQLException e1) {
					log.info("数据库会滚失败!", e);
				}
				log.error(sql + "语句执行失败!请仔细检查!", e);

			} finally {
				DbUtil.closeStatementAndConnection(pst, conn);
			}
			// 然后
		}
		return result;
	}

	/**
	 * 根据盒的id检查当前盒号是不是同一类别中最大的盒号!true表示是最大的,false表示不是。
	 * 
	 * @param hId
	 *            档案盒的id,注意不是盒号!
	 * @return boolean 
	 */
	public boolean checkMaxHh(String hId) {
		boolean result = false;
		// 首先根据id,查出当前记录,然后找到年度、机构、保管期限。按照这三个条件查询处最大的盒号。
		// select max(b.hh) from zx_danganhe a ,zx_danganhe b
		// where a.nd=b.nd and a.jg=b.jg and a.bgqx=b.bgqx and a.id =2;
		String hhSql = " select hh from zx_danganhe where id="+hId;
		String sql = " select max(b.hh) from zx_danganhe a ,zx_danganhe b "
				+ " where a.nd=b.nd and a.jg=b.jg and a.bgqx=b.bgqx and a.id="
				+ hId;
		int hh = DbUtil.getCount(hhSql);
		int destHh = DbUtil.getCount(sql);
		if (destHh == hh) {
			result = true;
		}
		return result;
	}
}

⌨️ 快捷键说明

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