请问哪位有用JAVA调用SAP的实例?

liangsonliu 2003-10-16 07:45:07
请问哪位有用JAVA调用SAP的实例?
...全文
56 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
chumliu 2003-10-16
  • 打赏
  • 举报
回复
这有:

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

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


//-------------------------------------------------------------
// 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) {}

}

}

2,679

社区成员

发帖
与我相关
我的任务
社区描述
企业开发 ERP/CRM
社区管理员
  • ERP/CRM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧