JAVA通过JCO连接SAP例子

来源:互联网 发布:淘宝充话费始终不到账 编辑:程序博客网 时间:2024/05/17 06:17

1.把librfc32.dll,sapjcorfc.dll 放到服务器的系统的c:\windows\system32目錄下

   (不然会报错:sap Field IT_TABLnot a member of TABLES)

2.下载 sap.jar;sapjco.jar;sappool.jar加到JAVA应用下.
3.下面类代码可以查物料数据
 
import com.sap.mw.jco.*;
import com.sap.mw.jco.JCO.Function;
class SAPServer extends JCO.Server
{
 
    public SAPServer(JCO.Repository repo)
    {
        super ("10.10.10.50", "sapgw00", "ABC", repo);
    }    
}
class SAPLogon
{
 public JCO.Client mConnection;
 public JCO.Repository mRepository;
 public SAPLogon(String client, String userid, String password, String language,
            String ip, String system_number)
 {
  try {
  
         mConnection = JCO.createClient(client, 
                                       userid, 
                                       password, 
                                       language, 
                                       ip, 
                                       system_number);
   mConnection.connect();
   mRepository = new JCO.Repository("Lee", mConnection);
   System.out.println("SAP连接成功");
 //调用RFC函数
 IFunctionTemplate ft = mRepository.getFunctionTemplate("BAPI_MATERIAL_GET_DETAIL");
 JCO.Function f = ft.getFunction();
 
 //1.传入参数为Field
    f.getImportParameterList().setValue("931029BA", "MATERIAL"); 
    f.getImportParameterList().setValue("1000", "PLANT");    
    mConnection.execute(f);
    //1.返回参数为Field
    JCO.Structure struct  = f.getExportParameterList().getStructure("MATERIAL_GENERAL_DATA");
    String name = struct.getString("MATL_DESC");
 System.out.println("物料名:"+name);
    String MATL_TYPE = struct.getString("MATL_TYPE");
    System.out.println("物料类型:"+MATL_TYPE);
    
   
    mRepository = new JCO.Repository("my_repository", mConnection);
    mConnection.disconnect();
  } catch (Exception ex) {
   ex.printStackTrace();
   System.exit(1);
   
  }
  
}
 
}
public class Main {
    
    public static void main(String[] args) {
        SAPLogon mySAP = new SAPLogon("800", "userid", "passwork", "zh", "21.4.13.2", "00");
        SAPServer myServer = new SAPServer(mySAP.mRepository);       
        myServer.start();
    }
}