jdbc

package com.ccic.service.cash.time;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.rpc.ServiceException;

import org.apache.log4j.Logger;

import com.ccic.service.util.CommonFunction;
import com.ccic.service.util.DataSourceUtil;
import com.ccic.service.util.SQLFunction;
import com.ccic.service.util.SysParam;
import com.hundsun.dto.Account;
import com.hundsun.service.SyncAccountHttpBindingStub;
import com.hundsun.service.SyncAccountLocator;
import com.hundsun.service.SyncAccountPortType;

public class SynAccount {

 private static Logger logger = Logger.getLogger(SynAccount.class);

 // 将数据进行备份,同时删除中间表
 public static boolean bakData(Connection conn, PreparedStatement ps)
   throws SQLException {

  String sql = "insert into t_accounthis select * from t_accountmid";
  String deleteSQL = "delete from t_accountmid";

  ps = conn.prepareStatement(sql);
  ps.executeUpdate();
  ps.close();
  ps = conn.prepareStatement(deleteSQL);
  ps.executeUpdate();
  ps.close();
  return true;
 }

 // 更新银行账号信息表为无效
 public static boolean updateInvalid(Connection conn, PreparedStatement ps)
   throws SQLException {

  String updateInvalid = "update t_bankaccount  set ACCOUNT_STATUS = '0'";
  ps = conn.prepareStatement(updateInvalid);
  int updateRow = ps.executeUpdate();
  ps.close();
  logger.info("更新银行账号信息表为无效的行数:" + updateRow);
  return true;
 }

 // 调用恒生webservice,获得响应体数据
 public static Account[] getAccountInfo() throws ServiceException,
   RemoteException {
  
  String endPointUrl = SysParam.getSysParam("synAccount");
  logger.info("资金账号同步服务地址:"+endPointUrl);
  
  SyncAccountLocator locator = new SyncAccountLocator();
  locator.setSyncAccountHttpPortEndpointAddress(endPointUrl);
  
  SyncAccountPortType service = locator.getSyncAccountHttpPort();
  Account accountArr[] = null;
  SyncAccountHttpBindingStub stub = (SyncAccountHttpBindingStub) service;
  stub.setTimeout(100000);
  String str[] = { "" };
  accountArr = stub.syncAcc(str, "0");

  return accountArr;
 }

 // 手工加入数据调试
 public static Account[] manu() {
  Account accountArr[] = new Account[2];
  Account a1 = new Account();
  Account a2 = new Account();

  a1.setACCOUNT_ID(1);
  a1.setACCOUNT_OTHER_CODE("001");
  a1.setACCOUNT_OTHER_FLAG(1);
  a1.setACCOUNT_STATUS(1);
  a1.setBANK_ACCOUNT("622001");
  a1.setBANK_ACCOUNT_CURRENCY("CNY");
  a1.setBANK_ACCOUNT_NAME("张一");
  a1.setBANK_ACCOUNT_TYPE("1");
  a1.setBANK_AREA_NAME("上海杨浦");
  a1.setBANK_CODE("001");
  a1.setBANK_DETAIL_CODE("000001");
  a1.setBANK_DETAIL_NAME("国顺路支行");
  a1.setBANK_LOCATIONS_NAME("中原路1022号");
  a1.setBANK_NAME("上海浦发银行");
  a1.setBANK_SAVE_TYPE(1);
  // a1.setCLOSE_DATE("");
  a1.setDirect_FLAG(0);
  a1.setDOOR_ID("1");
  a1.setENTITY_CODE("01");
  a1.setENTITY_NAME("01");
  a1.setIS_OPEN(1);
  // a1.setOPEN_DATE("");

  a2.setACCOUNT_ID(2);
  a2.setACCOUNT_OTHER_CODE("002");
  a2.setACCOUNT_OTHER_FLAG(2);
  a2.setACCOUNT_STATUS(0);
  a2.setBANK_ACCOUNT("622002");
  a2.setBANK_ACCOUNT_CURRENCY("CNY");
  a2.setBANK_ACCOUNT_NAME("张二");
  a2.setBANK_ACCOUNT_TYPE("2");
  a2.setBANK_AREA_NAME("上海杨浦");
  a2.setBANK_CODE("001");
  a2.setBANK_DETAIL_CODE("000001");
  a2.setBANK_DETAIL_NAME("国顺路支行");
  a2.setBANK_LOCATIONS_NAME("中原路1022号");
  a2.setBANK_NAME("上海浦发银行");
  a2.setBANK_SAVE_TYPE(2);
  a2.setDirect_FLAG(0);
  a2.setDOOR_ID("1");
  a2.setENTITY_CODE("02");
  a2.setENTITY_NAME("02");
  a2.setIS_OPEN(1);

  accountArr[0] = a1;
  accountArr[1] = a2;

  return accountArr;
 }

 // 将数据转换成Map,便于转换成insert语句,针对更新服务信息中间表T_ACCOUNTMID
 @SuppressWarnings("unchecked")
 public static List arrayToMapMid(Account accountArr[]) {

  List<Map> list = new ArrayList<Map>();
  logger.info("获得的服务端账号信息如下:");
  for (int i = 0; i < accountArr.length; i++) {
   Map m = new HashMap();
   Account account = new Account();
   account = accountArr[i];

   logger.info("账号ID号:" + account.getACCOUNT_ID() + ",银行账户号:"
     + account.getBANK_ACCOUNT());

   m.put("ACCOUNT_ID", "" + account.getACCOUNT_ID());
   m.put("ACCOUNT_STATUS", "" + account.getACCOUNT_STATUS());
   m.put("ACCOUNT_OTHER_FLAG", "" + account.getACCOUNT_OTHER_FLAG());
   m.put("ACCOUNT_OTHER_CODE", "" + account.getACCOUNT_OTHER_CODE());

   m.put("BANK_CODE", "" + account.getBANK_CODE());
   m.put("BANK_NAME", account.getBANK_NAME());
   m.put("BANK_AREA_NAME", account.getBANK_AREA_NAME());
   m.put("BANK_LOCATIONS_NAME", account.getBANK_LOCATIONS_NAME());
   m.put("BANK_ACCOUNT", account.getBANK_ACCOUNT());
   m.put("BANK_ACCOUNT_NAME", account.getBANK_ACCOUNT_NAME());
   m.put("BANK_ACCOUNT_CURRENCY", account.getBANK_ACCOUNT_CURRENCY());
   m.put("BANK_ACCOUNT_TYPE", "" + account.getBANK_ACCOUNT_TYPE());
   m.put("BANK_SAVE_TYPE", "" + account.getBANK_SAVE_TYPE());
   if ("".equals(account.getBANK_DETAIL_CODE())) {
    m.put("BANK_DETAIL_CODE", "无");
   } else {
    m.put("BANK_DETAIL_CODE", account.getBANK_DETAIL_CODE());
   }
   if ("".equals(account.getBANK_DETAIL_NAME())) {
    m.put("BANK_DETAIL_NAME", "无");
   } else {
    m.put("BANK_DETAIL_NAME", account.getBANK_DETAIL_NAME());
   }

   if("".equals(account.getCLOSE_DATE())||account.getCLOSE_DATE()==null){
    m.put("CLOSE_DATE","");
   }else{
    m.put("CLOSE_DATE",account.getCLOSE_DATE().getTime().toLocaleString());
   }
   
   if("".equals(account.getOPEN_DATE())||account.getOPEN_DATE()==null){
    m.put("OPEN_DATE","");
   }else{
    m.put("OPEN_DATE",account.getOPEN_DATE().getTime().toLocaleString());
   }
   
   
   m.put("DOORID", ""+account.getDOOR_ID());
   m.put("DIRECT_FLAG", "" + account.getDirect_FLAG());
   m.put("ENTITY_CODE", "" + account.getENTITY_CODE());
   m.put("ENTITY_NAME", "" + account.getENTITY_NAME());
   m.put("IS_OPEN", ""+account.getIS_OPEN());

   m.put("PROCESS_STATUS_2", "2");
   m.put("PROCESS_DATE_2", CommonFunction.getLocalTime14());
   m.put("PROCESS_MESSAGE_2", "");
   m.put("ROW_VERSION", "1");
   m.put("SYNCBANKFLAG", "0");
   m.put("SYNCBANKDESC", "");
   m.put("SYNCACCOUNTFLAG", "0");
   m.put("SYNCACCOUNTDESC", "");

   list.add(m);
  }
  return list;
 }

 // 将数据转换成Map,便于转换成update语句,针对银行账号业务表T_BANKACCOUNT
 @SuppressWarnings({ "rawtypes", "unchecked" })
 public static List arrayToMapBank(Account accountArr[]) {

  List<Map> list = new ArrayList<Map>();

  for (int i = 0; i < accountArr.length; i++) {

   Account account = new Account();
   account = accountArr[i];
   Map m = new HashMap();
   m.put("ACCOUNT_ID", "" + account.getACCOUNT_ID());
   m.put("ACCOUNT_STATUS", "1");
   m.put("ACCOUNT_OTHER_FLAG", "" + account.getACCOUNT_OTHER_FLAG());
   m.put("ACCOUNT_OTHER_CODE", "" + account.getACCOUNT_OTHER_CODE());

   m.put("BANK_CODE", account.getBANK_CODE());
   m.put("BANK_NAME", account.getBANK_NAME());
   m.put("BANK_AREA_NAME", account.getBANK_AREA_NAME());
   m.put("BANK_LOCATIONS_NAME", account.getBANK_LOCATIONS_NAME());
   m.put("BANK_ACCOUNT", account.getBANK_ACCOUNT());
   m.put("BANK_ACCOUNT_NAME", account.getBANK_ACCOUNT_NAME());
   m.put("BANK_ACCOUNT_CURRENCY", account.getBANK_ACCOUNT_CURRENCY());
   m.put("BANK_ACCOUNT_TYPE", "" + account.getBANK_ACCOUNT_TYPE());
   m.put("BANK_SAVE_TYPE", "" + account.getBANK_SAVE_TYPE());

   if ("".equals(account.getBANK_DETAIL_CODE())) {
    m.put("BANK_DETAIL_CODE", "无");
   } else {
    m.put("BANK_DETAIL_CODE", "" + account.getBANK_DETAIL_CODE());
   }
   if ("".equals(account.getBANK_DETAIL_NAME())) {
    m.put("BANK_DETAIL_NAME", "无");
   } else {
    m.put("BANK_DETAIL_NAME", "" + account.getBANK_DETAIL_NAME());
   }

   if("".equals(account.getCLOSE_DATE())||account.getCLOSE_DATE()==null){
    m.put("CLOSE_DATE","");
   }else{
    m.put("CLOSE_DATE",account.getCLOSE_DATE().getTime().toLocaleString());
   }
   
   if("".equals(account.getOPEN_DATE())||account.getOPEN_DATE()==null){
    m.put("OPEN_DATE","");
   }else{
    m.put("OPEN_DATE",account.getOPEN_DATE().getTime().toLocaleString());
   }

   m.put("DOORID", "" + account.getDOOR_ID());
   m.put("DIRECT_FLAG", "" + account.getDirect_FLAG());

   m.put("ENTITY_CODE", account.getENTITY_CODE());
   m.put("ENTITY_NAME", account.getENTITY_NAME());

   m.put("IS_OPEN", "" + account.getIS_OPEN());

   m.put("TUPDTM", CommonFunction.getLocalTime14());
   m.put("TCRTTM", CommonFunction.getLocalTime14());
   m.put("SYN_PROCESS_STATUS", "0");
   m.put("SYN_PROCESS_INFO", "");

   list.add(m);
  }
  return list;
 }

 // 获得insert语句数组
 public static String[] getInsertSQLArr(String tableName, List<Map> list) {

  String insertSQLArr[] = new String[list.size()];
  for (int i = 0; i < list.size(); i++) {
   Map map = new HashMap();
   map = list.get(i);
   String insertSQL = SQLFunction.insertSQLSplice(tableName, map);
   insertSQLArr[i] = insertSQL;
  }
  return insertSQLArr;
 }

 // 批量插入到中间表
 public static void exeBatch(Connection conn, Statement st,
   PreparedStatement ps, String[] insertSQL) throws SQLException {

  try {

   st = conn.createStatement();
   for (int i = 0; i < insertSQL.length; i++) {
    st.addBatch(insertSQL[i]);
   }
   st.executeBatch();
   st.clearBatch();
   st.close();
  } catch (SQLException e) {
   e.printStackTrace();
   throw e;
  }

 }

 // 格式化时间
 public static java.util.Calendar format(Date date) {

  Calendar calendar = Calendar.getInstance();
  calendar.setTime(date);
  return calendar;
 }

 // 往中间任务表插入从服务端接收过来的数据
 public static boolean insertAccMid(Connection conn, PreparedStatement ps,
   Account[] accountArr) throws SQLException {

  String insertCol = "insert into t_accountmid "
    + "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,"
    + "?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

  ps = conn.prepareStatement(insertCol);
  
  logger.info("获得的服务端账号信息如下:");

  for (int i = 0; i < accountArr.length; i++) {

   Account account = accountArr[i];
   
   logger.info("账号ID号:" + account.getACCOUNT_ID() + ",银行账户号:"
     + account.getBANK_ACCOUNT());
   
   ps.setString(1, "" + account.getACCOUNT_ID());
   ps.setString(2, "" + account.getENTITY_CODE());
   ps.setString(3, "" + account.getENTITY_NAME());
   ps.setString(4, "" + account.getBANK_CODE());
   ps.setString(5, "" + account.getBANK_NAME());
   ps.setString(6, "" + account.getBANK_AREA_NAME());
   ps.setString(7, "" + account.getBANK_LOCATIONS_NAME());
   ps.setString(8, "" + account.getBANK_ACCOUNT());
   ps.setString(9, "" + account.getBANK_ACCOUNT_NAME());
   ps.setString(10, "" + account.getBANK_ACCOUNT_CURRENCY());
   ps.setString(11, "" + account.getBANK_ACCOUNT_TYPE());
   ps.setString(12, "" + account.getBANK_SAVE_TYPE());
   
   if("".equals(account.getOPEN_DATE())||account.getOPEN_DATE()==null){
    ps.setString(13, "");
   }else{
    ps.setString(13, account.getOPEN_DATE().getTime().toLocaleString());
   }
   
   if("".equals(account.getCLOSE_DATE())||account.getCLOSE_DATE()==null){
    ps.setString(14, "");
   }else{
    ps.setString(14, account.getCLOSE_DATE().getTime().toLocaleString());
   }

   ps.setString(15, "" + account.getACCOUNT_STATUS());
   // 新增账户
   ps.setString(16, "");
   
   if("".equals(account.getBANK_DETAIL_CODE())||account.getBANK_DETAIL_CODE()==null){
    ps.setString(17, "无");
   }else{
    ps.setString(17,""+account.getBANK_DETAIL_CODE());
   }
   
   ps.setString(18, "" + account.getDirect_FLAG());
   ps.setString(19, "" + account.getACCOUNT_OTHER_FLAG());
   ps.setString(20, "" + account.getACCOUNT_OTHER_CODE());
   //PROCESS_STATUS_2
   ps.setString(21, "2");
   //PROCESS_DATE_2
   ps.setString(22, CommonFunction.getLocalTime14());
   //PROCESS_MESSAGE_2
   ps.setString(23, "");
   //CREATE_BY
   ps.setString(24, "");
   //ROW_VERSION
   ps.setString(25, "1");
   //IS_OPEN
   ps.setString(26, "" + account.getIS_OPEN());
   //DOORID
   ps.setString(27, account.getDOOR_ID());
   //SYNCBANKFLAG
   ps.setString(28, "1");
   //SYNCBANKDESC
   ps.setString(29, "");
   //SYNCACCOUNTFLAG
   ps.setString(30, "1");
   //SYNCACCOUNTDESC
   ps.setString(31, "");
   //BANK_DETAIL_NAME
   
   if("".equals(account.getBANK_DETAIL_NAME())||account.getBANK_DETAIL_NAME()==null){
    ps.setString(32, "无");
   }else{
    ps.setString(32,""+account.getBANK_DETAIL_NAME());
   }

   ps.addBatch();
  }
  ps.executeBatch();
  ps.clearBatch();
  ps.close();
  return true;
 }
 
 // 往银行账户业务表插入从服务端接收过来的数据 2011-11-04
 public static boolean insertBankAcc(Connection conn, PreparedStatement ps,
   Account[] accountArr) throws SQLException {

  String insertCol = "insert into T_BANKACCOUNT "
    + "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,"
    + "?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

  ps = conn.prepareStatement(insertCol);
  
//  logger.info("获得的服务端账号信息如下:");

  for (int i = 0; i < accountArr.length; i++) {

   Account account = accountArr[i];
   
//   logger.info("账号ID号:" + account.getACCOUNT_ID() + ",银行账户号:"
//     + account.getBANK_ACCOUNT());
   
   ps.setString(1, "" + account.getACCOUNT_ID());
   ps.setString(2, "" + account.getENTITY_CODE());
   ps.setString(3, "" + account.getENTITY_NAME());
   ps.setString(4, "" + account.getBANK_CODE());
   ps.setString(5, "" + account.getBANK_NAME());
   ps.setString(6, "" + account.getBANK_AREA_NAME());
   ps.setString(7, "" + account.getBANK_LOCATIONS_NAME());
   ps.setString(8, "" + account.getBANK_ACCOUNT());
   ps.setString(9, "" + account.getBANK_ACCOUNT_NAME());
   ps.setString(10, "" + account.getBANK_ACCOUNT_CURRENCY());
   ps.setString(11, "" + account.getBANK_ACCOUNT_TYPE());
   ps.setString(12, "" + account.getBANK_SAVE_TYPE());
   
   if("".equals(account.getOPEN_DATE())||account.getOPEN_DATE()==null){
    ps.setString(13, "");
   }else{
    ps.setString(13, account.getOPEN_DATE().getTime().toLocaleString());
   }
   
   if("".equals(account.getCLOSE_DATE())||account.getCLOSE_DATE()==null){
    ps.setString(14, "");
   }else{
    ps.setString(14, account.getCLOSE_DATE().getTime().toLocaleString());
   }

   ps.setString(15, "1");
   // 新增账户
   ps.setString(16, "");
   
   if("".equals(account.getBANK_DETAIL_CODE())||account.getBANK_DETAIL_CODE()==null){
    ps.setString(17, "无");
   }else{
    ps.setString(17,""+account.getBANK_DETAIL_CODE());
   }
   
   ps.setString(18, "" + account.getDirect_FLAG());
   ps.setString(19, "" + account.getACCOUNT_OTHER_FLAG());
   ps.setString(20, "" + account.getACCOUNT_OTHER_CODE());
   //CREATE_BY
   ps.setString(21, "");
   //ROW_VERSION
   ps.setString(22, "1");
   //IS_OPEN
   ps.setString(23, "" + account.getIS_OPEN());
   //DOORID
   ps.setString(24, account.getDOOR_ID());
   
   ps.setString(25, CommonFunction.getLocalTime14());
   ps.setString(26, CommonFunction.getLocalTime14());
   ps.setString(27, "1");
   ps.setString(28, "");
   
   if("".equals(account.getBANK_DETAIL_NAME())||account.getBANK_DETAIL_NAME()==null){
    ps.setString(29, "无");
   }else{
    ps.setString(29,""+account.getBANK_DETAIL_NAME());
   }

   ps.addBatch();
  }
  ps.executeBatch();
  ps.clearBatch();
  ps.close();
  return true;
 }
 
 // 更新银行账号业务表
 public static boolean updateBankAcc(Connection conn, PreparedStatement ps,
   Account[] addAccountExtArr) throws SQLException {

  String colVal = "ACCOUNT_STATUS='1',ACCOUNT_OTHER_FLAG=?,ACCOUNT_OTHER_CODE=?,SYN_PROCESS_INFO=?,"
    + "SYN_PROCESS_STATUS=?,TUPDTM=?,ROW_VERSION=ROW_VERSION+1,OPEN_DATE=?,"
    + "IS_OPEN=?,DIRECT_FLAG=?,DOORID=?,CLOSE_DATE=?,BANK_DETAIL_NAME=?,"
    + "BANK_DETAIL_CODE=?,BANK_SAVE_TYPE=?,BANK_ACCOUNT_TYPE=?,BANK_ACCOUNT_CURRENCY=?,"
    + "BANK_ACCOUNT_NAME=?,BANK_ACCOUNT=?,BANK_LOCATIONS_NAME=?,"
    + "BANK_AREA_NAME=?,BANK_NAME=?,BANK_CODE=? ";

  String updateBankAccSql = "update t_bankaccount set " + colVal
    + "where ACCOUNT_ID =?";

  ps = conn.prepareStatement(updateBankAccSql);

  for (int i = 0; i < addAccountExtArr.length; i++) {

   Account account = addAccountExtArr[i];

   ps.setString(1, "" + account.getACCOUNT_OTHER_FLAG());
   ps.setString(2, account.getACCOUNT_OTHER_CODE());
   ps.setString(3, "");
   ps.setString(4, "1");
   ps.setString(5, CommonFunction.getLocalTime14());
   // ps.setDate(6,(Date)account.getOPEN_DATE().getTime());
   ps.setString(7, "" + account.getIS_OPEN());
   ps.setString(8, "" + account.getDirect_FLAG());
   ps.setString(9, account.getDOOR_ID());
   // ps.setString(10, account.getCLOSE_DATE());
   ps.setString(11, account.getBANK_DETAIL_NAME());
   ps.setString(12, account.getBANK_DETAIL_CODE());
   ps.setString(13, "" + account.getBANK_SAVE_TYPE());
   ps.setString(14, account.getBANK_ACCOUNT_TYPE());
   ps.setString(15, account.getBANK_ACCOUNT_CURRENCY());
   ps.setString(16, account.getBANK_ACCOUNT_NAME());
   ps.setString(17, account.getBANK_ACCOUNT());
   ps.setString(18, account.getBANK_LOCATIONS_NAME());
   ps.setString(19, account.getBANK_AREA_NAME());
   ps.setString(20, account.getBANK_NAME());
   ps.setString(21, account.getBANK_CODE());

   ps.addBatch();
  }

  if (addAccountExtArr.length > 0) {
   ps.executeBatch();
  }
  ps.close();
  return true;
 }

 // 银行和账号信息同步业务处理
 @SuppressWarnings("unchecked")
 public static boolean synBankAcc(Connection conn, PreparedStatement ps,
   Statement st, Account[] accountArr) throws Exception {

  // PreparedStatement是一个管道,多个执行语句可以共用一个
  // ResultSet是操作结果,随用随关
  boolean result = false;
  ResultSet rs = null;

  try {

   List<String> listInvalid = new ArrayList<String>();
   String sql = "select t1.account_id from t_bankaccount t1 "
     + "where not exists"
     + "(select account_id from t_accountmid t2 where t1.account_id=t2.account_id )"
     + " and ACCOUNT_STATUS = '1'";

   ps = conn.prepareStatement(sql);

   rs = ps.executeQuery();

   while (rs.next()) {
    listInvalid.add(rs.getString(1));
   }
   rs.close();
   ps.close();
   String inCond = SQLFunction.getInConditon(listInvalid);
   String updateInvalid = "update t_bankaccount set ACCOUNT_STATUS='0',TUPDTM=? " +
     "where account_id in"+ inCond;
   ps = conn.prepareStatement(updateInvalid);
   ps.setString(1, CommonFunction.getLocalTime14());
   
   int updateRow = ps.executeUpdate();
   ps.close();
   logger.info("此次同步有【" + updateRow + "】条数据被更新为无效");

   String deleteBankAcc = "delete from t_bankaccount where ACCOUNT_STATUS = '1'";
   ps = conn.prepareStatement(deleteBankAcc);
   int deleteRow = ps.executeUpdate();
   ps.close();
   if (deleteRow == 0) {
    logger.info("此次共有【" + accountArr.length + "】条数据被同步!");
   } else {
    logger.info("此次共有【" + deleteRow + "】条数据被同步!");
   }
   
//   List<Map> acclist = arrayToMapBank(accountArr);
//   String insertSQLArr[] = getInsertSQLArr("T_BANKACCOUNT", acclist);
//   exeBatch(conn, st, ps, insertSQLArr);
   
   insertBankAcc(conn,ps,accountArr);
  
   result = true;

  } catch (SQLException e) {
   result = false;
   e.printStackTrace();
   throw e;
  } catch (Exception e) {
   result = false;
   e.printStackTrace();
   throw e;
  }

  return result;
 }

 // 业务入口
 public static Object doTask() {

  Connection conn = null;
  PreparedStatement ps = null;
  // 用于批量插入
  Statement st = null;

  try {

   conn = DataSourceUtil.getConnection();
   // 手动开启事务
   conn.setAutoCommit(false);

   logger.info("*********************中间表数据备份到历史表t_accounthis处理开始***********************");
   // 定时程序开始后,将中间表数据导入到历史表里,然后删除中间表数据
   // 中间表结构与历史表完全相同,只是各个字段都可以为空
   bakData(conn, ps);
   logger.info("*********************中间表数据备份到历史表t_accounthis处理结束***********************");

   // 通过webservice得到账号的全量信息
   Account accountArr[] = getAccountInfo();
   
   logger.info("*********************批量插入服务端返回的数据到中间表处理开始***********************");
   insertAccMid(conn,ps,accountArr);
   logger.info("*********************批量插入服务端返回的数据到中间表处理结束***********************");

   logger.info("*********************银行账号信息同步业务处理开始***********************");
   // 银行账号信息同步业务处理
   synBankAcc(conn, ps, st, accountArr);
   logger.info("*********************银行账号信息同步业务处理结束***********************");

   conn.commit();
   conn.setAutoCommit(true);

  } catch (SQLException e) {
   e.printStackTrace();
   logger.info("", e);
   try {
    conn.rollback();
    conn.setAutoCommit(true);
   } catch (SQLException e1) {
    e1.printStackTrace();
   }
  } catch (ServiceException se) {
   se.printStackTrace();
   logger.info("", se);
   try {
    conn.rollback();
    conn.setAutoCommit(true);
   } catch (SQLException e1) {
    logger.info("", e1);
    e1.printStackTrace();
   }
  } catch (RemoteException re) {
   re.printStackTrace();
   logger.info("", re);
   try {
    conn.rollback();
    conn.setAutoCommit(true);
   } catch (SQLException e1) {
    logger.info("", e1);
    e1.printStackTrace();
   }
  } catch (Exception e) {
   e.printStackTrace();
   logger.info("", e);
   try {
    conn.rollback();
    conn.setAutoCommit(true);
   } catch (SQLException e1) {
    logger.info("", e1);
    e1.printStackTrace();
   }
  } finally {

   try {
    if (st != null) {
     st.close();
    }
    if(ps !=null){
     ps.close();
    }
    if (conn != null) {
     conn.setAutoCommit(true);
     conn.close();
    }
   } catch (SQLException e) {
    e.printStackTrace();
    logger.info("", e);
   }

  }
  return "success";
 }

 // 定时器入口
 public static void main(String args[]) {

  logger.info("***********************账号同步开始***********************");
  doTask();
  logger.info("***********************账号同步结束***********************");
 }
}

你可能感兴趣的:(jdbc)