1. Download the Java Connector
You find the JCo on the SAP service marketplace. http://service.sap.com/connectors/
To download the software you have to have a valid user. Registration is free of charge.
window.google_render_ad();
The following description assumes your are using Windows.
Create a new Java project "de.vogella.sap.jco" and create a folder lib in it.
From your JCo Download copy the following files into the folder lib.
sapjco3.jar
sapjco3.dll
Select sapjco3.jar -> Right mouse click -> Properties and add the jar to your Java Build Path.
If you are using Windows add the sapjco3.dll to your Windows System c:\winnt\system32 (or c:\C:\WINDOWS\system32) directory. For any other operating system please check the installation notes of SAP.
We will save the SAP system settings in the following object.
package de.vogella.sap.jco.model; public class SapSystem { private String client; private String user; private String password; private String language; private String host; private String sysID; public void setClient(String client) { this.client = client; } public void setUser(String user) { this.user = user; } public void setPassword(String password) { this.password = password; } public void setLanguage(String language) { this.language = language; } public void setHost(String host) { this.host = host; } public void setSystemNumber(String sysID) { this.sysID = sysID; } public String getClient() { return client; } public String getUser() { return user; } public String getPassword() { return password; } public String getLanguage() { return language; } public String getHost() { return host; } public String getSystemNumber() { return sysID; } public String toString() { String s = ""; s += "Client " + client; s += " User " + user; s += " Password " + password; s += " Host " + host; s += " Language " + language; return s; } }
The class JCoDestinationManager allows to retrieve the connection to a defined connection to the SAP system via .getDestination(String s). The destination of can get configured via a DestinationDataProvider. Here you can add you own implementation.
First implement the class "MyDestinationDataProvider".
package de.vogella.sap.jco.connection; import java.util.Properties; import com.sap.conn.jco.ext.DestinationDataEventListener; import com.sap.conn.jco.ext.DestinationDataProvider; /* * Provides the data connection to the SAP system * */ public class MyDestinationDataProvider implements DestinationDataProvider { static String SAP_SERVER = "SAP_SERVER"; private DestinationDataEventListener eventListener; private Properties ABAP_AS_properties; @Override public Properties getDestinationProperties(String arg0) { return ABAP_AS_properties; } public void setDestinationDataEventListener( DestinationDataEventListener eventListener) { this.eventListener = eventListener; } @Override public boolean supportsEvents() { return true; } public void changePropertiesForABAP_AS(Properties properties) { if (properties == null) { eventListener.deleted("SAP_SERVER"); ABAP_AS_properties = null; } else { if (ABAP_AS_properties != null && !ABAP_AS_properties.equals(properties)) eventListener.updated("SAP_SERVER"); ABAP_AS_properties = properties; } } }
The following class is configured via the constructor with an SAPSystem and allows to retrieve a SAP function. This retrieved function can then be configured and finally executed.
package de.vogella.sap.jco.connection; import java.util.Properties; import com.sap.conn.jco.JCoContext; 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.JCoRepository; import com.sap.conn.jco.ext.DestinationDataProvider; import de.vogella.sap.jco.model.SapSystem; /** * Connection allows to get and execute SAP functions. The constructor expect a * SapSystem and will save the connection data to a file. The connection will * also be automatically be established. */ public class Connection { static String SAP_SERVER = "SAP_SERVER"; private JCoRepository repos; private JCoDestination dest; private final Properties properties; public Connection(SapSystem system) { properties = new Properties(); properties.setProperty(DestinationDataProvider.JCO_ASHOST, system .getHost()); properties.setProperty(DestinationDataProvider.JCO_SYSNR, system .getSystemNumber()); properties.setProperty(DestinationDataProvider.JCO_CLIENT, system .getClient()); properties.setProperty(DestinationDataProvider.JCO_USER, system .getUser()); properties.setProperty(DestinationDataProvider.JCO_PASSWD, system .getPassword()); properties.setProperty(DestinationDataProvider.JCO_LANG, system .getLanguage()); MyDestinationDataProvider myProvider = new MyDestinationDataProvider(); myProvider.changePropertiesForABAP_AS(properties); com.sap.conn.jco.ext.Environment .registerDestinationDataProvider(myProvider); try { dest = JCoDestinationManager.getDestination(SAP_SERVER); System.out.println("Attributes:"); System.out.println(dest.getAttributes()); System.out.println(); repos = dest.getRepository(); } catch (JCoException e) { throw new RuntimeException(e); } } /** * Method getFunction read a SAP Function and return it to the caller. The * caller can then set parameters (import, export, tables) on this function * and call later the method execute. * * getFunction translates the JCo checked exceptions into a non-checked * exceptions */ public JCoFunction getFunction(String functionStr) { JCoFunction function = null; try { function = repos.getFunction(functionStr); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException( "Problem retrieving JCO.Function object."); } if (function == null) { throw new RuntimeException("Not possible to receive function. "); } return function; } /** * Method execute will call a function. The Caller of this function has * already set all required parameters of the function * */ public void execute(JCoFunction function) { try { JCoContext.begin(dest); function.execute(dest); JCoContext.end(dest); } catch (JCoException e) { e.printStackTrace(); } } }
You can now run a function model against the SAP backend system.
I personally like the standard Java API therefore I introduced a wrapper around the SAP table parameter in a function module.
package de.vogella.sap.jco.adapter; import com.sap.conn.jco.JCoTable; /** * TableAdapter is used to simplify the reading of the values of the Jco tables */ public class TableAdapterReader { protected JCoTable table; public TableAdapterReader(JCoTable table) { this.table = table; } public String get(String s) { return table.getValue(s).toString(); } public Boolean getBoolean(String s) { String value = table.getValue(s).toString(); return value.equals("X"); } public String getMessage() { return table.getString("MESSAGE"); } public int size() { return table.getNumRows(); } public void next() { table.nextRow(); } }
Given this wrapper you can use the following class to read data. The function I'm using as an example is BAPI_USER_GETLIST which return a number of SAP Logon names.
package test; import com.sap.conn.jco.JCoFunction; import com.sap.conn.jco.JCoTable; import de.vogella.sap.jco.adapter.TableAdapterReader; import de.vogella.sap.jco.connection.Connection; import de.vogella.sap.jco.model.SapSystem; public class Tester { static String SAP = "SAP_SERVER"; public static void main(String[] args) { // SAP System SapSystem system = new SapSystem(); system.setClient("600"); system.setHost("pwdf6394.wdf.sap.corp"); system.setLanguage("en"); system.setSystemNumber("76"); system.setUser("mytester"); system.setPassword("welcome"); Connection connect = new Connection(system); JCoFunction function = connect.getFunction("BAPI_USER_GETLIST"); function.getImportParameterList().setValue("MAX_ROWS", 10); connect.execute(function); JCoTable table = function.getTableParameterList().getTable("USERLIST"); System.out.println(table.isEmpty()); TableAdapterReader tableAdapter = new TableAdapterReader(table); System.out.println("Number of Users: " + tableAdapter.size()); for (int i = 0; i < tableAdapter.size(); i++) { // USERNAME is on the fields in the structure user System.out.println(tableAdapter.get("USERNAME")); tableAdapter.next(); } } }
For questions and discussion around this article please use the www.vogella.de Google Group. Also if you note an error in this article please post the error and if possible the correction to the Group.
I believe the following is a very good guideline for asking questions in general and also for the Google group How To Ask Questions The Smart Way.
http://www.vogella.de/articles/Eclipse/article.html - Using Eclipse as IDE - Tutorial
원문 : http://www.vogella.de/articles/SAPJCo/article.html#downloadjco
'ERP-SAP > ABAP' 카테고리의 다른 글
<ABAP> SOURCE CODE 다운로드 / 소스 다운로드 (4) | 2015.04.17 |
---|---|
<ABAP> 현재 실행중인 Function 명 / 프로그램 명 가져오기. (0) | 2015.03.03 |
<ABAP> New debugger 사용 방법 (abap 디버깅 방법) (0) | 2014.11.07 |
<ABAP> error generating a test frame in FM 에러 처리 방법 (0) | 2014.11.06 |
<ABAP> Call function Parameter 변수 선언 자동 생성방법(혹은 셋팅) (0) | 2014.11.05 |