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();
        }
    }
}

 

posted @ 2015-12-04 14:15  没葱的葱油饼  阅读(24092)  评论(1编辑  收藏  举报