运行效果
一、配置连接
1、在自己电脑配置Java的开发环境( jdk、tcomcat、sapjco3),
在项目中引入sapjco3.jar包,工具包下载地址:https://mvnrepository.com/artifact/com.sap.conn.jco/sapjco3
http://maven.mit.edu/nexus/content/repositories/public/com/sap/conn/jco/sapjco3/3.0.14/
2、创建Java类,配置连接
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;
public class SAPConn { private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL"; static { Properties connectProperties = new Properties(); connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "192.168.XXX.XX"); //服务器 connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00"); //系统编号 connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "202"); //SAP环境 connectProperties.setProperty(DestinationDataProvider.JCO_USER, "XXX"); // SAP用户名 connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "XXX"); // 密码 connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "ZF"); // 登录语言:ZH ZF EN connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); // 最大连接数 connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); // 最大连接线程 //connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER, ""); //外网访问SAP需设置此参数
createDataFile(ABAP_AS_POOLED,"jcoDestination",connectProperties); } /** * 创建SAP接口属性文件 * @param name * ABAP管道名称 * @param suffix * 属性文件后缀 * @param properties * 属性文件内容 */ private static void createDataFile(String name, String suffix, Properties properties) { // TODO Auto-generated method stub File cfg = new File(name + "." + suffix); if (cfg.exists()) { cfg.deleteOnExit(); } try { FileOutputStream fos = new FileOutputStream(cfg,false); properties.store(fos, "for test only!"); } catch (Exception e) { // TODO: handle exception // System.out.println("Create Data file fault,error msg:" + e.toString()); throw new RuntimeException("无法创建目标文件" + cfg.getName()); } } public static JCoDestination connect() { JCoDestination destination = null; try { destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED); // System.out.println(destination.getAttributes()); } catch (JCoException e) { // TODO: handle exception // System.out.println("连接SAP失败,错误: " + e.toString()); } return destination; } }
二、接口调用
import com.sap.conn.jco.JCoContext;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoRepository;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;
import com.util.SAPConn;
public class Test2 { public static void main(String[] args) throws JCoException { //获取连接 JCoDestination destination = SAPConn.connect(); //获取接口 JCoFunction function = destination.getRepository().getFunction("BAPI_PO_CREATE1"); //Import //参数 //function.getImportParameterList().setValue("NO_MESSAGING", ""); //结构 JCoStructure poHeaderStructure = function.getImportParameterList().getStructure("POHEADER"); poHeaderStructure.setValue("DOC_DATE", "20200312"); //采购文件日期 poHeaderStructure.setValue("DOC_TYPE", "ZROH"); //采购文件类型 poHeaderStructure.setValue("COMP_CODE", "2000"); //公司代码 poHeaderStructure.setValue("PURCH_ORG", "2000"); //采购组织 poHeaderStructure.setValue("PUR_GROUP", "203"); //采购群组 poHeaderStructure.setValue("VENDOR", "0000001000"); //供应商号码 poHeaderStructure.setValue("LANGU", "M"); //语言代码 poHeaderStructure.setValue("CURRENCY", "USD"); //币别 JCoStructure poHeaderxStructure = function.getImportParameterList().getStructure("POHEADERX"); poHeaderxStructure.setValue("DOC_DATE", "X"); //采购文件日期 poHeaderxStructure.setValue("DOC_TYPE", "X"); //采购文件类型 poHeaderxStructure.setValue("COMP_CODE", "X"); //公司代码 poHeaderxStructure.setValue("PURCH_ORG", "X"); //采购组织 poHeaderxStructure.setValue("PUR_GROUP", "X"); //采购群组 poHeaderxStructure.setValue("VENDOR", "X"); //供应商号码 poHeaderxStructure.setValue("LANGU", "X"); //语言代码 poHeaderxStructure.setValue("CURRENCY", "X"); //币别 //表格 JCoTable poItemTabe = function.getTableParameterList().getTable("POITEM"); poItemTabe.appendRow(); poItemTabe.setValue("PO_ITEM", "10"); //采购单项次 poItemTabe.setValue("MATERIAL", "CTWP0296-XB"); //物料 poItemTabe.setValue("MATERIAL_EXTERNAL", "CTWP0296-XB"); //长物料 poItemTabe.setValue("INFO_REC", "5300000832"); //采购资讯记录 poItemTabe.setValue("STGE_LOC", "2172"); //收货储存地点 poItemTabe.setValue("PLANT", "2000"); //工厂 poItemTabe.setValue("QUANTITY", "1000"); //采购数量 poItemTabe.setValue("NET_PRICE", "24.07"); //净价 poItemTabe.setValue("PRICE_UNIT", "100"); //每 JCoTable poItemxTabe = function.getTableParameterList().getTable("POITEMX"); poItemxTabe.appendRow(); poItemxTabe.setValue("PO_ITEM", "10"); //采购单项次 poItemxTabe.setValue("MATERIAL", "X"); //物料 poItemxTabe.setValue("MATERIAL_EXTERNAL", "X"); //长物料 poItemxTabe.setValue("INFO_REC", "X"); //采购资讯记录 poItemxTabe.setValue("STGE_LOC", "X"); //收货储存地点 poItemxTabe.setValue("PLANT", "X"); //工厂 poItemxTabe.setValue("QUANTITY", "X"); //采购数量 poItemxTabe.setValue("NET_PRICE", "X"); //净价 poItemxTabe.setValue("PRICE_UNIT", "X"); //每 //执行函数 JCoContext.begin(destination); function.execute(destination); //Export //结构 //JCoStructure returnStructure = function.getExportParameterList().getStructure("RETURN"); //if (returnStructure.getString("TYPE").equals("E")){ //System.out.print(returnStructure.getString("MESSAGE")); //} //表 JCoTable returnTable = function.getTableParameterList().getTable("RETURN"); for(int i=0;i<returnTable.getNumRows();i++) { returnTable.setRow(i); if (returnTable.getString("TYPE").equals("S")) { JCoFunction commitFunction = destination.getRepository().getFunction("BAPI_TRANSACTION_COMMIT"); commitFunction.execute(destination); //参数 System.out.print("采购单:"+function.getExportParameterList().getValue("EXPPURCHASEORDER")); break; }else { System.out.print(returnTable.getString("TYPE")+" "); System.out.print(returnTable.getString("ID")+" "); System.out.print(returnTable.getString("NUMBER")+" "); System.out.print(returnTable.getString("MESSAGE")+" "); System.out.println(); JCoFunction rollbackFunction = destination.getRepository().getFunction("BAPI_TRANSACTION_ROLLBACK"); rollbackFunction.execute(destination); } } JCoContext.end(destination); } }