JSF – làm việc với cơ sở dữ liệu

21 545 1
JSF – làm việc với cơ sở dữ liệu

Đ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

JSF – Làm việc với CƠ SỞ DỮ LIỆU JSF – Làm việc với CƠ SỞ DỮ LIỆU Trong này, làm việc với JSF kết nối sở liệu sử dụng JDBC thể liệt kê bảng liệu, thêm mẫu tin mới, cập nhật thông tin dòng liệu xóa dòng liệu Giả sử Database thiết kế sau: Sau hoàn tất, ta có giao diện sau: Khi nhấn link “Insert New” cho ta trang thêm Lớp học Khi nhấn Link Edit, load thông tin dòng chọn cho hiệu chỉnh Khi nhấn link Delete, xóa dòng chọn Trong ví dụ dùng NetBeans IDE để thiết kế (Eclipse hay IDE khác vậy) Bước 1: Tạo Web Application mới, chọn Framework Java Server Faces hình Bước 2: Tạo Manage-bean 001 package vovanhai.wordpress.com; 002 003 import java.sql.Connection; 004 import java.sql.DriverManager; 005 import java.sql.PreparedStatement; 006 import java.sql.ResultSet; 007 import java.util.ArrayList; 008 import javax.faces.component.UIComponent; 009 import javax.faces.component.html.HtmlDataTable; 010 import javax.faces.event.ActionEvent; 011 012 /** 013 * 014 * @author VoVanHai 015 */ 016 public class LophocBean { 017 018 private String msLop; 019 private String tenLop; 020 private String tenGVCN; 021 022 /** Creates a new instance of LophocBean */ 023 public LophocBean() { } 024 025 026 public LophocBean(String msLop, String tenLop, String tenGVCN) { 027 028 029 030 this.msLop = msLop; this.tenLop = tenLop; this.tenGVCN = tenGVCN; } 031 032 public String getMsLop() { 033 034 return msLop; } 035 036 037 038 public void setMsLop(String msLop) { this.msLop = msLop; } 039 040 public String getTenGVCN() { 041 042 return tenGVCN; } 043 044 public void setTenGVCN(String tenGVCN) { 045 046 } this.tenGVCN = tenGVCN; 047 048 public String getTenLop() { 049 050 return tenLop; } 051 052 053 054 public void setTenLop(String tenLop) { this.tenLop = tenLop; } 055 056 /** 057 058 * Liệt kê record bảng lớp học để hiển thị lên table * @return 059 */ 060 public ArrayList getAllLophoc() { 061 ArrayList lst = new ArrayList(); 062 try { 06 iver"); Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDr Connection = 06 DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databas eName=GC0563", "sa", ""); 065 ResultSet rs = con.createStatement().executeQuery("select * from Lophoc"); 066 while (rs.next()) { 067 LophocBean lh = new LophocBean( rs.getString(1), 068 069 070 rs.getString(2), rs.getString(3)); 071 072 lst.add(lh); } 073 con.close(); 074 } catch (Exception e) { 075 076 e.printStackTrace(); } 077 078 079 return lst; } 080 081 082 /** * Action dùng để insert mẫu tin 083 084 085 * @return success thành công */ public String InsertNew() { 086 try { 08 iver"); Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDr Connection = 08 DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databas eName=GC0563", "sa", ""); 089 090 String sql = "insert into lophoc values(?,?,?)"; PreparedStatement ps = con.prepareStatement(sql); 091 092 ps.setString(1, msLop); ps.setString(2, tenLop); 093 ps.setString(3, tenGVCN); 094 int x = ps.executeUpdate(); 095 if (x > 0) { 096 return "success"; 097 } 098 } catch (Exception e) { 099 100 e.printStackTrace(); } 101 102 return "failed"; } 103 104 /** 105 106 * Action delete dùng để xóa mẫu tin chọn * @param evt 107 */ 108 public void deleteAction(ActionEvent evt) { 109 // We get the table object HtmlDataTable table = getParentDatatable((UIComponent) 110 evt.getSource()); 111 112 // We get the object on the selected line Object o = table.getRowData(); 113 LophocBean selLh = (LophocBean) o; 114 try { 11 iver"); Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDr Connection = 11 DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databas eName=GC0563", "sa", ""); 117 118 String sql = "delete lophoc where msLop=?"; PreparedStatement ps = con.prepareStatement(sql); 119 120 ps.setString(1, selLh.msLop); ps.executeUpdate(); 121 } catch (Exception e) { e.printStackTrace(); 122 123 124 } } 125 126 /** 127 128 * Action dùng để chọn mẫu tin cần cập nhật * @param evt 129 */ 130 public void editBook(ActionEvent evt) { 131 // We get the table object HtmlDataTable table = getParentDatatable((UIComponent) 132 evt.getSource()); 133 134 // We get the object on the selected line Object o = table.getRowData(); 135 136 LophocBean lh = (LophocBean) o; this.msLop = lh.msLop; 137 138 this.tenLop = lh.tenLop; this.tenGVCN = lh.tenGVCN; 139 } 140 141 // Method to get the HtmlDataTable 142 private HtmlDataTable getParentDatatable(UIComponent compo) { 143 if (compo == null) { 144 return null; 145 } 146 if (compo instanceof HtmlDataTable) { 147 return (HtmlDataTable) compo; 148 } 149 150 return getParentDatatable(compo.getParent()); } 151 152 /** 153 154 * Cập nhật liệu chọn lên sở liệu * @return 155 */ 156 public String update() { 15 try { 15 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDr iver"); Connection = 15 DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databas eName=GC0563", "sa", ""); 16 String sql = "update lophoc set tenLop=?, tenGVCN=? where msLop=?"; 161 162 PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, tenLop); 163 164 ps.setString(2, tenGVCN); ps.setString(3, msLop); 165 int x = ps.executeUpdate(); 166 if (x > 0) { 167 return "success"; 168 } 169 170 } catch (Exception e) { e.printStackTrace(); 171 } 172 return "failed"; 173 174 } } Bước 3: Thiết kế giao diện Tạo trang liệt kê danh sách lớp học: Lophoc.jsp 01 02 03 04 05 06 07 08 09 10 12 JSP Page 11 13 14 15 DANH SÁCH LỚP HỌC 16 17 21 22 ã số Lớp"/> 25 31 /> 39 - 27 28 [...]... tôi có 2 trang Trên trang 1 tôi hiển thị dữ liệu từ bean được thiết lập là SessionScoped, từ trang 1 chuyển qua trang 2 để thực hiện thao tác dữ liệu Nhưng sau khi thay đổi dữ liệu thành công và database đã thay đổi nhưng khi quay về trang 1 dữ liệu vẫn ko thay đổi Nếu dùng ViewScoped thì ko thể chuyển dữ liệu qua trang 2 được hoặc a có cách nào giúp chuyển dữ liệu qua trang 2 từ trang 1 ko (trang 2... tôi có 2 trang Trên trang 1 tôi hiển thị dữ liệu từ bean được thiết lập là SessionScoped, từ trang 1 chuyển qua trang 2 để thực hiện thao tác dữ liệu Nhưng sau khi thay đổi dữ liệu thành công và database đã thay đổi nhưng khi quay về trang 1 dữ liệu vẫn ko thay đổi Nếu dùng ViewScoped thì ko thể chuyển dữ liệu qua trang 2 được hoặc a có cách nào giúp chuyển dữ liệu qua trang 2 từ trang 1 ko (trang 2... the first to like this 22 Responses to JSF – Làm việc với CƠ SỞ DỮ LIỆU” 1 Sgboy17 said May 4, 2011 at 2:22 am Chào anh, e nghĩ mãi không ra cách sắp xếp để thêm phần search book Vấn đề chính là sau khi nhấn vào nút search, sẽ chuyển qua trang kết quả Nhưng không thể truyền tham số search về cho server để thực thi (thuộc tính value của chưa nhận được giá trị với sql query có where) 2 Đỉnh said May... những gói thư viện nào để có thể làm các vd liên quan tới các tag 04 05 06 07 08 09 10 ... trình thưởng thức Chúc thành công! Like Be the first to like this 22 Responses to JSF – Làm việc với CƠ SỞ DỮ LIỆU” Sgboy17 said May 4, 2011 at 2:22 am Chào anh, e nghĩ không cách xếp để thêm... 1:36 pm thư viện chuẩn JSF jsf-api-….jar jsf- impl-…jar, thẻ thẻ richfaces, bạn tìm hiểu thêm 15 Võ Văn Hải said November 10, 2011 at 1:54 pm “Em chào thầy, em làm JSF, để làm ví dụ em cần phải... e){e.printStackTrace();} return con; } 10 JSF said November 4, 2011 at 5:58 am Em chào thầy, em làm JSF, để làm ví dụ em cần phải có thư viện nòa MOng thầy cung cấp cho em Em xin cảm ơn thầy 11 JSF said November 4,

Ngày đăng: 18/01/2016, 19:12

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan