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;
	}

}
相关文章
相关标签/搜索