以下 demo 的資料庫是用 SQL Server 所以要先去下載適當的 JDBC Driver
下載下來後將JAR檔放到 Tomcat 5.5 安裝目錄下的 common\lib 裡面
接著要設定資料庫連線的參數
一樣開啟 Tomcat 5.5 安裝目錄下的 webapps\axis2\WEB-INF\conf\axis2.xml 檔案
並將以下相關參數寫入
<parameter name="DB_driverClassName">com.microsoft.sqlserver.jdbc.SQLServerDriver</parameter>
<parameter name="DB_url">jdbc:sqlserver://xxx.xxx.xx.xxx:1400;DatabaseName=test</parameter>
<parameter name="DB_username">XXXX</parameter>
<parameter name="DB_password">123456</parameter>
完成之後建立一個 Server 端的程式 開啟 Eclipse
左上角的 File →New→ Java Project ,在Project name 輸入 WS2MSSQL,並按下 next
完成後第一步要先將 Axis2 的 library 匯入
接著要建立資料庫的連線 首先建立一個新的 Java class
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.engine.ServiceLifeCycle;
public class DBtestServiceLifeCycle implements ServiceLifeCycle {
public static final String DB_CONNECTION = "connKey";
public void shutDown(ConfigurationContext configctx, AxisService service) {
// TODO Auto-generated method stub
Connection conn = (Connection) configctx.getProperty(DB_CONNECTION);
if (conn != null) {
try {
// 關閉資料庫連線
conn.close();
} catch (SQLException e) {
System.out.println("Error while closing the DB connection");
}
}
}
public void startUp(ConfigurationContext configctx, AxisService service) {
// TODO Auto-generated method stub
// 從axis2.xml讀取四個連線參數
String DB_driverClassName =
(String) service.getAxisConfiguration().getParameterValue("DB_driverClassName");
String DB_url =
(String) service.getAxisConfiguration().getParameterValue("DB_url");
String DB_username =
(String) service.getAxisConfiguration().getParameterValue("DB_username");
String DB_password =
(String) service.getAxisConfiguration().getParameterValue("DB_password");
try {
Class.forName(DB_driverClassName);
Connection conn = DriverManager.getConnection(DB_url, DB_username,DB_password);
configctx.setProperty(DB_CONNECTION, conn);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
資料連接部分完成後 這個 Demo 理的資料庫有一個 Table:t_dept
所以接下來先建立一個 Java bean
接著建立存取資料庫的 DAO 首先先建立 DAOBase 這個抽象類別
接著是 DeptDAO
最後在撰寫以下 services.xml
在 project 目錄下建立一個 META-INF 的資料夾
完成後同樣打包成 war 或 aar 檔 並儲存
接著開啟 Tomcat 5.5 將剛剛這包 war 或 aar 檔上傳
過程不會的話可以看這篇 【Axis2】Java Web Services 範例
所以接下來先建立一個 Java bean
import java.util.Date;
public class Dept
{
private String deptid;
private String dept_cname;
private String dept_fullname;
private String dept_no;
//省略 getter/setter
}
接著建立存取資料庫的 DAO 首先先建立 DAOBase 這個抽象類別
import java.sql.Connection;
import org.apache.axis2.context.MessageContext;
public abstract class DAOBase
{
protected Connection getConnByAxis2RuntimeConfig(){
return (Connection) MessageContext.getCurrentMessageContext().getProperty(
DBtestServiceLifeCycle.DB_CONNECTION);
}
}
接著是 DeptDAO
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class DeptDAO extends DAOBase
{
public String QueryById(String deptId)
{
String deptName = null;
Connection connection = getConnByAxis2RuntimeConfig();
if(connection !=null) {
String SQL = "SELECT dept_cname FROM t_dept WHERE deptid = '" + deptId + "'";
try {
PreparedStatement statement = connection.prepareStatement(SQL);
ResultSet rs = statement.executeQuery();
if(rs.next()) {
deptName = rs.getString("dept_cname");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return deptName;
}
public Dept[] listAllDept()
{
ArrayList list = new ArrayList();
Dept dept = null;
Connection connection = getConnByAxis2RuntimeConfig();
if(connection !=null) {
String SQL = "SELECT * FROM t_dept";
try{
PreparedStatement statement = connection.prepareStatement(SQL);
ResultSet rs = statement.executeQuery();
while(rs.next()){
dept = new Dept();
dept.setDeptid(rs.getString("deptid"));
dept.setDept_cname(rs.getString("dept_cname"));
dept.setDept_fullname(rs.getString("dept_fullname"));
dept.setDept_no(rs.getString("dept_no"));
list.add(dept);
}
}catch(SQLException e){
e.printStackTrace();
}
}
return (Dept[])list.toArray(new Dept[list.size()]);
}
}
接下來建立一個 Service public class DeptWSevices
{
private DeptDAO deptDao;
public DeptWSevices(){
deptDao = new DeptDAO();
}
public String getDeptById(String deptId){
return this.deptDao.QueryById(deptId);
}
public Dept[] listAllDept(){
return this.deptDao.listAllDept();
}
}
最後在撰寫以下 services.xml
在 project 目錄下建立一個 META-INF 的資料夾
<service name="DBSampleService" class="DBtestServiceLifeCycle">
<description>
DB sample for Web
Service
</description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"
/>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"
/>
</messageReceivers>
<parameter name="ServiceClass">
DeptWSevices
</parameter>
</service>
完成後同樣打包成 war 或 aar 檔 並儲存
接著開啟 Tomcat 5.5 將剛剛這包 war 或 aar 檔上傳
過程不會的話可以看這篇 【Axis2】Java Web Services 範例
上傳完成並無錯誤後就可以撰寫一個簡單的 Client 進行測試
同樣的 Client 端的細節可以看上一篇
public static void main(String[] args) throws RemoteException {
// TODO Auto-generated method stub
DBSampleServiceStub stub = new DBSampleServiceStub();
ListAllDeptResponse response = stub.listAllDept(); //這裡呼叫了 listAllDept method
Dept[] depts = response.get_return();
for(Dept d : depts){
System.out.println("dept : " + d.getDept_cname());
}
}
沒有留言:
張貼留言