IT Research Department @BKAP 2015 Page 1 / 11Lab 05 JAX-WS Mục tiêu - Xây dựng CRUD Web Service với JAX - WS Phần I Bài tập step by step Bài 1 Xây dựng các chức năng cơ bản GetAll,
Trang 1IT Research Department @BKAP 2015 Page 1 / 11
Lab 05 JAX-WS
Mục tiêu
- Xây dựng CRUD Web Service với JAX - WS
Phần I Bài tập step by step
Bài 1
Xây dựng các chức năng cơ bản GetAll, Insert, Update, Delete bảng AccountBank trong
cơ sở dữ liệu BankDB
AccountBank
Step 1: Xây dựng các procedure:
GetAllAccount
Trang 2 InsertAccount
UpdateAccount
DeleteAccount
Trang 3IT Research Department @BKAP 2015 Page 3 / 11
Step 2: Xây dựng Web Application CRUDWS
Step 3: Xây dựng Web Service CRUDAccount
Trang 4 CRUDWS RC New Other Web Services Web Service
Trang 5IT Research Department @BKAP 2015 Page 5 / 11
Step 4: Tạo lớp kết nối ConnectionDB.java trong package bkap.util
package bkap.util;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author Quang
*/
public class ConnectionDB {
private static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=BankDB";
private static final String DB_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String user_db = "sa";
private static final String pass_db = "Gacon1984";
public static Connection openConnection() {
Connection conn = null;
try {
Class.forName( DB_DRIVER);
conn = DriverManager.getConnection( URL, user_db, pass_db);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void closeConnection(Connection conn, CallableStatement callableSt) {
try {
if (conn != null) {
conn.close();
}
if (callableSt != null) {
callableSt.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Step 5: Tạo lớp Account.java trong package entity
package entity;
import java.util.Date;
/**
*
* @author Quang
Trang 6*/
public class Account {
private int accountId;
private String accNo;
private String accName;
private Date createDate;
private float availableBalance;
private boolean status;
public int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}
public String getAccNo() {
return accNo;
}
public void setAccNo(String accNo) {
this.accNo = accNo;
}
public String getAccName() {
return accName;
}
public void setAccName(String accName) {
this.accName = accName;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public float getAvailableBalance() {
return availableBalance;
}
public void setAvailableBalance(float availableBalance) {
this.availableBalance = availableBalance;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
Trang 7IT Research Department @BKAP 2015 Page 7 / 11
}
Step 6: Tạo các dịch vụ web Service
CRUDAccount.java
package bkap.ws;
import bkap.util.ConnectionDB;
import entity.Account;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
/**
*
* @author Quang
*/
@WebService(serviceName = "CRUDAccount")
public class CRUDAccount {
/**
* Web service operation
*
* @return
*/
@WebMethod(operationName = "GetAllAccount")
public List<Account> GetAllAccount() {
Connection conn = null;
CallableStatement callSt = null;
List<Account> listAcc = new ArrayList<>();
try {
conn = ConnectionDB.openConnection();
callSt = conn.prepareCall("{call GetAllAccount()}");
ResultSet rs = callSt.executeQuery();
while (rs.next()) {
Account acc = new Account();
acc.setAccountId(rs.getInt("AccountId"));
acc.setAccNo(rs.getString("AccNo"));
acc.setAccName(rs.getString("AccName"));
acc.setCreateDate(rs.getDate("CreatedDate"));
acc.setAvailableBalance(rs.getFloat("AvailableBalance"));
acc.setStatus(rs.getBoolean("Status"));
listAcc.add(acc);
}
} catch (Exception e) {
Trang 8e.printStackTrace();
}
return listAcc;
}
/**
* Web service operation
*
* @param accNo
* @param accName
* @param createDate
* @param availableBalance
* @param status
* @return
*/
@WebMethod(operationName = "InsertAccount")
public String InsertAccount(@WebParam(name = "accNo") String accNo, @WebParam(name = "accName") String accName, @WebParam(name = "createDate") String createDate, @WebParam(name = "availableBalance") float availableBalance, @WebParam(name = "status") boolean status) {
Connection conn = null;
CallableStatement callSt = null;
try {
conn = ConnectionDB.openConnection();
callSt = conn.prepareCall("{call InsertAccount(?,?,?,?,?)}");
callSt.setString(1, accNo);
callSt.setString(2, accName);
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date parsed = format.parse(createDate);
java.sql.Date sql = new java.sql.Date(parsed.getTime());
callSt.setDate(3, sql);
callSt.setFloat(4, availableBalance);
callSt.setBoolean(5, status);
callSt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
return "error";
}
return "success";
}
/**
* Web service operation
*
* @param accountId
* @param accNo
* @param accName
* @param createDate
* @param availableBalance
* @param status
* @return
*/
@WebMethod(operationName = "UpdateAccount")
public String Update(@WebParam(name = "accountId") int accountId, @WebParam(name = "accNo") String accNo,
@WebParam(name = "accName") String accName, @WebParam(name = "createDate") String createDate,
@WebParam(name = "availableBalance") float availableBalance, @WebParam(name = "status") boolean status) { Connection conn = null;
CallableStatement callSt = null;
Trang 9IT Research Department @BKAP 2015 Page 9 / 11
try {
conn = ConnectionDB.openConnection();
callSt = conn.prepareCall("{call UpdateAccount(?,?,?,?,?,?)}");
callSt.setInt(1, accountId);
callSt.setString(2, accNo);
callSt.setString(3, accName);
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date parsed = format.parse(createDate);
java.sql.Date sql = new java.sql.Date(parsed.getTime());
callSt.setDate(4, sql);
callSt.setFloat(5, availableBalance);
callSt.setBoolean(6, status);
callSt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
return "error";
}
return "success";
}
/**
* Web service operation
*
* @param accountId
* @return
*/
@WebMethod(operationName = "DeleteAccount")
public String DeleteAccount(@WebParam(name = "accountId") int accountId) {
Connection conn = null;
CallableStatement callSt = null;
try {
conn = ConnectionDB.openConnection();
callSt = conn.prepareCall("{call DeleteAccount(?)}");
callSt.setInt(1, accountId);
callSt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
return "error";
}
return "success";
}
}
Step 7: Build, Deploy and Test Web Service
Trang 11IT Research Department @BKAP 2015 Page 11 / 11