Connect to SAP using sapjco in Java.
Environment
- OS: Windows10
- Java version: 1.8
- Library: sapjco3
Import libs
Reference files: saro-lab/sap-jco-manager on Github
Sapjco.jar can also be find in this site: JAR download
-
Sapjco3.dll is needed to put in "C:/windows/system32/" or "C:/windows/SysWOW64/". (Depends on your running environment.)
-
Import the sapjco3.jar into your project.
Codes
Devided into 3 parts:
- SAP connection configuration class.
- Functions to connect to SAP.
- RFC.
SAP connection configuration class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
package com.sap; public class SapConn { // SAP server private String JCO_ASHOST; // SAP system number private String JCO_SYSNR; // SAP client private String JCO_CLIENT; // SAP user name private String JCO_USER; // SAP user password private String JCO_PASSWD; // SAP language private String JCO_LANG; // MAX connection private String JCO_POOL_CAPACITY; // MAX thread private String JCO_PEAK_LIMIT; // SAP ROUTER private String JCO_SAPROUTER; public SapConn(String JCO_ASHOST, String JCO_SYSNR, String JCO_CLIENT, String JCO_USER, String JCO_PASSWD, String JCO_LANG, String JCO_POOL_CAPACITY, String JCO_PEAK_LIMIT, String JCO_SAPROUTER) { this.JCO_ASHOST = JCO_ASHOST; this.JCO_SYSNR = JCO_SYSNR; this.JCO_CLIENT = JCO_CLIENT; this.JCO_USER = JCO_USER; this.JCO_PASSWD = JCO_PASSWD; this.JCO_LANG = JCO_LANG; this.JCO_POOL_CAPACITY = JCO_POOL_CAPACITY; this.JCO_PEAK_LIMIT = JCO_PEAK_LIMIT; this.JCO_SAPROUTER = JCO_SAPROUTER; } public SapConn(){} public String getJCO_ASHOST() { return JCO_ASHOST; } public void setJCO_ASHOST(String JCO_ASHOST) { this.JCO_ASHOST = JCO_ASHOST; } public String getJCO_SYSNR() { return JCO_SYSNR; } public void setJCO_SYSNR(String JCO_SYSNR) { this.JCO_SYSNR = JCO_SYSNR; } public String getJCO_CLIENT() { return JCO_CLIENT; } public void setJCO_CLIENT(String JCO_CLIENT) { this.JCO_CLIENT = JCO_CLIENT; } public String getJCO_USER() { return JCO_USER; } public void setJCO_USER(String JCO_USER) { this.JCO_USER = JCO_USER; } public String getJCO_PASSWD() { return JCO_PASSWD; } public void setJCO_PASSWD(String JCO_PASSWD) { this.JCO_PASSWD = JCO_PASSWD; } public String getJCO_LANG() { return JCO_LANG; } public void setJCO_LANG(String JCO_LANG) { this.JCO_LANG = JCO_LANG; } public String getJCO_POOL_CAPACITY() { return JCO_POOL_CAPACITY; } public void setJCO_POOL_CAPACITY(String JCO_POOL_CAPACITY) { this.JCO_POOL_CAPACITY = JCO_POOL_CAPACITY; } public String getJCO_PEAK_LIMIT() { return JCO_PEAK_LIMIT; } public void setJCO_PEAK_LIMIT(String JCO_PEAK_LIMIT) { this.JCO_PEAK_LIMIT = JCO_PEAK_LIMIT; } public String getJCO_SAPROUTER() { return JCO_SAPROUTER; } public void setJCO_SAPROUTER(String JCO_SAPROUTER) { this.JCO_SAPROUTER = JCO_SAPROUTER; } @Override public String toString() { return "SapConn{" + "JCO_ASHOST='" + JCO_ASHOST + '\'' + ", JCO_SYSNR='" + JCO_SYSNR + '\'' + ", JCO_CLIENT='" + JCO_CLIENT + '\'' + ", JCO_USER='" + JCO_USER + '\'' + ", JCO_PASSWD='" + JCO_PASSWD + '\'' + ", JCO_LANG='" + JCO_LANG + '\'' + ", JCO_POOL_CAPACITY='" + JCO_POOL_CAPACITY + '\'' + ", JCO_PEAK_LIMIT='" + JCO_PEAK_LIMIT + '\'' + ", JCO_SAPROUTER='" + JCO_SAPROUTER + '\'' + '}'; } } |
Functions to connect to SAP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
package com.sap; import java.io.File; import java.io.FileOutputStream; import java.util.Properties; import com.sap.conn.jco.*; import com.sap.conn.jco.ext.DestinationDataProvider; public class SAPConnUtils { private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL"; /** * Establish SAP interface * @param name ABAP name * @param suffix file suffix * @param properties file content */ private static void createDataFile(String name, String suffix, Properties properties){ File cfg = new File(name+"."+suffix); if(cfg.exists()){ cfg.deleteOnExit(); } try{ FileOutputStream fos = new FileOutputStream(cfg, false); properties.store(fos, "for tests only !"); fos.close(); }catch (Exception e){ System.out.println("Create Data file fault, error msg: " + e.toString()); throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e); } } /** * Init SAP connection */ private static void initProperties(SapConn sapConn) { Properties connectProperties = new Properties(); // SAP server location connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, sapConn.getJCO_ASHOST()); // SAP system number connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, sapConn.getJCO_SYSNR()); // SAP client connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, sapConn.getJCO_CLIENT()); // SAP user ID connectProperties.setProperty(DestinationDataProvider.JCO_USER, sapConn.getJCO_USER()); // SAP user PW connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, sapConn.getJCO_PASSWD()); // SAP language connectProperties.setProperty(DestinationDataProvider.JCO_LANG, sapConn.getJCO_LANG()); // MAX connection connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, sapConn.getJCO_POOL_CAPACITY()); // MAX connection threads connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, sapConn.getJCO_PEAK_LIMIT()); // SAP ROUTER connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER, sapConn.getJCO_SAPROUTER()); createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties); } /** * Get SAP connection * @return SAP connection object */ public static JCoDestination connect(SapConn sapConn){ System.out.println("Connecting to SAP..."); JCoDestination destination = null; initProperties(sapConn); try { destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED); destination.ping(); System.out.println("Connection estiblished."); } catch (JCoException e) { System.out.println("Connect SAP fault, error msg: " + e.toString()); } return destination; } } |
RFC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import java.util.ArrayList; import com.sap.conn.jco.JCoDestination; import com.sap.conn.jco.JCoFunction; import com.sap.conn.jco.JCoTable; public class RfcFunctions { public ArrayList<LT_RETURNData> sendData(JCoDestination des, ArrayList<LT_DATA1Data> dataList){ LT_DATA1Data LT_DATA1 = null; ArrayList<LT_RETURNData> arrayReturnData = new ArrayList<LT_RETURNData>(); JCoFunction function = null; try{ function = des.getRepository().getFunctionTemplate("SAP_RFC").getFunction(); if (null == function) { throw new RuntimeException("get function not found in sap"); } else { JCoTable INPUT_TABLE = function.getTableParameterList().getTable("TABLE_INPUT_NAME"); JCoTable OUTPUT_TABLE = null; for(int i=0;i < dataList.size();i++){ INPUT_TABLE.appendRow(); // Build your RFC request table input LT_DATA1 = (LT_DATA1Data) dataList.get(i); INPUT_TABLE.setValue("TEST1", "VAL01"); INPUT_TABLE.setValue("DATA1", "DATA1"); INPUT_TABLE.setValue("DATA2", "DATA2"); INPUT_TABLE.setValue("DATA3", "DATA3"); INPUT_TABLE.setValue("DATA4", "DATA4"); } function.execute(des); OUTPUT_TABLE = function.getTableParameterList().getTable("TABLE_RETURN_NAME"); System.out.println(OUTPUT_TABLE.toString()); if(OUTPUT_TABLE.getNumRows() != 0){ for (int i = 0; i < OUTPUT_TABLE.getNumRows(); i++) { // Get return table OUTPUT_TABLE.setRow(i); // Parse return table here. // arrayReturnData.add(LT_RETURNData); } }else{} } }catch(Exception e){ System.err.println(e.toString()); } return arrayReturnData; } } |
Send data to SAP Using RFC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.ArrayList; import java.util.Iterator; import com.sap.conn.jco.JCoDestination; public class sendDatatoSAP { public String doProcess(String JCO_HOST, String JCO_CLIENT, String JCO_USER, String JCO_PASSWD)throws Exception { JCoDestination destination = null; String returnStr = ""; // Configuration of SAP connection SapConn con = new SapConn(JCO_HOST, "00", JCO_CLIENT, JCO_USER, JCO_PASSWD, "ZH", "", "", ""); destination = SAPConnUtils.connect(con); RfcFunctions RfcFunctions = new RfcFunctions(); ArrayList<LT_DATA1Data> dataList = new ArrayList<LT_DATA1Data>(); ArrayList<LT_RETURNData> LT_RETURN = new ArrayList<LT_RETURNData>(); dataList.add(new LT_DATA1Data("TEST1","Data1","Data2","Data3","Data4")); LT_RETURN = RfcFunctions.sendData(destination, dataList); Iterator<LT_RETURNData> iterator = LT_RETURN.iterator(); if(iterator.hasNext()){ while(iterator.hasNext()){ returnStr += iterator.next(); returnStr += "\n"; } }else{ returnStr = "Success!"; } return returnStr; |
Above, ArrayList
留言