JCO訪問SAP進行增刪改

1.須要下載jco的相關jar包和.dll文件,若是Web項目,須要將dll文件放到tomcat的bin目錄下面,在項目中引入jco的jar包,若是不是web項目,直接所有放到lib下面就能夠了。 java

2. web

package com.towery.utils;

import com.sap.mw.jco.JCO;

import java.util.Properties;

/**
 * Created by Administrator on xxxxx.
 */
public class BWUtils {
    static Properties logonProperties = new Properties();

    static{
        try{
            logonProperties.load(BWUtils.class.getResourceAsStream("bw_logon.properties"));
        }catch(Exception e){
            System.out.println("加載配置文件出錯!");
        }
    }

    /**
     * 獲取鏈接
     * @return
     */
    public static JCO.Client getConnection(){
        return JCO.createClient(logonProperties);
    }

    /**
     * 關閉鏈接
     * @param conn
     */
    public static void closeConn(JCO.Client conn){
        if(conn != null){
            conn.disconnect();
        }
    }

    public static void main(String[] args) {
        JCO.Client mConn = getConnection();
        mConn.connect();
        System.out.println(mConn.getAttributes());
        closeConn(mConn);
    }
}


配置文件以下: tomcat


jco.client.ashost=10.5.60.118
jco.client.client=001
jco.client.sysnr=02
jco.client.user=EKA0281
jco.client.passwd=bw1234



從SAP查詢數據:



package com.towery.dao.impl;

import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.JCO;
import com.towery.dao.BWSync;
import com.towery.entity.Branch;
import com.towery.entity.Factory;
import com.towery.entity.Store;
import com.towery.utils.TempUtils;

import java.util.ArrayList;
import java.util.List;

import static com.towery.utils.BWUtils.*;

/**
 * Created by Administrator on 2015/7/15.
 */
public class BWSyncImpl implements BWSync {
    @Override
    public List<Object> syncData(String function, String tabName) {
        List<Object> ol = new ArrayList<Object>();
        JCO.Client mConn = getConnection();
        try{
            mConn.connect();
            if(mConn != null && mConn.isAlive()){
                System.out.println(mConn.getAttributes());
                //從鏈接得到一個邏輯意義上的‘倉庫’對象'
                JCO.Repository myRepository = new JCO.Repository("myRepository",mConn);
                //從‘倉庫’得到一個指定函數名稱的函數模板
                IFunctionTemplate ft = myRepository.getFunctionTemplate(function.toUpperCase());
                //從這個函數模板得到該SAP函數的對象
                JCO.Function fun = ft.getFunction();
                mConn.execute(fun);//執行該SAP函數
                JCO.Table table = fun.getTableParameterList().getTable(tabName);//從該函數的Table參數列表得到一個指定名稱的table

                if(tabName.equals("T_ZTORG")){//分公司
                    do {
                        Object branchid = table.getValue("ZORG");
                        Object branchname = table.getValue("ZORG_TXT");
                        Object areaname = table.getValue("ZONE");
                        String tp = "branchid:%s,branchname:%s,areaname:%s";

                        System.out.println(String.format(tp,branchid,branchname,areaname));
                        Branch b = new Branch();
                        b.setBranchId(branchid.toString());
                        b.setBranchName(branchname.toString());
                        b.setAreaId(TempUtils.getAreaId(areaname.toString()));

                        ol.add(b);
                    }while(table.nextRow());
                }else if(tabName.equals("T_ZTPLANT")){//工廠
                    do {
                        Object factoryid = table.getValue("ZPLANT");
                        Object factoryname = table.getValue("ZPLANT_TXT");
                        Object branchid = table.getValue("ZORG");
                        String tp = "factoryid:%s,factoryname:%s,branchid:%s";
                        System.out.println(String.format(tp,factoryid,factoryname,branchid));

                        if(!branchid.toString().equals("") && (branchid != null) && !factoryname.toString().equals("電子商務中心倉")){
                            Factory f = new Factory();
                            f.setFacId(factoryid.toString());
                            f.setFacName(factoryname.toString());
                            f.setBranchId(branchid.toString());
                            ol.add(f);
                        }

                    }while(table.nextRow());
                }else if(tabName.equals("T_STOR_LOC")){//門店
                    do {
                        Object storeid = table.getValue("STOR_LOC");
                        Object storename = table.getValue("TXTMD");
                        Object factoryid = table.getValue("PLANT");
                        String tp = "storeid:%s,storename:%s,factoryid:%s";
                        System.out.println(String.format(tp,storeid,storename,factoryid));
                        if(!factoryid.toString().equals("") && factoryid != null){
                            Store s = new Store();
                            s.setStoreId(storeid.toString());
                            s.setStoreName(storename.toString());
                            s.setFactoryId(factoryid.toString());
                            ol.add(s);
                        }

                    }while(table.nextRow());
                }
            }
        }catch(JCO.Exception e){
            e.printStackTrace();
        }finally {
            if(mConn != null){
                closeConn(mConn);
            }
        }

        return ol;
    }
}



更新和刪除,在SAP中建立的函數是給它的輸入參數傳值"I"表示更新和插入,傳值"D"表示刪除



package com.towery.imp.dao.impl;

import java.util.List;

import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.JCO;
import com.towery.imp.bean.CompositeIndicators;
import com.towery.imp.bean.CostRate;
import com.towery.imp.bean.DaySalesAmount;
import com.towery.imp.bean.DirectSales;
import com.towery.imp.bean.EfficiencyTarget;
import com.towery.imp.bean.OrderImp;
import com.towery.imp.bean.OrderTarget;
import com.towery.imp.bean.PaymentIndicators;
import com.towery.imp.bean.SalesRatio;
import com.towery.imp.bean.SoldOutTarget;
import com.towery.imp.bean.UpperData;
import com.towery.imp.dao.BWSynchronize;
import com.towery.imp.utils.BWUtils;
import com.towery.imp.utils.DateUtils;

public class BWSynchronizeImpl implements BWSynchronize {

	public boolean synchronizeData(List<Object> ol, String param, String function,String tabName) {
		if(ol.size() > 0){
			JCO.Client myConn = BWUtils.getConnection();//獲取鏈接
			try{
				myConn.connect();
				//若是鏈接不爲null而且處於活動狀態
				if(myConn != null && myConn.isAlive()){
					System.out.println(myConn.getAttributes());
					//從鏈接得到一個邏輯意義上的「倉庫」對象(Repository)
					JCO.Repository myRepository = new JCO.Repository("Repository", myConn);
					//從「倉庫」中得到一個指定函數名的函數模板
					IFunctionTemplate ft = myRepository.getFunctionTemplate(function.toUpperCase());
					//從這個函數模板得到該SAP函數的對象
					JCO.Function fun = ft.getFunction();
					//得到函數的import參數列表
					JCO.ParameterList input = fun.getImportParameterList();
					input.setValue(param, "I_FLAG");//設置輸入參數
					//得到函數的export參數列表
					JCO.ParameterList output = fun.getExportParameterList();
					//獲取table參數列表
					JCO.Table tab = fun.getTableParameterList().getTable(tabName);
					
						for(int i = 0; i < ol.size(); i++){
							PaymentIndicators pi = (PaymentIndicators) ol.get(i);
							tab.insertRow(i+1);
							tab.setValue("", "/BIC/Z3MBQR");//目標確認狀況
				        	tab.setValue(pi.getArea_name(), "/BIC/Z1ZONE");//大區
				        	tab.setValue(pi.getBranch_id(), "/BIC/Z1_ORG");//分公司
				        	tab.setValue(pi.getBranch_name(), "/BIC/Z3ORGTXT");//公司名稱
				        	tab.setValue(pi.getFac_id(), "/BIC/Z3PLANT");//工廠
				        	tab.setValue(pi.getFac_name(), "/BIC/Z3PLATXT");//工廠描述
				        	tab.setValue(pi.getPi_year(), "/BIC/Z3YEAR");//年
				        	tab.setValue(pi.getPi_month(), "/BIC/Z3MONTH");//月
				        	tab.setValue(pi.getPi_create_date(), "/BIC/Z3CJDATE");//建立日期
				        	tab.setValue("", "/BIC/Z3GXDATE");//更新日期
				        	tab.setValue("", "RECORDMODE");//增量模式:更新處理
				        	tab.setValue(pi.getPi_base_target(), "/BIC/Z3JCHKMB");//基礎回款目標
				        	tab.setValue(pi.getPi_complete_target(), "/BIC/Z3DCHKMB");//達成回款目標
				        	tab.setValue(pi.getPi_challenge_target(), "/BIC/Z3TZHKMB");//挑戰回款目標
				        	tab.setValue(pi.getPi_sale_target(), "/BIC/Z3XSHKMB");//銷售目標
						}
					
		        	myConn.execute(fun);
					System.out.println("E_MESSAGE=" + output.getString("E_MESSAGE"));
					if(output.getString("E_MESSAGE").equals("S")){
						return true;
					}
				}
			}catch(JCO.Exception e){
				System.out.println("鏈接失敗!");
				e.printStackTrace();
			}finally{
				if(myConn != null){
					BWUtils.closeConn(myConn);
				}
			}
		}
		return false;
	}

}
相關文章
相關標籤/搜索