`
zjut_xiongfeng
  • 浏览: 287305 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

SAP Java Connector - Example 2: CompanyCode_GetList

阅读更多
if (window.name != "content") document.write("

Click here to show toolbars of the Web Online Help System: show toolbars

"); <!-- !chm2web! -->

SAP Java Connector - Example 2: CompanyCode_GetList

Code
How to compile and run the program

Code

This example shows how to use the BAPI CompanyCode_GetList to retrieve a list of company codes from SAP and display them in a listbox..

//-------------------------------------------------------------
// GetCompanycodeList CLASS
//-------------------------------------------------------------

import com.sap.mw.jco.*;  //The JCO

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.border.*;


public class GetCompanycodeList extends JFrame implements ActionListener
{
 private JCO.Client mConnection = null;
 private JTextField returnValues;
 private IRepository mRepository;
 private JCO.Function myFunction;
 private JList companyCodeList;


  public GetCompanycodeList()
  { //------------------------------------------------------------
    //Set size of window and center it on the screen
    //------------------------------------------------------------
    setSizeAndPosition();

    //------------------------------------------------------------
    // Windows listener
    //------------------------------------------------------------
    addWindowListener(new WindowAdapter()
      {  public void windowClosing(WindowEvent e)
         {  if (mConnection != null)
              JCO.releaseClient(mConnection);
            System.exit(0);
         }
      } );

    //------------------------------------------------------------
    // Add Logon and Cancel button
    //------------------------------------------------------------
    Container myContentPane = getContentPane();
    JPanel buttonPanel = new JPanel();
    Border bevel1 = BorderFactory.createBevelBorder(0);
    buttonPanel.setBorder(bevel1);
    myContentPane.add(buttonPanel,BorderLayout.SOUTH);

    JButton logonButton = new JButton("Logon");
    JButton cancelButton = new JButton("Cancel");

    logonButton.setActionCommand("logon_action");
    cancelButton.setActionCommand("cancel_action");

    logonButton.addActionListener(this);
    cancelButton.addActionListener(this);

    buttonPanel.add(logonButton);
    buttonPanel.add(cancelButton);

    //------------------------------------------------------------
    // Add textfield for system messages
    //------------------------------------------------------------
    returnValues = new JTextField(50);
    myContentPane.add(returnValues,BorderLayout.NORTH);

    //------------------------------------------------------------
    // Add a listbox for Company codes
    //------------------------------------------------------------

    companyCodeList = new JList();
    JScrollPane scrollPane = new JScrollPane(companyCodeList );
    myContentPane.add(scrollPane,BorderLayout.CENTER);


  }

//---------------------------------------------------------
// Action listener for push buttons
//---------------------------------------------------------
 public void actionPerformed(ActionEvent evt)
 { Object mySource = evt.getSource();
   String command = evt.getActionCommand();
   if (command == "logon_action")
     ConnectToSap();
   else if  (command == "cancel_action")
     if (mConnection != null)
        JCO.releaseClient(mConnection);
//     System.exit(0);

 }

//------------------------------------------------------------
// Logon and display Company Code list
//------------------------------------------------------------
 private void  ConnectToSap()
 {
    //--------------------------------------------------------
    // Logon to SAP
    //--------------------------------------------------------
    try
    {
      mConnection = JCO.createClient("800",           //SAP client
                                     "WMHEFRN",       //User ID
                                     "SLUPPERT3",     //Password
                                     "EN",            //Language
                                     "172.29.80.207", //Host
                                     "00");           //System


      mConnection.connect();
    }
    catch (Exception ex)
    {
      JOptionPane.showMessageDialog(this,"Error in logon");
      System.exit(0);

    }

    JOptionPane.showMessageDialog(this, "Logon ok");


   //---------------------------------------------------------
   // Create metadata with JCO Repository
   //---------------------------------------------------------
    try
    { mRepository = new JCO.Repository("Henrik",mConnection);
    }
    catch (Exception ex)
    { JOptionPane.showMessageDialog(this,"Error reading meta data");
      System.exit(0);
    }


    //---------------------------------------------------------
    // Get a function template from the repository, create a
    // function from it and and execute the function
    //---------------------------------------------------------

    try
    {
     // Get a function template from the repository
     IFunctionTemplate ftemplate = mRepository.getFunctionTemplate("BAPI_COMPANYCODE_GETLIST");

     // Create a function from the template
      myFunction = new JCO.Function(ftemplate);

     //Execute function
      mConnection.execute(myFunction);

    }
    catch (Exception ex)
    { JOptionPane.showMessageDialog(this,"Error executing function");
      System.exit(0);
    }

//---------------------------------------------------------
// Handle return data
//---------------------------------------------------------

// Check return parameter.
  JCO.Structure vReturn = myFunction.getExportParameterList().getStructure("RETURN");
  if (vReturn.getString("TYPE").equals("")  ||
      vReturn.getString("TYPE").equals("S") )
      returnValues.setText("OK");
  else
    returnValues.setText("Returnvalues: " + vReturn.getString("MESSAGE"));

// Get companycode list
   JCO.Table company_codes = myFunction.getTableParameterList().getTable("COMPANYCODE_LIST");

// Create vector an store Company code data in it
   Vector listElements = new Vector(0,1);
   for (int i = 0; i < company_codes.getNumRows();i++)
   { company_codes.setRow(i);
     listElements.setSize( i + 1);
     listElements.addElement( company_codes.getString("COMP_CODE") + " " +
                              company_codes.getString("COMP_NAME"));

   }

// Use vector as listbox source
   companyCodeList.setListData(listElements);

// Release the client into the pool
   JCO.releaseClient(mConnection);

  JOptionPane.showMessageDialog(this,"Ended without errors");

 }



//---------------------------------------------------------
//Set window properties and center the frame on the screen
//---------------------------------------------------------
  private void setSizeAndPosition()
  {
    setSize(300,400);
    setTitle("Event handling");
    setResizable(false);


    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = this.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);


    //Change look and feel to Windows
      try
        { //Call the static setLookAndFeel method and give it the name of the
          //look and feel you want
          UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
          //Refresh components
          SwingUtilities.updateComponentTreeUI(this);
         }
       catch(Exception e) {}

  }

}

//-----------------------------------------------------------------
// Run CLASS - Runs the application
//-----------------------------------------------------------------

public class Run
{ public static void main(String[] args)
{ GetCompanycodeList GetCompanycodeList1 = new GetCompanycodeList();
GetCompanycodeList1.show();
}
}

How to compile and run the program

Preparations

The application consists of two classes stored in the two files Run.java and and GetCompanycodeList.java. We are using a directory name d:\javatest for the classes.

For our convinience we also copy the SAP/Java connector class and the jRFC12.dll to the same directory, so we easily can include them in the CLASSPATH.

Copy: jCO.jar and jRFC12.dll to d:\javatest

Compiling:

We now compile the two classes. Remember to use the classpath statement

d:\javatest>javac *.java -classpath d:\javatest;d:\javatest\jCO.jar

Running the application from a DOS prompt:

Aalso here remember the classpath (-cp)

d:\javatest>java -cp d:\javatest;d:\javatest\jCO.jar Run

Making a JAR file

Make a manifest file in notepad and name it mainclass. In the manifest write:

Main-Class: Run

Make the JAR file:

jar cvmf mainclass sap.jar *.class jCO.jar

Important: When you compile the java claases a file named GetCompanycodeList$1.class is automatically generated. This file must be included in the JAR file.

Running the JAR file from a DOS prompt:

java -jar sap.jar

分享到:
评论

相关推荐

    eslint-plugin-scissors::police_officer_light_skin_tone:检测长呼叫链嵌套的表达式:scissors:

    夹板剪刀 :police_officer_light_skin_tone:检测长调用链/嵌套表达式 :scissors: :person_raising_hand: 向“ NullPointerError”说再见 :bomb:介绍名为“嵌套表达式”的规则将使then/catch方法中的... getList ( )

    我的最牛代码

    根据提供的标题、描述以及部分内文,我们可以梳理出与ABAP相关的SAP BAPIs(Business Application Programming Interfaces)的关键知识点。 ### SAP中的ABAP BAPIs:财务管理与会计 #### 1. 总账管理(General ...

    PyPI 官网下载 | multidict-5.1.0-cp39-cp39-manylinux2014_s390x.whl

    2. **高效查询**:提供多种方式来访问键对应的值,如`get`, `getlist`, `values`, `items`等方法。 3. **迭代和遍历**:可以方便地遍历所有键或值,甚至按照键值对遍历。 4. **兼容性**:与Python的内置dict保持高度...

    VC-HTTP-Download.rar_9aps download_vc http下载

    2. `Download.clw`:这是VC的类库工作文件,存储了类视图的信息。 3. `HTTPDownload.cpp`:这个文件很可能包含了实现HTTP下载逻辑的C++源代码。 4. `DownloadDlg.cpp`:可能包含了与用户交互的下载对话框的代码。 5....

    Sap Ruby Ajax

    # SAP Ruby on Rails with AJAX: An In-depth Exploration ## Introduction The integration of SAP and Ruby on Rails has sparked considerable interest in the IT community, leading to the development of ...

    Ipmsg协议翻译文档.pdf

    - **IPMSG_GETLIST**:发送主机列表请求。 - **IPMSG_ANSLIST**:响应发送主机列表请求,返回发送主机列表。 - **IPMSG_SENDMSG**:发送消息。 - **IPMSG_RECVMSG**:通知接收者已收到消息。 - **PMSG_READMSG*...

    SAP JCOAPI服务配置与程序编写

    在IT领域,SAP JCO(Java Connector)API是一种用于非SAP系统与SAP系统间集成的关键技术。本文将深入探讨SAP JCOAPI服务的配置与程序编写,包括JCOAPI的基本概念、服务配置步骤、程序设计原理以及RFCAPI和SAP连接器...

    文档(DocService)WebService接口使用说明.doc

    2: LDAP验证) - `ipaddress`: 用户IP地址 - 返回值: 用户登录Session码 - 功能描述: 验证用户身份并返回Session码。 **2. `createDoc` 方法** - 参数: - `docinfo`: 文档信息对象 - `sessioncode`: 登录...

    泛微 ecology9.0 文档(DocService)WebService接口使用说明

    - **getList**:获取文档列表。 - 参数:`sessioncode`。 - 返回值:文档对象数组。 - 功能描述:获取用户有权限访问的所有文档对象数组,但不包含具体内容和附件。 #### 四、接口调用示例 以下是一个简单的...

    支持选举的Java IDL应用

    在"支持选举的Java IDL应用"中,我们主要探讨的是如何利用Java IDL来实现一个选举系统,其中包含了服务器端的方法`getList`和`castVote`。 1. **Java IDL基础**: - Java IDL是Java Remote Method Invocation (RMI...

    安卓版开发说明1.001

    通过`getList()`方法,可以直接从RfidManager获取标签列表。 六、读写器对象模块 读写器对象由四个主要模块组成: - Ack:应答模块,处理设备返回的确认信息。 - Cmd:命令模块,用于发送和接收命令。 - Err:错误...

    .net连接sap的环境

    通过SAP .NET Connector,开发者可以调用特定的BAPI,如`BAPI_CUSTOMER_GETLIST`来获取客户列表,或者`BAPI_MATERIAL_GETDETAIL`来获取物料详细信息。 6. **异常处理和错误处理**:在与SAP交互时,必须处理可能出现...

    .NET项目命名规范

    - 获取列表的操作可以使用`GetList()`、`GetListById()`、`GetListByName()`等命名方式。 - 如果某个函数被重复调用超过两次,则应考虑将其封装成公共函数。 - 所有下拉列表、多级联动等功能的绑定方法应当放入...

    Django框架获取form表单数据方式总结

    2. **单选按钮** - 示例:`男` - 获取方法:`gender = request.POST.get('gender')` 单选按钮的值是与`name`属性相同的那个`value`属性值,如上例中的`man`或`woman`。 3. **复选框** - 示例:`...

    cms后台管理

    一 Jeecms安装过程 ...&lt;id name="id" type="java.lang.Integer" column="id"&gt;&lt;generator class="identity"/&gt;&lt;/id&gt; &lt;property name="title" column="title" type="java.lang.String" not-null="true" /&gt; ...

    手持机通信函数说明v2.0 华蓝佳声

    - `-2`:文件不存在。 - `-3`:内存分配失败。 - `-4`:文件读取失败。 - `-5`:文件写入失败。 - `-6`:文件重命名失败。 - `-7`:文件删除失败。 - `-8`:其他未知错误。 通过上述详细介绍,我们可以看到...

    jphp-windows-ext:DevelNext模块

    变更日志 --- 2.1.1 --- [Add] Windows::getSystemDrive() Migrate to jppm --- 2.1 --- [Add] Windows::reboot() [Add] Windows::shutdown() [Add] Windows::pressKey() ...[Fix] Startup::getList()

    java-sax-rss-reader:使用 Java SAX 的基本 Java RSS 阅读器

    使用 Java SAX 的基本 Java RSS 阅读器 只是 SAX 的 DefaultHandler 的基本实现,用于解析 RSS 2 提要。 不依赖于任何外部库,它只使用 SAX(自 Java 5 起包含在 JavaSE 中,也在 Android 上)。 使用示例 ...

    JAVA连接SAP说明.pdf

    在进行Java程序与SAP系统的连接操作时,通常需要借助Java连接器(JCo)库,这是SAP提供给Java开发者的一种连接方式,允许Java应用程序与SAP系统通信。以下知识点将基于提供的文件内容详细阐述如何使用JCo库连接到SAP...

Global site tag (gtag.js) - Google Analytics