用eclipse時,先導入sapjco3.jar去。
如果沒有sapjco3包,可以 下载,32位64位都有,并附有详细作说明。
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("要傳的參數", "參數值");
JCoStructure structure = input.getStructure("STRUTURENAME");//傳結構體類的參數
structure.setValue("field1","value1");
structure.setValue("field2", "value2");
JCoTable tb_list = function.getTableParameterList().getTable("傳表");
tb_list.appendRow();
tb_list.setValue("表中列名","第一行的值");
function.execute(destination);
JCoParameterList exportParams = function.getExportParameterList();//獲取返回的參數值
String msg = exportParams.getString("參數1");
JCoStructure returnStructure = exportParams.getStructure("STRUCTURE");//獲取返回的結構體的值
String str1 = returnStructure.getString("字段1");
String str2 = returnStructure.getString("字段2");
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");
如果传入参数为深层内表:
JCoDestination destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
JCoRepository repository = destination.getRepository();
JCoFunction function = repository.getFunction(RFCName);
JCoParameterList input = function.getImportParameterList();
if (input != null){
JCoParameterFieldIterator iterator = input.getParameterFieldIterator();
while (iterator.hasNextField()) {
JCoParameterField field = iterator.nextParameterField();
JCoField tableField = iterator.nextField();
try{//判断类型,创建对应的方法传入参数
if (field.isTable()){
fillSAPTable(field);
}if (field.isStructure()){
// fillSAPStructure(field);
}else{
input.setValue("FIELD1", "123");
}
} catch (Exception e){
throw new IOException("Function " + function.getName() + " Error >>" + e.getMessage());
}
}
}