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..

picture10.gif

//-------------------------------------------------------------
// 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.*;



{
 private JCO.Client mConnection = null;
 private JTextField returnValues;
 private IRepository mRepository;
 private JCO.Function myFunction;
 private JList companyCodeList;


  public GetCompanycodeList()
  { 
    setSizeAndPosition();

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

    
    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);

    
    returnValues = new JTextField(50);
    myContentPane.add(returnValues,BorderLayout.NORTH);


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


  }


 { 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);

 }


 {
    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");


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


    

    try
    {

     IFunctionTemplate ftemplate = mRepository.getFunctionTemplate("BAPI_COMPANYCODE_GETLIST");


      myFunction = new JCO.Function(ftemplate);


      mConnection.execute(myFunction);

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


  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"));


   JCO.Table company_codes = myFunction.getTableParameterList().getTable("COMPANYCODE_LIST");


   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"));

   }


   companyCodeList.setListData(listElements);


   JCO.releaseClient(mConnection);

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

 }





  {
    setSize(300,400);
    setTitle("Event handling");
    setResizable(false);



    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);


    
      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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值