1.在nwa控制台中配置properties属性,连接ERP的相关参数属性。
2. 查看里面的信息如下:
(1)连接的hostnanme,systemnum和System ID
(2) 连接登录名和密码,客户端号
(3)连接池信息
3. 添加对应的工程中的dependencies中添加JCO依赖
4. 实现的Java逻辑代码
package cn.com.dbic;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import cn.com.bean.AccountBean;
import cn.com.bean.CompanyBean;
import cn.com.bean.CosterBean;
import cn.com.bean.EmployeeBean;
import cn.com.bean.SalesTeamBean;
import cn.com.bean.VendorsBean;
importcom.sap.conn.jco.JCoDestinationManager;
importcom.sap.mw.jco.IFunctionTemplate;
importcom.sap.mw.jco.IRepository;
import com.sap.mw.jco.JCO;
importcom.sap.mw.jco.JCO.Structure;
import com.sap.mw.jco.JCO.Table;
importcom.sap.tc.logging.Location;
/**
* ?
*
* @version 1.0 ?
* @author Gregor Wolf, 2004
*
*/
public class myJCoClient {
//Name of the ClientPool,可以随意定义
staticfinal String POOL_NAME = "POOL_A";
JCO.ClientmConnection;
//The repository we will be using
IRepositoryrepository;
//Parameters,输入参数
privateJCO.ParameterList input;
privateJCO.ParameterList tables;
privateString inputXML;
privateString tablesXML;
privateLocation logger = Location.getLocation(myJCoClient.class);
publicmyJCoClient() {
try{
JCO.getClientPoolManager().removePool(POOL_NAME);
JCO.Pool pool =JCO.getClientPoolManager().getPool(POOL_NAME);
if (pool == null) {
Properties pro =JCoDestinationManager.getDestination(
"FinSystem").getProperties();
pro.put("jco.client.passwd","xxxxxxxxx"); // 用户密码
JCO.addClientPool(POOL_NAME, // pool name
5, // maximum number of connections
pro); // properties
}
mConnection = JCO.getClient(POOL_NAME);
// Create a new repository
repository =JCO.createRepository("MYRepository", POOL_NAME);
}catch (Exception ex) {
ex.printStackTrace();
}
}
protectedvoid cleanUp() {
JCO.removeClientPool(POOL_NAME);
}
publicString getInputXML() {
//System.out.println("getInputXML");
returninputXML;
}
publicString getTablesXML() {
//System.out.println("tablesXML");
returntablesXML;
}
//Retrieves and prints information about the remote system
//会计科目
publicList<AccountBean> myAccountClientCall(String outputXml) {
JCO.Clientclient = null;
List<AccountBean>list = new ArrayList<AccountBean>();
try{
IFunctionTemplateftemplate = repository
.getFunctionTemplate("ZBAPI_GL_ACCOUNT_MASTER_GET");
if(ftemplate != null) {
JCO.Functionfunction = ftemplate.getFunction();
//JCO.ParameterList input = function.getImportParameterList();
JCO.ParameterListoutput = function.getExportParameterList();
JCO.ParameterListtables = function.getTableParameterList();
logger.errorT(output.getString("E_MESSAGE").toString());
client= JCO.getClient(POOL_NAME);
client.execute(function);
outputXml= output.toXML();
Tabletab = tables.getTable("ZFIACCOUNT");
logger.errorT(tab.getNumRows()+ "aa");
for(int i = 0; i < tab.getNumRows(); i++) {
AccountBeanaccbean = new AccountBean();
StringAccountid = tab.getField("SAKNR").getValue()
.toString();
StringAccDes = tab.getField("TXT50").getValue().toString();
Stringcompcode = tab.getField("BUKRS").getValue()
.toString();
accbean.setAccountID(Accountid);
accbean.setAccDes(AccDes);
accbean.setCompanycode(compcode);
list.add(accbean);
tab.nextRow();
}
}else {
logger
.errorT("FunctionZBAPI_GL_ACCOUNT_MASTER_GET not found in backend system.");
}
}catch (Exception ex) {
logger.errorT("Caughtan exception: \n" + ex);
}finally {
JCO.releaseClient(client);
}
returnlist;
}
//Retrieves and prints information about the remote system
//成本中心
publicList<CosterBean> myCostCenterClientCall(String outputXml) {
JCO.Clientclient = null;
List<CosterBean>list = new ArrayList<CosterBean>();
try{
IFunctionTemplateftemplate = repository
.getFunctionTemplate("ZBAPI_COST_CENTER_MASTER_GET");
if(ftemplate != null) {
JCO.Functionfunction = ftemplate.getFunction();
//JCO.ParameterList input = function.getImportParameterList();
JCO.ParameterListoutput = function.getExportParameterList();
JCO.ParameterListtables = function.getTableParameterList();
logger.errorT(output.getString("E_MESSAGE").toString());
client= JCO.getClient(POOL_NAME);
client.execute(function);
outputXml= output.toXML();
Tabletab = tables.getTable("ZFICOST");
logger.errorT(tab.getNumRows()+ "aa");
for(int i = 0; i < tab.getNumRows(); i++) {
CosterBeanaccbean = new CosterBean();
StringBUKRS = tab.getField("BUKRS").getValue().toString();
StringKOSTL = tab.getField("KOSTL").getValue().toString();
StringLTEXT = tab.getField("LTEXT").getValue().toString();
StringKOSAR = tab.getField("KOSAR").getValue().toString();
//logger.errorT(
//(Date)tab.getField("DATBI").getValue()+"dada");
Datedatebeg = new Date(((java.util.Date) tab.getField(
"DATAB").getValue()).getTime());
Datedateend = new Date(((java.util.Date) tab.getField(
"DATBI").getValue()).getTime());
accbean.setCOMPANYCODE(BUKRS);
accbean.setCOSTCENTERID(KOSTL);
accbean.setCOSTCENTERDESC(LTEXT);
accbean.setCOSTCENTERTYPE(KOSAR);
accbean.setDATEBEG(datebeg);
accbean.setDATEEND(dateend);
list.add(accbean);
tab.nextRow();
}
}else {
logger
.errorT("FunctionZBAPI_COST_CENTER_MASTER_GET not found in backend system.");
}
}catch (Exception ex) {
logger.errorT("Caughtan exception: \n" + ex);
}finally {
JCO.releaseClient(client);
}
returnlist;
}
//Retrieves and prints information about the remote system
//外部供应商
publicList<VendorsBean> myVendorClientCall(String companycode,
StringoutputXml) {
JCO.Clientclient = null;
List<VendorsBean>list = new ArrayList<VendorsBean>();
try{
IFunctionTemplateftemplate = repository
.getFunctionTemplate("ZBAPI_VERDOR_MASTER_GET");
if(ftemplate != null) {
JCO.Functionfunction = ftemplate.getFunction();
JCO.ParameterListinput = function.getImportParameterList();
JCO.ParameterListoutput = function.getExportParameterList();
JCO.ParameterListtables = function.getTableParameterList();
input.setValue(companycode,"I_BUKRS");
logger.errorT(output.getString("E_MESSAGE").toString());
client= JCO.getClient(POOL_NAME);
client.execute(function);
outputXml= output.toXML();
Tabletab = tables.getTable("ZFIVENDOR");
logger.errorT(tab.getNumRows()+ "vendor");
for (int i = 0; i < tab.getNumRows();i++) {
VendorsBeanvbean = new VendorsBean();
StringBUKRS = tab.getField("BUKRS").getValue().toString();
StringLIFNR = tab.getField("LIFNR").getValue().toString();
StringNAME1 = tab.getField("NAME1").getValue().toString();
vbean.setCOMPANYCODE(BUKRS);
vbean.setVENDORID(LIFNR);
vbean.setVENDORNAME(NAME1);
list.add(vbean);
tab.nextRow();
}
}else {
logger
.errorT("FunctionZBAPI_VERDOR_MASTER_GET not found in backend system.");
}
}catch (Exception ex) {
logger.errorT("Caughtan exception: \n" + ex);
}finally {
JCO.releaseClient(client);
}
returnlist;
}
//Retrieves and prints information about the remote system
//员工主数据
publicList<EmployeeBean> myEmployeeClientCall(String outputXml,
List<EmployeeBean>secjoblist) {
JCO.Clientclient = null;
List<EmployeeBean>list = new ArrayList<EmployeeBean>();
try{
IFunctionTemplateftemplate = repository
.getFunctionTemplate("ZBAPI_GET_HR_EMPLOYEE_INFOR");
if(ftemplate != null) {
JCO.Functionfunction = ftemplate.getFunction();
//JCO.ParameterList input = function.getImportParameterList();
JCO.ParameterListoutput = function.getExportParameterList();
JCO.ParameterListtables = function.getTableParameterList();
logger.errorT(output.getString("E_MESSAGE").toString());
client= JCO.getClient(POOL_NAME);
client.execute(function);
outputXml= output.toXML();
Tabletab = tables.getTable("ZHREMPLOYEE");
logger.errorT(tab.getNumRows()+ "ZHREMPLOYEE");
for(int i = 0; i < tab.getNumRows(); i++) {
EmployeeBeanEbean = new EmployeeBean();
StringZEMPLID = tab.getField("ZEMPLID").getValue()
.toString();
StringZNAME = tab.getField("ZNAME").getValue().toString();
StringZKOSTL = tab.getField("ZKOSTL").getValue()
.toString();
StringZSEX = tab.getField("ZSEX").getValue().toString();
StringZEMAIL_ADDR = tab.getField("ZEMAIL_ADDR").getValue()
.toString();
StringZPOSITION = tab.getField("ZPOSITION").getValue()
.toString();
StringZEMPL_RCD = tab.getField("ZEMPL_RCD").getValue()
.toString();
if("".equals(ZEMPLID)||ZEMPLID==null||"".equals(ZKOSTL)||ZKOSTL==null){
}else{
Ebean.setEMPLOYEEID(ZEMPLID);
Ebean.setEMPLOYEENAME(ZNAME);
Ebean.setDEPARTMENTID(ZKOSTL);
Ebean.setEMAIL(ZEMAIL_ADDR);
Ebean.setSEX(ZSEX);
Ebean.setEMPGRADE(ZPOSITION);
Ebean.setZEMPL_RCD(ZEMPL_RCD);
if("0".equals(ZEMPL_RCD)) {
list.add(Ebean);
}else {// 兼职人员信息
secjoblist.add(Ebean);
}
}
tab.nextRow();
}
}else {
logger
.errorT("FunctionZBAPI_GET_HR_EMPLOYEE_INFOR not found in backend system.");
}
}catch (Exception ex) {
logger.errorT("Caughtan exception: \n" + ex);
}finally {
JCO.releaseClient(client);
}
returnlist;
}
//Retrieves and prints information about the remote system
//公司主数据
publicList<CompanyBean> myCompanyClientCall(String outputXml) {
JCO.Clientclient = null;
List<CompanyBean>list = new ArrayList<CompanyBean>();
try{
IFunctionTemplateftemplate = repository
.getFunctionTemplate("ZBAPI_COMPANY_CODE_GET");
if(ftemplate != null) {
JCO.Functionfunction = ftemplate.getFunction();
//JCO.ParameterList input = function.getImportParameterList();
JCO.ParameterListoutput = function.getExportParameterList();
JCO.ParameterListtables = function.getTableParameterList();
logger.errorT(output.getString("E_MESSAGE").toString());
client= JCO.getClient(POOL_NAME);
client.execute(function);
outputXml= output.toXML();
Tabletab = tables.getTable("ZCOMPCODE");
logger.errorT(tab.getNumRows()+ "ZCOMPCODE");
for(int i = 0; i < tab.getNumRows(); i++) {
StringBUKRS = tab.getField("BUKRS").getValue().toString();
StringBUTXT = tab.getField("BUTXT").getValue().toString();
CompanyBeancbean = new CompanyBean();
cbean.setCOMPANYCODE(BUKRS);
cbean.setCOMPANYNAME(BUTXT);
//Service
list.add(cbean);
tab.nextRow();
}
}else {
logger
.errorT("FunctionZBAPI_COMPANY_CODE_GET not found in backend system.");
}
}catch (Exception ex) {
logger.errorT("Caughtan exception: \n" + ex);
}finally {
JCO.releaseClient(client);
}
returnlist;
}
//销售团队,遍历内表,将里面的数据读出,放到一个list中去
public List<SalesTeamBean>mySalsTeamClientCall(){
JCO.Clientclient = null;
List<SalesTeamBean>salesTeamBeanList = new ArrayList<SalesTeamBean>();
try{
IFunctionTemplateftempalte = repository.getFunctionTemplate("ZBAPI_GET_XSTD");
if(ftempalte != null){
JCO.Functionfunction = ftempalte.getFunction();
JCO.ParameterListoutput = function.getExportParameterList();
JCO.ParameterListtables = function.getTableParameterList();
client= JCO.getClient(POOL_NAME);
client.execute(function);
Tabletable = tables.getTable("T_ZXSTD");
logger.errorT("销售团队表T_ZXSTD,行数:"+table.getNumRows());
SalesTeamBeansalesTeamBean = null;
if (table.getNumRows() > 0){
do{
salesTeamBean = new SalesTeamBean();
salesTeamBean.setCompanyCode(table.getField("BUKRS").getValue().toString());//公司编码
salesTeamBean.setSalesTeamCode(table.getField("ZTDBH").getValue().toString());//销售团队编号
salesTeamBean.setSalesTeamDesc(table.getField("ZTDMS").getValue().toString());//销售团队名称
salesTeamBeanList.add(salesTeamBean);
}while(table.nextRow());
}
}else{
logger.errorT("FunctionZBAPI_GET_XSTD not found in backend system.");
}
}catch (Exception e) {
logger.errorT("functionZBAPI_GET_XSTD query error:"+e.getMessage());
}finally{
JCO.releaseClient(client);
}
returnsalesTeamBeanList;
}
}
对应的ABAP开发代码非常简单,查询一张表返回结果内表。
FUNCTION zbapi_get_xstd.
*"----------------------------------------------------------------------
*"*"Local interface:
*" TABLES
*" T_ZXSTD STRUCTURE ZXSTD
*"----------------------------------------------------------------------
* È¡µÃÏúÊÛÍŶÓ
SELECT* INTO CORRESPONDING FIELDSOF TABLEt_zxstd FROM zxstd.
ENDFUNCTION.