java中調用web service接口(使用AXIS的CALL調用方式)


1.首先要把AXIS包里的jar文件放到java項目的lib目錄下,這里用的是AXIS1_4版本

2.在java代碼中實現:遠程調用路徑以及設置參數

 

3.若接口提供的方法需要傳遞xml類型的參數,例如接口是這樣的

實現代碼 如下:

package test;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

import util.DBUtil;
import util.MySqlDBUtil;


public class CopyOfOrgSt {
    private static Connection con = null;
    private static PreparedStatement pst = null;
    private static ResultSet rs = null;
    private static Connection mcon = null;
    private static PreparedStatement mpst = null;
    private static ResultSet mrs = null;
    
    public static void main(String[] args){
        CopyOfOrgSt orgst=new CopyOfOrgSt();
        orgst.seAndtr();
    }
    
    public String invokeRemoteFuc(String method,String param) {
        System.out.println(param);
          // 遠程調用路徑 
          String endpoint = "http://IP地址:端口號/*/*/*?wsdl";
          String result = "call failed!";
          Service service = new Service();
          Call call;
                 try {
                     call = (Call) service.createCall();
                     call.setTargetEndpointAddress(endpoint); 
                     //new QName的URL是要指向的命名空間的名稱,這個URL地址在你的wsdl打開后可以看到的,
                     //上面有寫着targetNamespace="http://*.*.*/",這個就是你的命名空間值了;
                     call.setOperationName(new QName("http://*.*.*/",method)); // 調用的方法名
                     // 設置參數名 :參數名 ,參數類型:String, 參數模式:'IN' or 'OUT'
                     call.addParameter("guid",XMLType.XSD_STRING,ParameterMode.IN);
                     call.setEncodingStyle("UTF-8");
                     call.setReturnType(XMLType.XSD_STRING);
                     result = (String) call.invoke(new Object[] {param});// 遠程調用
                } catch (ServiceException e) {
                     e.printStackTrace();
                } catch (RemoteException e) {
                     e.printStackTrace();
                }
                return result;
    }
    
    public  void seAndtr() {
        con=DBUtil.getConnection();
        mcon=MySqlDBUtil.getConnection();
        String param="";
        String sql="select a.FKeyID,isnull(b.FKeyID,'-1') as PARENTORGID,a.FNo,a.FName from ORGMAIN a left join ORGMAIN b on cast(a.FParentID as nvarchar(50))=b.FNo";
        String msql="select * from org_organization where orgserial=?";
        try {//   更新/添加
            pst=con.prepareStatement(sql);
            mpst=mcon.prepareStatement(msql);
            rs=pst.executeQuery();
            if(rs!=null){
                while(rs.next()){
                    param="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"+
                              "<ORGANIZATION>"+
                                      "<Field ColName=\"guid\" Value=\""+rs.getString("FKeyID")+"\"></Field>"+
                                      "<Field ColName=\"parentOrgId\" Value=\""+rs.getString("PARENTORGID")+"\"></Field>"+
                                      "<Field ColName=\"orgName\" Value=\""+rs.getString("FName")+"\"></Field>"+
                                      "<Field ColName=\"orgCode\" Value=\""+rs.getString("FNo")+"\"></Field>"+
                                      //"<Field ColName=\"orgManagerEmpId\" Value=\""+rs.getString("FKeyID")+"\"></Field>"+
                                      //"<Field ColName=\"orgDescripte\" Value=\""+rs.getString("FKeyID")+"\"></Field>"+
                              "</ORGANIZATION>";
                    String ppid=rs.getString("FNo");
                    System.out.println("======================================="+ppid);
                    mpst.setString(1,ppid);
                    mrs=mpst.executeQuery();
                    
                    if(mrs.next()){//有結果集,更新
                        String result=invokeRemoteFuc("updateOrganization",param);
                        System.out.println("更新結果:"+result);
                    }else{//無結果集,新增
                        String result=invokeRemoteFuc("addOrganization",param);
                        System.out.println("新增結果:"+result);
                    }
                }
            }
            //刪除
            String delsql="select orgserial from org_organization where orgserial not in (?) and guid is not null";
            mpst=mcon.prepareStatement(delsql);
                String dpsql="select FNo from ORGMAIN";
                Statement dpst=con.createStatement();
                ResultSet dprs=dpst.executeQuery(dpsql);
                String str="";
                if(dprs!=null){//從中間庫中查詢所有的FNo
                    while(dprs.next()){
                        str=str.concat(","+dprs.getString("FNo"));//將所有FNo用","拼接
                        //str.append(","+dprs.getString("FNo"));//將所有FNo用","拼接
                    }
                    str=str.substring(1);
                    mpst.setString(1,str);//將字符串設置到最外層查詢
                    mrs=mpst.executeQuery();//執行最外層查詢
                    String delorgserial="";
                    if(mrs!=null){
                        while(mrs.next()){
                            delorgserial=delorgserial.concat(","+mrs.getString("orgserial"));//取得oa所有的需要刪除的組織編號
                        }
                        delorgserial=delorgserial.substring(1);
                        System.out.println("---"+delorgserial);
                        //取得需要刪除的記錄的guid和組織名,orgparentorgid字段用來關聯出parentOrgId
                        String selsql="select guid,orgname,orgserial,orgparentorgid from org_organization where orgserial=?";
                        mpst=mcon.prepareStatement(selsql);    
                        String[] strs=delorgserial.split(",");
                        for(int i=0;i<strs.length;i++){
                            mpst.setString(1,strs[i]);//遍歷字符數組分別賦值到sql
                            mrs=mpst.executeQuery();//執行最外層查詢
                            //用查詢出來的guid作為parentOrgId
                            String selmsql="";
                            if(mrs.next()){
                                int orgparentorgid=mrs.getInt("orgparentorgid");
                                selmsql="select guid from org_organization where org_id="+orgparentorgid;
                            }
                            Statement selmst=mcon.createStatement();
                            ResultSet selmrs=selmst.executeQuery(selmsql);
                            String guid="";
                            if(selmrs.next()){
                                guid=selmrs.getString("guid");
                            }
                            param="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"+
                                      "<ORGANIZATION>"+
                                              "<Field ColName=\"guid\" Value=\""+mrs.getString("guid")+"\"></Field>"+
                                              "<Field ColName=\"parentOrgId\" Value=\""+guid+"\"></Field>"+
                                              "<Field ColName=\"orgName\" Value=\""+mrs.getString("orgname")+"\"></Field>"+
                                              "<Field ColName=\"orgCode\" Value=\""+mrs.getString("orgserial")+"\"></Field>"+
                                              //"<Field ColName=\"orgManagerEmpId\" Value=\""+rs.getString("FKeyID")+"\"></Field>"+
                                              //"<Field ColName=\"orgDescripte\" Value=\""+rs.getString("FKeyID")+"\"></Field>"+
                                      "</ORGANIZATION>";
                            //執行刪除操作
                            String result=invokeRemoteFuc("delOrganization",param);
                            System.out.println("刪除結果:"+result);
                            
                        }
                        
                    }else{
                        System.out.println("沒有需要刪除的數據");
                    }
                }
            
                
            rs.close();
            pst.close();
            con.close();
            mrs.close();
            mpst.close();
            mcon.close();
            
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM