1. Trang chủ
  2. » Công Nghệ Thông Tin

Thực hành Web Services Lab 5

11 167 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 11
Dung lượng 1,02 MB

Nội dung

Enterprise Application Development in Java EE 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 Xây dựng chức GetAll, Insert, Update, Delete bảng AccountBank sở liệu BankDB  AccountBank Step 1: Xây dựng procedure:  GetAllAccount IT Research Department @BKAP 2015 Page / 11 Enterprise Application Development in Java EE  InsertAccount  UpdateAccount  DeleteAccount IT Research Department @BKAP 2015 Page / 11 Enterprise Application Development in Java EE Step 2: Xây dựng Web Application CRUDWS Step 3: Xây dựng Web Service CRUDAccount IT Research Department @BKAP 2015 Page / 11 Enterprise Application Development in Java EE  CRUDWS  RC  New  Other  Web ServicesWeb Service IT Research Department @BKAP 2015 Page / 11 Enterprise Application Development in Java EE Step 4: Tạo lớp kết nối ConnectionDB.java 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 package entity package entity; import java.util.Date; /** * * @author Quang IT Research Department @BKAP 2015 Page / 11 Enterprise Application Development in Java EE */ 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; } IT Research Department @BKAP 2015 Page / 11 Enterprise Application Development in Java EE } Step 6: Tạo 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 GetAllAccount() { Connection conn = null; CallableStatement callSt = null; List 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) { IT Research Department @BKAP 2015 Page / 11 Enterprise Application Development in Java EE e.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; IT Research Department @BKAP 2015 Page / 11 Enterprise Application Development in Java EE 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 IT Research Department @BKAP 2015 Page / 11 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page 10 / 11 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page 11 / 11 ... createDate; } public float getAvailableBalance() { return availableBalance; } public void setAvailableBalance(float availableBalance) { this.availableBalance = availableBalance; } public boolean... InsertAccount(@WebParam(name = "accNo") String accNo, @WebParam(name = "accName") String accName, @WebParam(name = "createDate") String createDate, @WebParam(name = "availableBalance") float availableBalance,... "accNo") String accNo, @WebParam(name = "accName") String accName, @WebParam(name = "createDate") String createDate, @WebParam(name = "availableBalance") float availableBalance, @WebParam(name = "status")

Ngày đăng: 07/05/2018, 16:45

TỪ KHÓA LIÊN QUAN

w