java 连接 sap

 

源代码:

 import com.sap.mw.jco.*;
public class control extends Object
{
public static void main (String str[])
{
 control control=new control();
}
int count;
JCO.Client mConnection;
JCO.Repository mRepository;
String[] SAPInterfaces;

public control()
{
   try {
    // Logon info
     mConnection = JCO.createClient("110", // SAP client
                                     "", // userid
                                       "", // password
                                          "zh", // language
                                 "", // application server host name
                                            ""); // system number
    mConnection.connect();
    mRepository = new JCO.Repository("ARAsoft", mConnection);
    System.out.println("SAP连接成功");
   } catch (Exception ex) {
    ex.printStackTrace();
    System.exit(1);
   }
   JCO.Function function = null;
   JCO.Table codes = null;
   JCO.Table DATA=null;
 
   try
   {
    function=this.createFunction("BAPI_PO_GETDETAIL");
    if (function == null)
    {
     System.out.println(
      "BAPI_MATERIAL_GETLIST" + " not found in SAP.");
     System.exit(1);
    }
   // codes = function.getTableParameterList().getTable("FIELDS");
    DATA =function.getTableParameterList().getTable("PO_ITEMS");
    JCO.ParameterList input=function.getImportParameterList();
    input.setValue("4500000388", "PURCHASEORDER");
    input.setValue("x","ITEMS");
    mConnection.execute(function);
    System.out.print("PO_ITEMS个数:");
    System.out.println(Integer.toString(DATA.getNumRows()));
    System.out.println("调用函数为 :BAPI_PO_GETDETAIL 输入参数为:4500000388, PURCHASEORDER");
    System.out.println("输出如下:");
    for (int i = 0; i < DATA.getNumRows();i++)
    {
     DATA.setRow(i);
     System.out.print("采购凭证号");
     System.out.println(DATA.getString("PO_NUMBER"));
     System.out.print("采购凭证的项目编号");
     System.out.println(DATA.getString("PO_ITEM"));
     System.out.print("工厂");
     System.out.println(DATA.getString("PLANT"));
    
     System.out.println("***********");
    }
   }
   catch (Exception ex)
   {
    ex.printStackTrace();
    System.exit(1);
   }
}
public JCO.Function createFunction(String name) throws Exception
{
   try {
    IFunctionTemplate ft =
     mRepository.getFunctionTemplate(name.toUpperCase());
    if (ft == null)
     return null;
    return ft.getFunction();
   } catch (Exception ex)
   {
    throw new Exception("Problem retrieving JCO.Function object.");
   }
}
}

 

输出:

SAP连接成功
PO_ITEMS个数:2
调用函数为 :BAPI_PO_GETDETAIL 输入参数为:4500000388, PURCHASEORDER
输出如下:
采购凭证号4500000388
采购凭证的项目编号00010
工厂L4E3
***********
采购凭证号4500000388
采购凭证的项目编号00020
工厂L4E3
***********

你可能感兴趣的:(java 连接 sap)