日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

JCO连接SAP例子

發(fā)布時(shí)間:2025/3/20 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JCO连接SAP例子 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

JCO連接SAP例子

?SAP JCo(SAP?Java?Connector,SAP?Java連接器)是SAP組件和Java應(yīng)用程序之間的中間件和接口實(shí)現(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)行時(shí)環(huán)境)。
將從SAP官方服務(wù)站點(diǎn)下載的 JCO文件 sapjco3-ntintel-3.0.0.zip解壓,其中有連接組件(sapjco3.dll、sapjco3.jar)以及相關(guān)文檔和示例。

下面就用JCreator 做一個(gè)簡(jiǎn)單的例子來(lái)測(cè)試JCO的功能:

1. 環(huán)境部署
?? 在D盤(pán)建立一個(gè)目錄lib,將sapjco3.dll、sapjco3.jar 拷貝到D:\lib下
?? 打開(kāi)JCreator,打開(kāi)菜單:配置--》選項(xiàng),然后打開(kāi)JDK配置文件,選中配置文件后點(diǎn)擊編輯,然后將D:\lib\sapjco3.jar添加到JDK配置文件的類(lèi)清單中。

2.SAP端開(kāi)發(fā)

SAP端用事務(wù)代碼SE37開(kāi)發(fā)一個(gè)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程序開(kāi)發(fā)

用JCreator開(kāi)發(fā)Java端程序:

1) 創(chuàng)建類(lèi)User,封裝用戶(hù)的屬性

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類(lèi),實(shí)現(xiàn)對(duì)連接池的管理

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)編號(hào)?????????
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "900");?????? //SAP集團(tuán)???????
connectProperties.setProperty(DestinationDataProvider.JCO_USER,?? "baichi");?????? //SAP用戶(hù)名????????
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "12345678");???? //密碼???????
connectProperties.setProperty(DestinationDataProvider.JCO_LANG,?? "ZH");??????? //登錄語(yǔ)言?????????
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");? //最大連接數(shù)????????
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,"10");???? //最大連接線(xiàn)程?????????????
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);????
?}???????

? //如果連接配置文件不存在,則創(chuàng)建一個(gè)配置文件,并把配置信息寫(xiě)入到文件中
? 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)建一個(gè)類(lèi)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)建一個(gè)類(lèi)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例子的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。