Java连SAP

参考:http://www.cnblogs.com/cfas/p/3141468.html

1、连接SAP服务器

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 { 
    static String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL"; 
    static{ 
        Properties connectProperties = new Properties(); 
        connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "192.168.0.1");  
        connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR,  "00");        
        connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "450");       
        connectProperties.setProperty(DestinationDataProvider.JCO_USER,   "a0001");  
        connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "123456");     
        connectProperties.setProperty(DestinationDataProvider.JCO_LANG,   "en");        
        connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3");  
        connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT,"10");     
     
        createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties); 
    } 
     
    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(); 
        e.printStackTrace();
        }      
        return destination; 
    } 



2、

public class SalesOrder { 
    private String vkorg; 
    private String vtweg; 
    private String spart; 
    private String vbeln;
    private String posnr;
    private String kunnr;
    private String matnr;
     
    public SalesOrder(){} 
     
    public SalesOrder(String vkorg,String vtweg,String spart, String vbeln, 
    String posnr, String kunnr, String matnr){ 
        this.vkorg = vkorg; 
        this.vtweg = vtweg; 
        this.spart = spart;
        this.vbeln = vbeln;
        this.posnr = posnr;
        this.kunnr = kunnr;
        this.matnr = matnr;
    } 
 
    public String getVkorg() { 
        return vkorg; 
    } 
 
    public void setVkorg(String vkorg) { 
        this.vkorg = vkorg; 
    } 
 
    public String getVtweg() { 
        return vtweg; 
    } 
 
    public void setVtweg(String vtweg) { 
        this.vtweg = vtweg; 
    } 
 
    public String getSpart() { 
        return spart; 
    } 
 
    public void setSpart(String spart) { 
        this.spart = spart; 
    } 
    
    public String getVbeln() { 
        return vbeln; 
    } 
 
    public void setVbeln(String vbeln) { 
        this.vbeln = vbeln; 
    } 
    
    public String getPosnr() { 
        return posnr; 
    } 
 
    public void setPosnr(String posnr) { 
        this.posnr = posnr; 
    } 
    
    public String getKunnr() { 
        return kunnr; 
    } 
 
    public void setKunnr(String kunnr) { 
        this.kunnr = kunnr; 
    } 
    public String getMatnr() { 
        return matnr; 
    } 
 
    public void setMatnr(String matnr) { 
        this.matnr = matnr; 
    } 


3、

import javax.swing.table.AbstractTableModel;


public class SoTableModel extends AbstractTableModel{
private static final long serialVersionUID = 1L;  
public String[] colNames;
    private Object[][] stockdata = null;

    public SoTableModel(String[] tableTitle, Object[][] tabledata){
    colNames = tableTitle;
    stockdata = tabledata;
    }
    public int getColumnCount() {  
        return colNames.length;  
    }  
    public int getRowCount() { 
        return stockdata.length;  
    }  
    public String getColumnName(int col) {
        return colNames[col];
      }
    public void setValueAt(Object value, int row, int col){
    Integer num = null;
        if ((String) value != null || !String.valueOf(value).equals("")) {
            stockdata[row][col] = value;
            this.fireTableDataChanged();
          }
    }
    // 从list中拿出rowIndex行columnIndex列显示的值  
    public Object getValueAt(int row, int col) {  
    return stockdata[row][col];
    }  
    public void setStockData(Object[][] data) {
        this.stockdata = data;
        this.fireTableDataChanged();
      }


      public Object[][] getStockData() {
        return stockdata;
      }
}


4、

import javax.swing.*;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
import java.util.ArrayList;


import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JTable;


import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoTable;


public class Inputfile extends JFrame implements ActionListener {


private static final long serialVersionUID = 1L;
JTextField jvbeln;
JTextField jkunnr;
JButton btn;
Object[][] pp;
SoTableModel somodel;
public Inputfile() {
JFrame f = new JFrame("SAP-Java");
JPanel p = new JPanel();
JLabel lablevbeln = new JLabel("销售单号");
JLabel lablekunnr = new JLabel("客户编号");
jvbeln = new JTextField(10);
jkunnr = new JTextField(10);
btn = new JButton("确定");
btn.addActionListener(this);
String[] colname = {"销售组织", "分销渠道", "产品组", "客户编号", "销售单号","行项目号","物料号"};
Object[][]pp ={
{"","","","","","",""}}; 

somodel = new SoTableModel(colname, pp);
JTable table = new JTable(somodel);
table.setPreferredScrollableViewportSize(new Dimension(500,200));
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
   table.setModel(somodel);
JScrollPane scrollPane=new JScrollPane(table);
   p.add(lablevbeln);
p.add(jvbeln);
p.add(lablekunnr);
p.add(jkunnr);
p.add(btn);
f.add(p,BorderLayout.NORTH);
f.getContentPane().add(scrollPane,BorderLayout.SOUTH);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
              System.exit(0);
            }
          });  
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(800,300);

}
private static JCoDestination jCoDestination;
public void actionPerformed(ActionEvent e) {
   String t1 = jvbeln.getText();
if (e.getSource() == btn) {
pp = Getsodata(t1);
somodel.setStockData(pp);
}
}

   public Object[][]Getsodata(String t1){
  Object[][] redata = null;
 // Object[][] redata ={
// {"fff","dsafasdf","dasfasf","cdd","da","da","dd"},
// {"阿呆","dsafasdf","dasfasf","cdd","da","da","dd"},
// }; 
  ArrayList<SalesOrder> solist = new ArrayList<SalesOrder>();
  ArrayList arrvkorg = new ArrayList();
  ArrayList arrvtweg = new ArrayList();
  ArrayList arrspart = new ArrayList();
  ArrayList arrkunnr = new ArrayList();
  ArrayList arrvbeln = new ArrayList();
  ArrayList arrposnr = new ArrayList();
  ArrayList arrmatnr = new ArrayList();
  
try {
jCoDestination = ConnectSAPServer.Connect();
JCoFunction function = jCoDestination.getRepository()
.getFunction("Z_RFC_SO_GETDATA");
if (function == null)
throw new RuntimeException(
"Z_RFC_SO_GETDATA not found in SAP.");


// 传入的参数
function.getImportParameterList().setValue("SVBELN", t1);
//function.getImportParameterList().setValue("SKUNNR", t2);


function.execute(jCoDestination);
           
JCoTable returnTable = function.getTableParameterList()
.getTable("ZSO_INFO");

if (returnTable.getNumRows() > 0) {
           
returnTable.firstRow();
for (int i = 0; i < returnTable.getNumRows(); i++, returnTable
.nextRow()) {
SalesOrder so = new SalesOrder();
so.setVkorg(returnTable.getString("VKORG"));
so.setVtweg(returnTable.getString("VTWEG"));
so.setSpart(returnTable.getString("SPART"));
so.setKunnr(returnTable.getString("KUNNR"));
so.setVbeln(returnTable.getString("VBELN"));
so.setPosnr(returnTable.getString("POSNR"));
so.setMatnr(returnTable.getString("MATNR"));
solist.add(so); 
arrvkorg.add(returnTable.getString("VKORG"));
arrvtweg.add(returnTable.getString("VTWEG"));
arrspart.add(returnTable.getString("SPART"));
arrkunnr.add(returnTable.getString("KUNNR"));
arrvbeln.add(returnTable.getString("VBELN"));
arrposnr.add(returnTable.getString("POSNR"));
arrmatnr.add(returnTable.getString("MATNR"));
}

     redata = new Object[returnTable.getNumRows()][7];
         for(int t =0; t<returnTable.getNumRows(); t++){
         redata[t][0] = arrvkorg.get(t).toString();
         redata[t][1] = arrvtweg.get(t).toString();
         redata[t][2] = arrspart.get(t).toString(); 
         redata[t][3] = arrkunnr.get(t).toString(); 
         redata[t][4] = arrvbeln.get(t).toString(); 
         redata[t][5] = arrposnr.get(t).toString(); 
         redata[t][6] = arrmatnr.get(t).toString(); 
         }
         //for(int m = 0; m<returnTable.getNumRows(); m++){
       //  System.out.print(pp[m][0]+"~"+pp[m][1]+"~"+pp[m][5]+"~"+pp[m][6]);  
      //   }
//for (SalesOrder saporder : solist) {
                  //  System.out.println(saporder.getVbeln());
              // System.out.println(saporder.getMatnr());
         //  }


}


} catch (Exception exx) {
exx.printStackTrace();
}
  return redata;
   }
public static void main(String args[]) {
new Inputfile();
}
}


5、SE37创建函数如下:

z_rfc_so_getdata

DATA:itab LIKE TABLE OF zsoinfo WITH HEADER LINE.

  SELECT
    vbak~vkorg
    vbak~vtweg
    vbak~spart
    vbak~kunnr
     vbap~vbeln
     vbap~posnr
    vbap~matnr
    INTO CORRESPONDING FIELDS OF TABLE itab FROM vbak INNER JOIN vbap ON vbak~vbeln vbap~vbeln
    WHERE  vbak~vbeln svbeln.

  LOOP AT itab.
    "MOVE-CORRESPONDING itab TO zso_info.
    zso_info itab.
    APPEND zso_info.
  ENDLOOP.
ENDFUNCTION.

输入参数


输出参数




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值