開發Axis2服務訪問oracle數據庫(遠程) 關於錯誤Class Not found : oracle.jdbc.driver.OracleDriver

    以前的一篇文章是關於開發和部署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下,再次運行這個代碼,發現運行成功。
相關文章
相關標籤/搜索