一段使用Java程序和JCO(Java connector)连接SAP ABAP Netweaver系统的代码

简介: 一段使用Java程序和JCO(Java connector)连接SAP ABAP Netweaver系统的代码
package jco;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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;
/**
 * basic examples for Java to ABAP communication  
 * See help: https://help.sap.com/saphelp_nwpi711/helpdata/en/48/70792c872c1b5ae10000000a42189c/frameset.htm
 */
public class StepByStepClient
{
  static String DESTINATION_NAME = "ABAP_AS_WITHOUT_POOL";
  static public final String ABAP_DURATION = "abapLayerDuration";
  static public final String UPSELL_PRODUCT = "upsellProducts";
  static public final String PRODUCT_ID = "productID";
  static public final String PRODUCT_TEXT = "productText";
  static private final String FILE_ID = "fileID";
  static private final String FILE_OWNER = "fileOwner";
  static private final String FILE_CDATE = "fileCreationDate";
  static private final String FILE_NAME = "fileName";
    @SuppressWarnings("unused")
  static private Properties prepareProperty(){
        Properties connectProperties = new Properties();
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "ldcixcd.jerry");
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "00");
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "111");
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "WANGJER");
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "pleaseInputYourPassword");
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "en");
        createDestinationDataFile(DESTINATION_NAME, connectProperties);
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,    "10");
        createDestinationDataFile(DESTINATION_NAME, connectProperties);
        return connectProperties;
    }
    static private void getProductImageTest(){
      JCoDestination destination = null;
    try {
      destination = JCoDestinationManager.getDestination(DESTINATION_NAME);
      JCoRepository repo = destination.getRepository();
        JCoFunction stfcConnection = repo.getFunction("ZDIS_GET_MATERIAL_IMAGES");
        JCoParameterList imports = stfcConnection.getImportParameterList();
        String materialID = "16";
        imports.setValue("IV_MATERIAL_ID", materialID);
        stfcConnection.execute(destination);
        JCoParameterList exports = stfcConnection.getExportParameterList();
          int abapDuration = exports.getInt("EV_DURATION");
          StringBuilder sb = new StringBuilder();
          sb.append("{ \"" + ABAP_DURATION + "\": " + abapDuration + ",");
          sb.append("\"" + UPSELL_PRODUCT + "\":[");
          JCoTable codes = exports.getTable("ET_IMAGES");
          int row = codes.getNumRows();
          System.out.println("Total rows: " + row);
          System.out.println("ABAP duration: " + abapDuration);
          for( int i = 0; i < row; i++){
            codes.setRow(i);
                sb.append("{\"" + FILE_ID + "\":" + codes.getString("FILEID") + ","
                    + "\"" + FILE_OWNER + "\":\"" + codes.getString("OWNER") + "\"" + ",");
                sb.append("{\"" + FILE_CDATE + "\":" + codes.getString("CREATION_DATE") + ","
                    + "\"" + FILE_NAME + "\":\"" + codes.getString("FILENAME") + "\""); 
                storeLocalFile(codes);
                if( i < row - 1){
                  sb.append("},");
                }
                else{
                  sb.append("}");
                }
          }
          sb.append("]}");
          System.out.println("Final json: " + sb.toString());
    } catch (JCoException e) {
      e.printStackTrace();
    }
    }
    static private void storeLocalFile(JCoTable codes){
      InputStream is = codes.getBinaryStream("FILECONTENT");
      try {
        File file = new File("c:\\temp\\" + codes.getString("FILENAME"));
        byte[] bytes = new byte[is.available()];
        is.read(bytes);
        /*String asB64 = Base64.getEncoder().encodeToString(bytes);
        byte[] asBytes = Base64.getDecoder().decode(asB64);*/
      OutputStream output = new FileOutputStream(file);
      BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
      bufferedOutput.write(bytes);
      bufferedOutput.close();
      is.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
     }
    @SuppressWarnings("unused")
  static private void getUpsellProductTest(){
      JCoDestination destination = null;
    try {
      destination = JCoDestinationManager.getDestination(DESTINATION_NAME);
      JCoRepository repo = destination.getRepository();
        JCoFunction stfcConnection = repo.getFunction("ZDIS_GET_UPSELL_MATERIALS");
        JCoParameterList imports = stfcConnection.getImportParameterList();
        String customerID = "1000040";
        String materialID = "11";
        imports.setValue("IV_CUSTOMER_ID", customerID);
        imports.setValue("IV_MATERIAL_ID", materialID);
        stfcConnection.execute(destination);
        JCoParameterList exports = stfcConnection.getExportParameterList();
        // int result = exports.getInt("EV_RESULT");
          int abapDuration = exports.getInt("EV_DURATION");
          StringBuilder sb = new StringBuilder();
          sb.append("{ \"" + ABAP_DURATION + "\": " + abapDuration + ",");
          sb.append("\"" + UPSELL_PRODUCT + "\":[");
          JCoTable codes = exports.getTable("ET_MATERIALS");
          int row = codes.getNumRows();
          System.out.println("Total rows: " + row);
          System.out.println("ABAP duration: " + abapDuration);
          for( int i = 0; i < row; i++){
            codes.setRow(i);
                System.out.println(codes.getString("MATERIAL_ID") + '\t' + codes.getString("MATERIAL_TEXT"));
                sb.append("{\"" + PRODUCT_ID + "\":" + codes.getString("MATERIAL_ID") + ","
                    + "\"" + PRODUCT_TEXT + "\":\"" + codes.getString("MATERIAL_TEXT") + "\"");
                if( i < row - 1){
                  sb.append("},");
                }
                else{
                  sb.append("}");
                }
          }
          sb.append("]}");
          System.out.println("Final json: " + sb.toString());
    } catch (JCoException e) {
      e.printStackTrace();
    }
    }
    static public void main(String[] arg) {
      getProductImageTest();
    }
    static private void createDestinationDataFile(String destinationName, Properties connectProperties)
    {
        File destCfg = new File(destinationName+".jcoDestination");
        try
        {
            FileOutputStream fos = new FileOutputStream(destCfg, false);
            connectProperties.store(fos, "for tests only !");
            fos.close();
        }
        catch (Exception e)
        {
            throw new RuntimeException("Unable to create the destination files", e);
        }
    }
}


相关文章
|
10天前
|
监控 安全 Java
Java与物联网:连接与控制设备
Java与物联网:连接与控制设备
39 0
|
10天前
|
前端开发 Java 应用服务中间件
【异常解决】java程序连接MinIO报错The request signature we calculated does not match the signature you provided.
【异常解决】java程序连接MinIO报错The request signature we calculated does not match the signature you provided.
36 0
|
10天前
|
SQL Java 关系型数据库
【JAVA基础篇教学】第十六篇:Java连接和操作MySQL数据库
【JAVA基础篇教学】第十六篇:Java连接和操作MySQL数据库
|
10天前
|
SQL Java 数据库连接
Java数据库编程实践:连接与操作数据库
Java数据库编程实践:连接与操作数据库
14 0
|
10天前
|
NoSQL Java 关系型数据库
Java基础教程(21)-Java连接MongoDB
【4月更文挑战第21天】MongoDB是开源的NoSQL数据库,强调高性能和灵活性。Java应用通过MongoDB Java驱动与之交互,涉及MongoClient、MongoDatabase、MongoCollection和Document等组件。连接MongoDB的步骤包括:配置连接字符串、创建MongoClient、选择数据库和集合。伪代码示例展示了如何建立连接、插入和查询数据。
|
10天前
|
Java 关系型数据库 MySQL
Java基础教程(20)-Java连接mysql数据库CURD
【4月更文挑战第19天】MySQL是流行的关系型数据库管理系统,支持SQL语法。在IDEA中加载jar包到项目类路径:右击项目,选择“Open Module Settings”,添加库文件。使用JDBC连接MySQL,首先下载JDBC驱动,然后通过`Class.forName()`加载驱动,`DriverManager.getConnection()`建立连接。执行CRUD操作,例如创建表、插入数据和查询,使用`Statement`或`PreparedStatement`,并确保正确关闭数据库资源。
|
10天前
|
前端开发 NoSQL JavaScript
java域控连接AD遇到的问题
java域控连接AD遇到的问题
|
11月前
|
SQL 关系型数据库 MySQL
Java连接MySQL改成连接SQL Server数据库
Java连接MySQL改成连接SQL Server数据库
140 0
|
SQL Java 数据库连接
JAVA中jdbc连接数据库 sql注入与PrepareStatement预处理
JAVA中jdbc连接数据库 sql注入与PrepareStatement预处理
169 0
|
SQL 数据库 Android开发
java168-java连接SQL server数据库
java168-java连接SQL server数据库
109 0
java168-java连接SQL server数据库