Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 12 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
12
Dung lượng
884,61 KB
Nội dung
Enterprise Application Development in Java EE Lab03 - 04Web Service Endpoints Designing Web Service Clients Mục tiêu - Xây dựng web service toán - Xây dựng Client test dịch vụ Phần I Bài tập step by step Bài Cho sở liệu toán ngân hàng BankDB AccountBank IT Research Department @BKAP 2015 Page / 12 Enterprise Application Development in Java EE Merchant TransactionBank Xây dựng dịch vụ: CheckOrder: Kiểm tra tính xác tài khoản, số dư khả dụng tk khách hàng FinishOrder: Thực toán cho khách hàng, trừ số tiền tài khoản khách hàng cộng vào tài khoản toán Xây dựng client thực test dịch vụ IT Research Department @BKAP 2015 Page / 12 Enterprise Application Development in Java EE Step 1: Xây dựng procedure thực nghiệp vụ dịch vụ USE [BankDB] GO ============================================ Author: Quangnd Create date: 20161109 Description: Dich vu CheckOrder ============================================= CREATE PROCEDURE [dbo].[CheckOrder] @merchantName nvarchar(50), @accNo nvarchar(30), @accountName nvarchar(50), @paymentAmount float, @returncode char(2) OUTPUT, @transactionId int OUTPUT AS BEGIN Kiem tra tat ca dieu kien xem co toan duoc hay khong Declare @checkMerchant int; Declare @merchantId int; Declare @accountid int; Select @checkMerchant = count(*) from Merchant m where m.MerchantName = @merchantName and m.Status = 1; If @checkMerchant > BEGIN lay merchantid tu @merchantName truyen vao Select @merchantId = m.MerchantId from Merchant m; Declare @checkAcc int; Kiem tra tai khoan co ton tai hay khong Select @checkAcc = count(*) from AccountBank a where a.AccName = @accountName and a.AccNo = @accNo and a.Status = 1; if @checkAcc>0 BEGIN Tai khoan ton tai > Kiem tra so du kha dung cua tai khoan co du toan hay khong Lay ma tai khoan Select @accountid = a.AccountId from AccountBank a; Kiem tra so du kha dung Declare @checkAvailableBalance int; Select @checkAvailableBalance = count(*) from AccountBank a where a.AccountId = @accountid and a.AvailableBalance>@paymentAmount; if @checkAvailableBalance>0 set @returncode = '00'; else set @returncode = '03'; END else set @returncode = '02'; END else Set @returncode = '01'; Neu dieu kien thoa man, tao ban ghi transaction if @returncode = '00' IT Research Department @BKAP 2015 Page / 12 Enterprise Application Development in Java EE BEGIN Insert into TransactionBank values(@merchantId,@accountid,@paymentAmount,GETDATE(),N'Thanh toán cho trang web rau sạch',0); Commit; Declare @maxTrans int; Select @maxTrans = max(t.TransachtionId) from TransactionBank t; Set @transactionId = @maxTrans; END else Set @transactionId = 0; END FinishOrder USE [BankDB] GO ============================================= Author: Quangnd Create date: 20161109 Description: Dich vu FinishOrder ============================================= CREATE PROCEDURE [dbo].[FinishOrder] @transactionId int, @merchantName nvarchar(50), @returnCode char(2) OUTPUT AS BEGIN Kiem tra co transaction khong Declare @checkTransaction int; Declare @accountIdKH int; Declare @accountIdWeb int; lay tai khoan cua trang web Select @accountIdWeb = m.AccountId from Merchant m where m.MerchantName = @merchantName; Declare @paymentAmount float; Select @checkTransaction = count(*) from TransactionBank t where t.TransachtionId = @transactionId; if @checkTransaction>0 BEGIN Select @paymentAmount = t.BalancePayment from TransactionBank t where t.TransachtionId = @transactionId; Select @accountIdKH = t.AccountId from TransactionBank t where t.TransachtionId = @transactionId; Kiem tra lai so du kha dung Declare @checkSDKD int; Select @checkSDKD = count(*) from AccountBank a where a.AccountId = @accountIdKH and a.AvailableBalance>@paymentAmount; if @checkSDKD>0 BEGIN Cong vao so du tk Web Declare @amountWeb float; Declare @updateWeb float; Select @amountWeb = a.AvailableBalance from AccountBank a where a.AccountId = @accountIdWeb; IT Research Department @BKAP 2015 Page / 12 Enterprise Application Development in Java EE Set @updateWeb = @amountWeb + @paymentAmount; Update AccountBank Set AvailableBalance = @updateWeb Where AccountId = @accountIdWeb; Tru vao so du tk KH Declare @amountKH float; Declare @updateKH float; Select @amountKH = a.AvailableBalance from AccountBank a where a.AccountId = @accountIdKH; Set @updateKH = @amountKH - @paymentAmount; Update AccountBank Set AvailableBalance = @updateKH Where AccountId = @accountIdKH; Cap nhat trang thai cua Transaction Update TransactionBank Set Status = Where TransachtionId = @transactionId; Tra ve returncode Set @returnCode = '00' END Else So du khong du de toan set @returnCode = '02'; END Else Khong ton tai transaction set @returnCode = '01'; END Step 2: Tạo Web Application File New Project Java Web Web Application Project Name: PaymentApp Server: GlassFish Server Step 3: Tạo lớp java ConnectionDB.java để kết nối sở liệu PaymentApp New Other Java Java Class Class Name: ConnectionDB Package: util ConnectionDB.java IT Research Department @BKAP 2015 Page / 12 Enterprise Application Development in Java EE package util; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; /** * * @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); } catch (ClassNotFoundException ex) { Logger.getLogger(ConnectionDB.class.getName()).log(Level.SEVERE, null, ex); } try { conn = DriverManager.getConnection(URL, user_db, pass_db); } catch (SQLException ex) { Logger.getLogger(ConnectionDB.class.getName()).log(Level.SEVERE, null, ex); } return conn; } public static void closeConnection(Connection conn, CallableStatement callableSt) { if (conn != null) { try { conn.close(); } catch (SQLException ex) { Logger.getLogger(ConnectionDB.class.getName()).log(Level.SEVERE, null, ex); } } if (callableSt != null) { try { callableSt.close(); } catch (SQLException ex) { Logger.getLogger(ConnectionDB.class.getName()).log(Level.SEVERE, null, ex); } } } } Step 4: Tạo Web Service PaymentWebService PaymentApp New Other WebServices Web Service IT Research Department @BKAP 2015 Page / 12 Enterprise Application Development in Java EE Web Service Name: PaymentWebService Package: ws Chọn Create Web Service from Scratch Tạo dịch vụ cho PaymentWebService CheckOrder FinishOrder PaymentWebService.java package ws; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.Types; import javax.jws.WebService; import javax.jws.WebMethod; import util.ConnectionDB; /** * * @author Quang */ @WebService(serviceName = "PaymentWebservice") public class PaymentWebservice { /** * Web service operation * * @param merchantName * @param accNo * @param accName * @param paymentAmount * @return */ @WebMethod(operationName = "checkOrder") public String checkOrder(String merchantName, String accNo, String accName, float paymentAmount) { Connection conn = null; String returnSt = ""; CallableStatement callableSt = null; try { conn = ConnectionDB.openConnection(); callableSt = conn.prepareCall("{call CheckOrder(?,?,?,?,?,?)}"); callableSt.setString(1, merchantName); callableSt.setString(2, accNo); callableSt.setString(3, accName); callableSt.setFloat(4, paymentAmount); IT Research Department @BKAP 2015 Page / 12 Enterprise Application Development in Java EE callableSt.registerOutParameter(5, Types.CHAR); callableSt.registerOutParameter(6, Types.INTEGER); callableSt.execute(); String returnCode = callableSt.getString(5); int transactionId = callableSt.getInt(6); returnSt = returnCode + "#" + transactionId; } catch (Exception e) { e.printStackTrace(); } return returnSt; } public String finishOrder(int transactionid, String merchantName) { Connection conn = null; String returnSt = ""; CallableStatement callableSt = null; try { conn = ConnectionDB.openConnection(); callableSt = conn.prepareCall("{call FinishOrder(?,?,?)}"); callableSt.setInt(1, transactionid); callableSt.setString(2, merchantName); callableSt.registerOutParameter(3, Types.CHAR); callableSt.execute(); returnSt = callableSt.getString(3); } catch (Exception e) { e.printStackTrace(); } return returnSt; } } Ứng dụng sau hoàn thành Step 5: Build, Deploy and Test Web Service IT Research Department @BKAP 2015 Page / 12 Enterprise Application Development in Java EE Chuẩn bị liệu Test Test Web Service IT Research Department @BKAP 2015 Page / 12 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page 10 / 12 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page 11 / 12 Enterprise Application Development in Java EE Phần II Bài Tập Tự Làm Bài 2: Xây dựng ứng dụng thực toán chuyển khoản để test dịch vụ web service xây dựng IT Research Department @BKAP 2015 Page 12 / 12 ... @accountIdKH and a.AvailableBalance>@paymentAmount; if @checkSDKD>0 BEGIN Cong vao so du tk Web Declare @amountWeb float; Declare @updateWeb float; Select @amountWeb = a.AvailableBalance from AccountBank... @accountIdWeb; IT Research Department @BKAP 2015 Page / 12 Enterprise Application Development in Java EE Set @updateWeb = @amountWeb + @paymentAmount; Update AccountBank Set AvailableBalance = @updateWeb... Other Web Services Web Service IT Research Department @BKAP 2015 Page / 12 Enterprise Application Development in Java EE Web Service Name: PaymentWebService Package: ws Chọn Create Web