之前的一篇文章是关于开发和部署axis2服务的一个简单例子,由于有的开发者需要在服务中访问数据库,所以我在这里开发一个访问oracle数据库的axis2服务。
    具体的axis2服务开发和部署步骤见前面的文章:[url]http://panpan.blog.51cto.com/489034/119204 [/url] 这里不再详述。

    新建工程:TestJDBCAxis,在src目录下新建包org.company,在包下新建DB类,这个类就是我要包装成服务的代码。
    工程需要加入ojdbc14.jar到build path下。
    DB.java代码如下:
package org.company;

import java.sql.*;

public class DB {
   private String sConnStr = "";
   final static String sDBDriver = "oracle.jdbc.driver.OracleDriver";

         public DB()
        {
          sConnStr = "jdbc:oracle:thin:@202.117.118.45:1521:repace";
        }
        
         public DB(String ip,String serviceName)
        {
          sConnStr = "jdbc:oracle:thin:@"+ip+ ":1521:"+serviceName;
        }
        
         public DB(String ip,String port,String serviceName)
        {          
          sConnStr = "jdbc:oracle:thin:@"+ip+ ":"+port+ ":"+serviceName;
        }
        
         public java.sql.Connection connectDbByThin()
        {
          java.sql.Connection conn= null;
           try
          {
            Class.forName(sDBDriver);
            conn = DriverManager.getConnection(sConnStr, "RCRegistry", "repace");
              }
           catch(Exception e)
          {
            System.out.println( "ERROR:"+e.getMessage());
          }
           return conn;
        }
        
         public java.sql.Connection connectDbByThin(String userId,String password)
        {
          java.sql.Connection conn= null;
           try
          {
            Class.forName(sDBDriver);
            conn = DriverManager.getConnection(sConnStr,userId,password);
          }
           catch(Exception e)
          {
            System.out.println( "ERROR:"+e.getMessage());
          }
           return conn;
        }
        
         public boolean save(String serviceid) {
          DB myDB = new DB();
          java.sql.Connection conn = myDB.connectDbByThin();
          System.out.println(conn);
           //创建statement对象
          Statement stmt;
     try {
      stmt = conn.createStatement();
            String sql = "insert into SERVICE(\"SERVICEID\") VALUES('" + serviceid + "')";
            stmt.executeQuery(sql);
             return true;
    } catch (SQLException e) {
       // TODO Auto-generated catch block
      e.printStackTrace();
       return false;
    }
        }
        
         /*
        public static void main(String[] args) throws SQLException {
          DB myDB = new DB();
          java.sql.Connection conn = myDB.connectDbByThin();
          System.out.println(conn);
          //创建statement对象
          Statement stmt = conn.createStatement();
          String sql = "insert into SERVICE(\"SERVICEID\") VALUES('kkkkkkk1')";
          stmt.executeQuery(sql);          
          //调用并执行SQL语句
          //rs.next();
          //rs.next();
          //System.out.println(rs.getString(2));
             }
             */

}

    连接字符串sConnStr = "jdbc:oracle:thin:@202.117.118.45:1521:repace";中, 202.117.118.45是oracle数据库服务器的地址,可以是远程或者本地,repace是一个数据库名。
getConnection(sConnStr, "RCRegistry", "repace")中, RCRegistry是数据库名, repace是数据库密码。
    发布该工程到axis2服务,服务名为TestJDBCAxis_service.aar,在axis2的WEB-INF\services下,在网址中输入:[url]http://localhost:8080/axis2/services/listServices[/url] 可以看到刚部署好的服务。
    编写客户端访问代码,同上一篇文章,TestDB.java代码如下:
package org.company;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

public class TestDB {

     private RPCServiceClient serviceClient;
     private Options options;
     private EndpointReference targetEPR;
    
     public TestDB(String endpoint) throws AxisFault {
        serviceClient = new RPCServiceClient();
        options = serviceClient.getOptions();
        targetEPR = new EndpointReference(endpoint);
        options.setTo(targetEPR);
    }
     public Object[] invokeOp(String targetNamespace, String opName,
            Object[] opArgs, Class<?>[] opReturnType) throws AxisFault,
            ClassNotFoundException {
         // 设定操作的名称
        QName opQName = new QName(targetNamespace, opName);
         // 设定返回值
        
         //Class<?>[] opReturn = new Class[] { opReturnType };

         // 操作需要传入的参数已经在参数中给定,这里直接传入方法中调用
         return serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
    }
     /**
        * @param args
        * @throws AxisFault
        * @throws ClassNotFoundException
        */

     public static void main(String[] args) throws AxisFault, ClassNotFoundException {
         // TODO Auto-generated method stub
         final String endPointReference = "http://localhost:8080/axis2/services/TestJDBCAxisService";
        final String targetNamespace = "http://company.org";
        TestDB client = new TestDB(endPointReference);
        
        String opName = "save";
        Object[] opArgs = new Object[]{"kkkkkkkk11"};
        Class<?>[] opReturnType = new Class[]{boolean[].class};
        
        Object[] response = client.invokeOp(targetNamespace, opName, opArgs, opReturnType);
        System.out.println(((boolean[])response[0])[0]);
    }
}

    运行该代码,可以发现出现错误,在tomcat后台打印出:
    ERROR:Class Not found : oracle.jdbc.driver.OracleDriver
    这个错误应该引起我们的注意,因为很多程序开发人员写web service访问数据库经常遇到这样的错误,然后花费很长时间也找不到错误的原因。出现这个错误是因为我们没有把ojdbc14.jar放入到axis2下的WEB-INF\lib下导致的。
    把ojdbc14.jar放入到到axis2下的WEB-INF\lib下,再次运行这个代码,发现运行成功。