使用SAP JCo从SAP ECC读取表格

huangapple go评论73阅读模式
英文:

Reading Table from SAP ECC using SAP JCo

问题

以下是要翻译的内容:

我想要打印所有运输请求的详细信息,我正在尝试使用SAP JCo和RFC_READ_TABLE从SAP ECC中的表格E070读取数据,我运行了以下代码但没有输出。

以下是代码:

import com.sap.conn.jco.*;
import java.util.logging.*;

public class Transport {
    private static final Logger logger = Logger.getLogger(Transport.class.getName());

    public static void main(String[] args) {
        // 配置日志记录器
        LogManager.getLogManager().reset();
        logger.setLevel(Level.ALL);
        ConsoleHandler consoleHandler = new ConsoleHandler();
        consoleHandler.setLevel(Level.ALL);
        logger.addHandler(consoleHandler);

        // 建立连接
        JCoDestination destination;
        try {
            destination = JCoDestinationManager.getDestination("SAP-ECC-Dest");
            destination.ping();
        } catch (JCoException e) {
            logger.log(Level.SEVERE, "连接错误", e);
            return;
        }

        // 创建函数模块调用
        JCoFunction function;
        try {
            function = destination.getRepository().getFunction("RFC_READ_TABLE");
        } catch (JCoException e) {
            logger.log(Level.SEVERE, "函数模块错误", e);
            return;
        }

        // 设置函数模块参数
        JCoParameterList imports = function.getImportParameterList();
        imports.setValue("QUERY_TABLE", "E070"); // 运输请求的表名
        imports.setValue("DELIMITER", "|"); // 设置结果的分隔符

        JCoParameterList tableOptions = function.getTableParameterList();
        JCoTable data = tableOptions.getTable("DATA");
        // System.out.println(data);
        // 如果需要,添加筛选条件
        // imports.setValue("OPTIONS", ...);

        // 执行函数模块调用
        try {
            function.execute(destination);
        } catch (JCoException e) {
            logger.log(Level.SEVERE, "函数模块执行错误", e);
            return;
        }

        // 处理结果
        String[] rows = data.getString().split("\\|");
        for (String row : rows) {
            String[] columns = row.split("\\|");
            if (columns.length >= 3) {
                String transportRequestId = columns[0]; // 请求ID列
                String description = columns[2]; // 描述列

                logger.info("运输请求ID:" + transportRequestId);
                logger.info("描述:" + description);
                logger.info("-----------------------------------");
            }
        }
    }
}

我已经尝试调试它,但没有效果。谢谢 使用SAP JCo从SAP ECC读取表格

也许有更多经验的人可以告诉我应该如何配置这个示例代码以使其工作。

英文:

I want to print details of all Transport requests, I am trying to read data in table E070 from SAP ECC using SAP JCo and RFC_READ_TABLE, I am running the following code but I am getting no output.

Here is the code:

import com.sap.conn.jco.*;
import java.util.logging.*;
public class Transport {
private static final Logger logger = Logger.getLogger(Transport.class.getName());
public static void main(String[] args) {
// Configure logger
LogManager.getLogManager().reset();
logger.setLevel(Level.ALL);
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.ALL);
logger.addHandler(consoleHandler);
// Establish connection
JCoDestination destination;
try {
destination = JCoDestinationManager.getDestination("SAP-ECC-Dest");
destination.ping();
} catch (JCoException e) {
logger.log(Level.SEVERE, "Connection error", e);
return;
}
// Create function module call
JCoFunction function;
try {
function = destination.getRepository().getFunction("RFC_READ_TABLE");
} catch (JCoException e) {
logger.log(Level.SEVERE, "Function module error", e);
return;
}
// Set up function module parameters
JCoParameterList imports = function.getImportParameterList();
imports.setValue("QUERY_TABLE", "E070"); // Table name for transport requests
imports.setValue("DELIMITER", "|"); // Set delimiter for the result
JCoParameterList tableOptions = function.getTableParameterList();
JCoTable data = tableOptions.getTable("DATA");
// System.out.println(data);
// Add filters if required
// imports.setValue("OPTIONS", ...);
// Execute the function module call
try {
function.execute(destination);
} catch (JCoException e) {
logger.log(Level.SEVERE, "Function module execution error", e);
return;
}
// Process the results
String[] rows = data.getString().split("\\|");
for (String row : rows) {
String[] columns = row.split("\\|");
if (columns.length >= 3) {
String transportRequestId = columns[0]; // Request ID column
String description = columns[2]; // Description column
logger.info("Transport Request ID: " + transportRequestId);
logger.info("Description: " + description);
logger.info("-----------------------------------");
}
}
}
}

I have tried debugging it but nothing worked. Thank you 使用SAP JCo从SAP ECC读取表格

Maybe someone with more experience can tell me how should I configure this sample code in order to work.

答案1

得分: 0

以下代码对我有效:

import com.sap.conn.jco.*;

public class TransportRequestDetailsExamples {
    public static void main(String[] args) {
        try {
            JCoDestination destination = JCoDestinationManager.getDestination("SAP-ECC-Dest");
            destination.ping();
            JCoFunction function = destination.getRepository().getFunction("RFC_READ_TABLE");
            JCoParameterList imports = function.getImportParameterList();
            imports.setValue("QUERY_TABLE", "E070");
            imports.setValue("DELIMITER", ";");
            JCoParameterList tableOptions = function.getTableParameterList();
            JCoTable data = tableOptions.getTable("DATA");
            function.execute(destination);
            System.out.println(data);
        } catch (JCoException e) {
            e.printStackTrace();
        }
    }
}
英文:

following code worked for me:

import com.sap.conn.jco.*;                                                                                                                                                                                                                                                                                                                              
public class TransportRequestDetailsExamples {                                                                                                                                  
public static void main(String[] args) {                                                                                                                                        
try {                                                                                                                                                                                                                                                                                                                                
JCoDestination destination = JCoDestinationManager.getDestination("SAP-ECC-Dest");                                                                                          
destination.ping();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
JCoFunction function= destination.getRepository().getFunction("RFC_READ_TABLE");                                                                                                                                                                                                                                                                                                                                                                                                              
JCoParameterList imports = function.getImportParameterList();                                                                                                               
imports.setValue("QUERY_TABLE", "E070");                                                                                                                                    
imports.setValue("DELIMITER", ";");                                                                                                                                                                                                                                                                                                                     
JCoParameterList tableOptions = function.getTableParameterList();                                                                                                           
JCoTable data = tableOptions.getTable("DATA");                                                                                                                                                                                                                                                                                                           
function.execute(destination);                                                                                                                                                                                                                                                                                                                          
System.out.println(data);                                                                                                                                                                                                                                                                                           
} catch (JCoException e) {                                                                                                                                                      
e.printStackTrace();                                                                                                                                                    
}                                                                                                                                                                       
}                                                                                                                                                                       
}  

huangapple
  • 本文由 发表于 2023年6月19日 19:03:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定