Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 15 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
15
Dung lượng
1,09 MB
Nội dung
Enterprise Application Development in Java EE Lab08 Transactions Mục tiêu - Cấu hình JDBC Connection Pool GlassFish Server - Tạo thực thi Session Bean với Bean-Managed Transasction Phần I Bài tập step by step Bài 5.1 Xây dựng ứng dụng chuyển tiền tài khoản Step 1: Tạo Database Banking Tạo bảng Accounts có trường AccountNo: Nvarchar(10), Primary Key Balance: Numeric(20,2) Tạo ghi bảng Accounts IT Research Department @BKAP 2015 Page / 15 Enterprise Application Development in Java EE Step 2: Copy sqljdbc4.jar vào thư mục lib server glassfish Step 3: Tạo Connection Pool Netbean Services Servers RC GlassFish Server View Domain Admin Console Resources JDBC JDBC Connection Pools New IT Research Department @BKAP 2015 Page / 15 Enterprise Application Development in Java EE Test Connection Pool BankingPool IT Research Department @BKAP 2015 Page / 15 Enterprise Application Development in Java EE Step 4: Tạo JDBC Resources Step 5: Tạo Enterprise Application File New Project Java EE Enterprise Application IT Research Department @BKAP 2015 Page / 15 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page / 15 Enterprise Application Development in Java EE Step 6: Tạo Stateless Session Bean package beanpack PaymentApplication-ejb New Other Enterprise JavaBeans Session Bean IT Research Department @BKAP 2015 Page / 15 Enterprise Application Development in Java EE AccountBean.java package beanpack; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.ejb.Stateless; import javax.ejb.LocalBean; import javax.ejb.TransactionManagement; import javax.ejb.TransactionManagementType; import javax.sql.DataSource; import javax.transaction.SystemException; import javax.transaction.UserTransaction; /** * * @author Quang */ @Stateless @LocalBean @TransactionManagement(value = TransactionManagementType.BEAN) public class AccountBean { IT Research Department @BKAP 2015 Page / 15 Enterprise Application Development in Java EE //Khai bao Connection private Connection conn; //khai bao datasource @Resource(mappedName = "jdbc/Banking") private DataSource dataSource; //Khai bao User Transaction @Resource private UserTransaction userTransaction; //Phuong thuc khoi tao connection @PostConstruct public void initializeResources() { System.out.println("PostConstruct callback"); try { conn = dataSource.getConnection(); } catch (SQLException ex) { Logger.getLogger(AccountBean.class.getName()).log(Level.SEVERE, null, ex); } } //Phuong thuc kiem tra so tai khoan co ton tai hay khoong public boolean confirmAccount(String accountNo) { //Khai bao bien kiem tra so tai khoan boolean validAcc = false; if (accountNo != null && !accountNo.isEmpty()) { try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT AccountNo FROM Accounts WHERE AccountNo = '" + accountNo + "'"); if (rs.next()) { validAcc = true; } rs.close(); } catch (SQLException ex) { Logger.getLogger(AccountBean.class.getName()).log(Level.SEVERE, null, ex); throw new javax.ejb.EJBException(ex.getMessage()); } } return validAcc; } //Phuong thuc chuyen khoan public void transferFund(String fromAcc, double balance, String toAacc) { try { userTransaction.begin(); if (confirmAccount(fromAcc) && confirmAccount(toAacc)) { Statement stmt = null; stmt = conn.createStatement(); //Tru tien tai khoan chuyen stmt.executeUpdate("UPDATE Accounts SET Balance = Balance - " + balance + " WHERE AccountNo = '" + fromAcc + "'"); //Cong tien tai khoan duoc chuyen stmt.executeUpdate("UPDATE Accounts SET Balance = Balance + " + balance + " WHERE AccountNo = '" + toAacc + "'"); userTransaction.commit(); } else { throw new SQLException("Invalid Account Number"); IT Research Department @BKAP 2015 Page / 15 Enterprise Application Development in Java EE } } catch (Exception e) { try { userTransaction.rollback(); } catch (IllegalStateException ex) { Logger.getLogger(AccountBean.class.getName()).log(Level.SEVERE, null, ex); } catch (SecurityException ex) { Logger.getLogger(AccountBean.class.getName()).log(Level.SEVERE, null, ex); } catch (SystemException ex) { Logger.getLogger(AccountBean.class.getName()).log(Level.SEVERE, null, ex); } } } } Step 7: Tạo trang PaymentForm.jsp PaymentApplication-war Web Pages New Other Web JSP PaymentForm.jsp Payment Form Payment Form From Account Number: Amount to be transferred: To Account Number: Step 8: Tạo Servlet tên FundTransferServlet package servlet PaymentApplication-war Source Packages New Other Servlet IT Research Department @BKAP 2015 Page / 15 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page 10 / 15 Enterprise Application Development in Java EE FundTransferServlet.java package servlet; import beanpack.AccountBean; import java.io.IOException; import java.io.PrintWriter; import javax.ejb.EJB; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author Quang */ public class FundTransferServlet extends HttpServlet { //goi Enterprise Session Bean @EJB private AccountBean accountBean; /** * Processes requests for both HTTP GET and POST * methods * * @param request servlet request IT Research Department @BKAP 2015 Page 11 / 15 Enterprise Application Development in Java EE * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); //Lay cac thong tin tren form String fromAcc = request.getParameter("fromAcc"); String balance = request.getParameter("balance"); String toAcc = request.getParameter("toAcc"); PrintWriter out = response.getWriter(); try { /* TODO output your page here You may use following sample code */ out.println(""); out.println(""); out.println(""); out.println("Servlet FundTransferServlet"); out.println(""); out.println(""); accountBean.transferFund(fromAcc, Double.valueOf(balance), toAcc); out.println("Fund Transferred Succcessfully"); } catch (Exception ex) { out.println("Error in transferring fund"); } finally { out.println(""); out.println(""); out.close(); } } // /** * Handles the HTTP GET method * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP POST method * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override IT Research Department @BKAP 2015 Page 12 / 15 Enterprise Application Development in Java EE protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// } Step 9: Đặt PaymentForm.jsp làm trang home web.xml FundTransferServlet servlet.FundTransferServlet FundTransferServlet /FundTransferServlet 30 PaymentForm.jsp IT Research Department @BKAP 2015 Page 13 / 15 Enterprise Application Development in Java EE Ứng dụng sau hoàn thành Step 10: Build and Run Application IT Research Department @BKAP 2015 Page 14 / 15 Enterprise Application Development in Java EE IT Research Department @BKAP 2015 Page 15 / 15 ... java. util.logging.Level; import java. util.logging.Logger; import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax .ejb. Stateless; import javax .ejb. LocalBean; import javax .ejb. TransactionManagement;... java. io.IOException; import java. io.PrintWriter; import javax .ejb. EJB; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;... Development in Java EE AccountBean .java package beanpack; import java. sql.Connection; import java. sql.ResultSet; import java. sql.SQLException; import java. sql.Statement; import java. util.logging.Level;