Bạn có thể viết bài mới.Bạn có thể “đặt hàng” bài viết.Mọi chi tiết xin mail về cho tác giả blog. Xin chân thành cảm ơn sự cộng tác của các bạn!
Đang xem: Chuyển file jsf sang word
Recent Posts
Archives
ArchivesSelect Month August 2011 (1) March 2011 (1) June 2010 (1) January 2010 (1) September 2009 (1) May 2009 (1) March 2009 (1) December 2008 (3) November 2008 (1) October 2008 (1) October 2007 (1)
Subscribe
Các phản hồi gần đây
Các trang được yêu thích
Đừng nghĩ bạn được bao nhiêu điểm. Đừng nghĩ bạn học ở trường nào. Đừng nghĩ bạn tốt nghiệp loại gì. Hãy nghĩ: MÌNH CÓ GÌ TRONG ĐẦU.
Pages
C#CTDLJ2EEEJBEJB 2.x với NetBeans 6.7.1 & JBoss AS 4.23GAEJB 3Java MailJMSRMIJavaLập trình java cănbảnBài tập chương4GUI applicationLập trình java nângcaoCác chủ đềkhácJDBC – Java DatabaseConnectivityJPA – Java PersistenceAPINetworkingMobile DevAndroid developmentMy GardenÂm nhạcSeminarMy ProjectsSEVisual BasicWebASP.NetHost ứng dụngwebJSF – Java ServerFaceJBoss RichFacesRichFaces: Logon và RegistrationapplicationJSPServlet programmingStrutsWeb servicesC# Web servicesJava Web servicesTạo Web services với JAX-WS 2.0 và Java SE 6PlatformXMLJXML – JSPXSLT Examples
Blog Stats
2,902,115 hits
Email Subscription
Enter your email address to subscribe to this blog and receive notifications of new posts by email.
Join 2,200 other followers
Email Address:
Sign me up!
JSF – Làm việc với CƠ SỞ DỮLIỆU
Xem thêm: Lịch Thi Đấu Sea Games 29 : U22 Việt Nam Dễ Thở? Lịch Thi Đấu Bóng Chuyền Sea Games 29
JSF – Làm việc với CƠ SỞ DỮ LIỆU
Trong bài này, chúng ta làm việc với JSF kết nối cơ sở dữ liệu sử dụng JDBC thể liệt kê 1 bảng dữ liệu, thêm mẫu tin mới, cập nhật thông tin của dòng dữ liệu cũng như xóa 1 dòng dữ liệu. Giả sử Database được thiết kế như sau:
Sau khi hoàn tất, ta có giao diện như sau:
Khi nhấn link “Insert New” sẽ cho ta trang thêm Lớp học mới
Khi nhấn Link Edit, sẽ load thông tin của dòng được chọn và cho hiệu chỉnh
Khi nhấn link Delete, sẽ xóa dòng được chọn.
Trong ví dụ này tôi dùng NetBeans IDE để thiết kế (Eclipse hay IDE khác cũng vậy)
Bước 1: Tạo 1 Web Application mới, chọn Framework là Java Server Faces như hình
Bước 2: Tạo Manage-bean
package hoanhtao3d.vn;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import javax.faces.component.UIComponent;import javax.faces.component.html.HtmlDataTable;import javax.faces.event.ActionEvent;/** * *
author hoanhtao3d.vn */public class LophocBean { private String msLop; private String tenLop; private String tenGVCN; /** Creates a new instance of LophocBean */ public LophocBean() { } public LophocBean(String msLop, String tenLop, String tenGVCN) { this.msLop = msLop; this.tenLop = tenLop; this.tenGVCN = tenGVCN; } public String getMsLop() { return msLop; } public void setMsLop(String msLop) { this.msLop = msLop; } public String getTenGVCN() { return tenGVCN; } public void setTenGVCN(String tenGVCN) { this.tenGVCN = tenGVCN; } public String getTenLop() { return tenLop; } public void setTenLop(String tenLop) { this.tenLop = tenLop; } /** * Liệt kê các record của bảng lớp học để hiển thị lên table *
return */ public ArrayList getAllLophoc() { ArrayList lst = new ArrayList(); try { Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”); Connection con = DriverManager.getConnection(“jdbc:sqlserver://localhost:1433;databaseName=GC0563”, “sa”, “”); ResultSet rs = con.createStatement().executeQuery(“select * from Lophoc”); while (rs.next()) { LophocBean lh = new LophocBean( rs.getString(1), rs.getString(2), rs.getString(3)); lst.add(lh); } con.close(); } catch (Exception e) { e.printStackTrace(); } return lst; } /** * Action dùng để insert 1 mẫu tin mới *
return success nếu thành công */ public String InsertNew() { try { Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”); Connection con = DriverManager.getConnection(“jdbc:sqlserver://localhost:1433;databaseName=GC0563”, “sa”, “”); String sql = “insert into lophoc values(?,?,?)”; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, msLop); ps.setString(2, tenLop); ps.setString(3, tenGVCN); int x = ps.executeUpdate(); if (x > 0) { return “success”; } } catch (Exception e) { e.printStackTrace(); } return “failed”; } /** * Action delete dùng để xóa mẫu tin được chọn *
param evt */ public void deleteAction(ActionEvent evt) { // We get the table object HtmlDataTable table = getParentDatatable((UIComponent) evt.getSource()); // We get the object on the selected line. Object o = table.getRowData(); LophocBean selLh = (LophocBean) o; try { Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”); Connection con = DriverManager.getConnection(“jdbc:sqlserver://localhost:1433;databaseName=GC0563”, “sa”, “”); String sql = “delete lophoc where msLop=?”; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, selLh.msLop); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } /** * Action dùng để chọn mẫu tin cần cập nhật *
Xem thêm: Top 11 Phần Mềm Chỉnh Sửa Video Chuyên Nghiệp Miễn Phí Tốt Nhất
param evt */ public void editBook(ActionEvent evt) { // We get the table object HtmlDataTable table = getParentDatatable((UIComponent) evt.getSource()); // We get the object on the selected line. Object o = table.getRowData(); LophocBean lh = (LophocBean) o; this.msLop = lh.msLop; this.tenLop = lh.tenLop; this.tenGVCN = lh.tenGVCN; } // Method to get the HtmlDataTable. private HtmlDataTable getParentDatatable(UIComponent compo) { if (compo == null) { return null; } if (compo instanceof HtmlDataTable) { return (HtmlDataTable) compo; } return getParentDatatable(compo.getParent()); } /** * Cập nhật dữ liệu được chọn lên cơ sở dữ liệu. *
return */ public String update() { try { Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”); Connection con = DriverManager.getConnection(“jdbc:sqlserver://localhost:1433;databaseName=GC0563”, “sa”, “”); String sql = “update lophoc set tenLop=?, tenGVCN=? where msLop=?”; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, tenLop); ps.setString(2, tenGVCN); ps.setString(3, msLop); int x = ps.executeUpdate(); if (x > 0) { return “success”; } } catch (Exception e) { e.printStackTrace(); } return “failed”; }}Bước 3: Thiết kế giao diện