欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java连接SAP实例

程序员文章站 2022-07-10 18:30:16
用eclipse時,先導入sapjco3.jar進去。...

用eclipse時,先導入sapjco3.jar進去。

package testsap; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; 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.JCoFunction; import com.sap.conn.jco.JCoParameterList; import com.sap.conn.jco.JCoRepository; import com.sap.conn.jco.JCoTable; import com.sap.conn.jco.ext.DestinationDataProvider; public class test { static String ABAP_AS_POOLED = "ABAP_AS_POOL"; static { Properties connectProperties = new Properties(); connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST,"服務器Ip"); connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "連接的Client"); connectProperties.setProperty(DestinationDataProvider.JCO_USER, "Username"); connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "Password"); connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "事例號碼"); connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "語言"); connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties); } static void createDataFile(String name, String suffix, Properties properties) { File cfg = new File(name + "." + suffix); if (!cfg.exists()) { try { FileOutputStream fos = new FileOutputStream(cfg, false); properties.store(fos, "for tests only !"); fos.close(); } catch (Exception e) { e.printStackTrace(); } } } private void writeArrayToTxt(JCoTable demands, String string) { try { FileWriter fw = new FileWriter(string); for (int i = 0; i < 1; i++) { for (int j = 0; j < 1; j++) fw.write(demands+ "\t"); fw.write("\n"); } fw.close(); } catch (IOException e){ e.printStackTrace(); } } public static void RFC() throws JCoException { String RFCName = "調用的RFC"; JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED); JCoRepository repository = destination.getRepository(); JCoFunction function = repository.getFunction(RFCName); JCoParameterList input = function.getImportParameterList(); input.setValue("要傳的參數", "參數值"); JCoTable tb_list = function.getTableParameterList().getTable("傳表"); tb_list.appendRow(); tb_list.setValue("表中列名","第一行的值"); function.execute(destination); JCoTable codes = function.getTableParameterList().getTable("返回的輸出表"); //控制台輸出返回內容 for (int i = 0; i < codes.getNumRows(); i++) { codes.setRow(i); System.out.println(codes.getString("輸出表的列名") '\t' + codes.getString("輸出表的列名")); } //保存為txt文檔 test wa = new test(); wa.writeArrayToTxt(codes, "my.txt"); } public static void main(String[] args) throws JCoException { RFC(); } } 

上面的是單個服務器連接,群組服務器連接,換一下參數就行,下面是群組連接參數:

connectProperties.setProperty(DestinationDataProvider.JCO_R3NAME,"系統ID"); connectProperties.setProperty(DestinationDataProvider.JCO_GROUP,"群組名"); connectProperties.setProperty(DestinationDataProvider.JCO_MSHOST,"服務器IP"); connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT,"client"); connectProperties.setProperty(DestinationDataProvider.JCO_USER,"username"); connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD,"password"); connectProperties.setProperty(DestinationDataProvider.JCO_LANG,"EN"); connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,"10"); connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); 

本文地址:https://blog.csdn.net/qq_44861248/article/details/107874426

相关标签: 后端 sap java