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

📄 zgzyswygdtask.java

📁 一个真实项目的源代码。有一个比较优秀的时间类
💻 JAVA
字号:
package com.zx.dangangl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

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

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

public class ZgzyswYgdTask {

	private static Log log = LogFactory.getLog(ZgzyswYgdTask.class);

	/**
	 * @param nd
	 *            年度,格式yyyy 。例如:2006
	 * @param ygdIds
	 *            需要预归档的记录的id。格式: id,id,id 。例如:"2,3,4"
	 * @return 如果返回结果为“success”说明执行成功,否则,返回的是错误信息。
	 */
	public String zgzySwYgd(String nd, String ygdIds) throws OurException{
		if (nd == null || nd.trim().equals("")) {
			return "年度不能为空。";
		}
		if (ygdIds == null || ygdIds.trim().equals("")) {
			return "预归档的id不能为空。";
		}

		String qianBaoYgdSql = "select id,lwjg as zrz,lwzh as wh,wjbt as tm,cwrq rq,ys "
				+ " from zx_zgzyswdj "
				+ " where sfygd=0  and substring(cwrq,1,4)='" + nd + "' " // 对应的年度
				+ " and id in (" + ygdIds + ") ";
		log.debug("要进行预归档的sql语句为qianBaoYgdSql:" + qianBaoYgdSql);

		List srcList = DbUtil.executeQueryList(qianBaoYgdSql); // 首先从签报登记表中查询到对应的记录
		if (srcList == null) {
			return "未查询到任何需要预归档的普通收文登记记录。";
		}
		int LEN = srcList.size();

		String insertToJianSql = "insert into zx_jian "
				+ "(zrz,wh,tm,rq,ys,glbm,glid) values (?,?,?,?,?,?,?)";
		
		String sfygdSql = "update zx_zgzyswdj set sfygd=1 where id in ("
				+ ygdIds + ")";

		String zrz = "";  //责任者
		String wh = "";	  //文号
		String tm = "";   //题名
		String rq = "";   //日期 
		int ys = 0;       //页数
		
		String glbm ="zx_zgzyswdj"; //关联表名
		int glid = 0;           //关联id
		

		Connection conn = DbConnection.getConn();
		if (conn == null)
			throw new OurException("获取数据库连接失败!");

		PreparedStatement pst = null;
		PreparedStatement pst2 = null;
		int result = 0;
		int result2 = 0; // 这个sqlserver驱动程序竟然不支持PreparedStatement循环传递参数。
		try {
			conn.setAutoCommit(false);
			pst = conn.prepareStatement(insertToJianSql);
			pst2 = conn.prepareStatement(sfygdSql);

			for (int i = 0; i < LEN; i++) {
				HashMap map = (HashMap) srcList.get(i);

				zrz = (String) map.get("zrz");
				wh = (String) map.get("wh");
				tm = (String) map.get("tm");
				rq = (String) map.get("rq");
				ys = ((Integer) map.get("ys")).intValue();
				//glbm 
				glid = ((Integer) map.get("id")).intValue();

				pst.setString(1, zrz);
				pst.setString(2, wh);
				pst.setString(3, tm);
				pst.setString(4, rq);
				pst.setInt(5, ys);
				pst.setString(6, glbm);
				pst.setInt(7, glid);

				result += pst.executeUpdate();
			}

			result2 = pst2.executeUpdate(); 

			conn.commit();
			log.debug("共" + result + "条中共中央收文记录预归档成功!共" + result2
					+ "条中共中央收文记录设置预归档标志成功!");

		} catch (SQLException e) {
			log.error("出现异常!由于预归档了" + result + "记录,更新了" + result2
					+ "条记录!所以预归档失败!" + "估计是记录数目太多,超出了PreparedStatement的缓存数!");
			try {
				conn.rollback();
			} catch (SQLException e1) {
				log.error("会滚失败!",e1);
			}

			throw new OurException("中共中央收文自动预归档执行失败!请通知管理员!", e);

		} finally {
			DbUtil.closeStatement(pst2);
			DbUtil.closeStatement(pst);
			DbUtil.closeConnection(conn);
		}

		return "success";
	}


}

⌨️ 快捷键说明

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