JCO连接SAP例子
JCO連接SAP例子
?SAP JCo(SAP?Java?Connector,SAP?Java連接器)是SAP組件和Java應(yīng)用程序之間的中間件和接口實現(xiàn)機(jī)制。
JCo基于JNI(Java Native Interface),建立在RFC協(xié)議基礎(chǔ)之上,支持SAP服務(wù)器端入站(JAVA調(diào)用ABAP)及出站(ABAP調(diào)用JAVA)數(shù)據(jù)通信。
使用JCo的機(jī)器中必須要安裝JRE(JAVA運(yùn)行時環(huán)境)。
將從SAP官方服務(wù)站點下載的 JCO文件 sapjco3-ntintel-3.0.0.zip解壓,其中有連接組件(sapjco3.dll、sapjco3.jar)以及相關(guān)文檔和示例。
下面就用JCreator 做一個簡單的例子來測試JCO的功能:
1. 環(huán)境部署
?? 在D盤建立一個目錄lib,將sapjco3.dll、sapjco3.jar 拷貝到D:\lib下
?? 打開JCreator,打開菜單:配置--》選項,然后打開JDK配置文件,選中配置文件后點擊編輯,然后將D:\lib\sapjco3.jar添加到JDK配置文件的類清單中。
2.SAP端開發(fā)
SAP端用事務(wù)代碼SE37開發(fā)一個Function Module,支持RFC:
FUNCTION Z_BAPI_GET_ALL_USERS_DAILY.
*"----------------------------------------------------------------------
*"*"Local interface:
*"? IMPORTING
*"???? VALUE(USTYP) TYPE? XUUSTYP
*"? TABLES
*"????? USERS STRUCTURE? USR02
*"----------------------------------------------------------------------
TABLES:USR02.
DATA:ITAB_SAPUSERS TYPE TABLE of USR02 WITH HEADER LINE.
SELECT * INTO CORRESPONDING FIELDS OF TABLE ITAB_SAPUSERS
FROM USR02.
LOOP AT ITAB_SAPUSERS.
? users = ITAB_SAPUSERS.
? APPEND USERS.
ENDLOOP.
if ustyp is not INITIAL.
?? delete users
?????? where? ustyp <> ustyp.
endif.
ENDFUNCTION.
3.Java程序開發(fā)
用JCreator開發(fā)Java端程序:
1) 創(chuàng)建類User,封裝用戶的屬性
public class User {??
??? private String userId;??
??? private String name;??
??? private int age;?
??? public User(){}??
??? public User(String userId,String name,int age){??
??????? this.userId = userId;??
??????? this.name = name;??
??????? this.age = age;??
??? }?
??? public String getUserId() {??
??????? return userId;??
??? }?
??? public void setUserId(String userId) {??
??????? this.userId = userId;??
??? }?
??? public String getName() {??
??????? return name;??
??? }?
??? public void setName(String name) {??
??????? this.name = name;??
??? }?
??? public int getAge() {??
??????? return age;???
??? }?
??? public void setAge(int age) {??
??????? this.age = age;??
??? }??
}??
2) 創(chuàng)建ConnectSAPServer類,實現(xiàn)對連接池的管理
import java.io.File;
import java.io.FileOutputStream;
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.ext.DestinationDataProvider;
public class ConnectSAPServer
{
?//SAP服務(wù)器IP地址?????
?static String D12 = "XXX.25.0.XX";????
?static String T11 = "XXX.25.0.XX";?????????
?static String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";??????
?static{??????
Properties connectProperties = new Properties();???????
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, D12);??????????
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,? "00");??????? //系統(tǒng)編號?????????
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "900");?????? //SAP集團(tuán)???????
connectProperties.setProperty(DestinationDataProvider.JCO_USER,?? "baichi");?????? //SAP用戶名????????
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "12345678");???? //密碼???????
connectProperties.setProperty(DestinationDataProvider.JCO_LANG,?? "ZH");??????? //登錄語言?????????
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");? //最大連接數(shù)????????
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,"10");???? //最大連接線程?????????????
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);????
?}???????
? //如果連接配置文件不存在,則創(chuàng)建一個配置文件,并把配置信息寫入到文件中
? static void createDataFile(String name, String suffix, Properties properties)
?{????
???? File cfg = new File(name+"."+suffix);???????
???? if(!cfg.exists()){????????????
????? try{???????????????
???????? FileOutputStream fos = new FileOutputStream(cfg, false);????????
???????? properties.store(fos, "for tests only !");??????????????
???????? fos.close();???????????
???????? }
catch (Exception e){??????????????
? throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);???
????????? }?????
??? }????
?}??
?????
public static JCoDestination Connect(){????
? JCoDestination destination =null;??????????
? try {????????????
??????? destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);???
????? }
? catch (JCoException e)?
????? {??
??????? e.getCause();?????
????? }????????????????
? return destination;???
? }?
??
? public static void main(String[] s)
? {
?? JCoDestination jcoConn = testJCO.Connect();
?? System.out.println(jcoConn.getDestinationName());
? }?
}
3) 創(chuàng)建一個類ConnectSAPTable,用于調(diào)用sap的function module
import java.util.ArrayList;??
import java.util.List;??
import com.sap.conn.jco.JCoDestination;??
import com.sap.conn.jco.JCoException;??
import com.sap.conn.jco.JCoFunction;??
import com.sap.conn.jco.JCoTable;??
//import User;??
public class ConnectSAPTable {???
??? private static JCoDestination jCoDestination;??
??? public static List<User> returnSAPUser_All(String ustyp) throws JCoException{??
??????? List<User> users = new ArrayList<User>();??
??????? jCoDestination = ConnectSAPServer.Connect();???
??????? JCoFunction function = jCoDestination.getRepository().getFunction("Z_BAPI_GET_ALL_USERS_DAILY");???
??????? if (function == null) throw new RuntimeException("Z_BAPI_GET_ALL_USERS_DAILY? not found in SAP.");??
??????? //傳入的參數(shù)?
??????? function.getImportParameterList().setValue("USTYP",ustyp);? // user type?
??????? function.execute(jCoDestination);??
??????? JCoTable returnTable = function.getTableParameterList().getTable("USERS");??
??????? if (returnTable.getNumRows()>0) {??
?????????? returnTable.firstRow();???
??????????? for (int i = 0; i < returnTable.getNumRows(); i++,returnTable.nextRow()) {??
??????????????? User user = new User();???
??????????????? user.setUserId(returnTable.getString("BNAME"));???
??????????????? users.add(user);??
??????????? }??
??????? }??
??????? return users;??
??? }??
}??
4) 創(chuàng)建一個類ExportSAPUserByDaily,用于調(diào)度,提供參數(shù)輸入并處理返回的結(jié)果
import java.util.List;????
import com.sap.conn.jco.JCoException;
//import User;
public class ExportSAPUserByDaily {??
??? public static void main(String[] args) {??
??????? System.out.println("the call result by JCo:");
??????? try {???
??????????? List<User> userList = ConnectSAPTable.returnSAPUser_All(args[0]);??
??????????? for (User sapUser : userList) {???
??????????????? System.out.println(sapUser.getUserId());??
??????????? }???
??????? } catch (JCoException e) {??
??????????? e.getCause();??
??????????? e.printStackTrace();
??????? }??
??? }??
}?
總結(jié)
以上是生活随笔為你收集整理的JCO连接SAP例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ABAP--如何快速从BSEG读取数据
- 下一篇: JCO_(配置连接池)