📄 materialsearch.java
字号:
package myApp.beans;
import java.math.BigDecimal;
import java.util.Enumeration;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import com.sap.mw.jco.JCO;
/**
* @author Steve Steffen
*
* Bean to connect to SAP and perform a material search
*/
public class MaterialSearch {
public static void main(String[] arv){
MaterialSearch m = new MaterialSearch();
m.setMaterial("*");
System.out.println(m.getList());
}
private static final String POOL_NAME = "POOL";
private String myMaterial="";
/**
* Constructor for MaterialGetBapi.
*/
public MaterialSearch() {
super();
}
/**
*
*/
public void setMaterial(String m){
myMaterial = m;
}
public String getMaterial(){
return myMaterial;
}
public String getList() {
JCO.Client client = null;
JCO.Pool pool = JCO.getClientPoolManager().getPool(POOL_NAME);
if(pool == null){
JCO.addClientPool(POOL_NAME,
5,
loadProperties("myApp/properties/sapLogin"));
}
try{
client = JCO.getClient(POOL_NAME);
}catch(JCO.Exception e){
if(e.JCO_ERROR_RESOURCE == e.getGroup())
System.out.println("Pool is Full");
}
//
JCO.Repository mrep = new JCO.Repository("JCOrep",POOL_NAME);
JCO.Function jfun = mrep.getFunctionTemplate("BAPI_MATERIAL_GETLIST").getFunction();
JCO.Table matnrselection = jfun.getTableParameterList().getTable("MATNRSELECTION");
//don't want to crash the web component, so only return 100 components
jfun.getImportParameterList().setValue("100","MAXROWS");
matnrselection.appendRow();
matnrselection.setValue("I","SIGN"); //set at first position
matnrselection.setValue("CP","OPTION"); //set at first position
matnrselection.setValue(myMaterial,"MATNR_LOW"); //set at first position
client.connect();
client.execute(jfun);
JCO.releaseClient(client);
JCO.Table matnrlist = jfun.getTableParameterList().getTable("MATNRLIST");
StringBuffer sb = new StringBuffer();
for(int rows =0; rows < matnrlist.getNumRows(); rows++,matnrlist.nextRow()){
sb.append("<tr><td>");
sb.append(matnrlist.getString("MATERIAL") + "</td><td>");
sb.append(matnrlist.getString("MATL_DESC") + "</td></tr>");
}
return sb.toString();
}
private Properties loadProperties(String propertiesFile){
ResourceBundle myResourceBundle = null;
try{
myResourceBundle = ResourceBundle.getBundle(propertiesFile);
}catch(MissingResourceException e){
e.printStackTrace();
return null;
}
Properties myProperties = new Properties();
Enumeration e = myResourceBundle.getKeys();
String tempString = "";
while(e.hasMoreElements()){
tempString = (String) e.nextElement();
myProperties.setProperty(tempString, myResourceBundle.getString(tempString));
}
return myProperties;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -