Đầu tiên ta tạo một lớp để kết nối đến cơ sở dữ liệu với tên là DataAccess và đặt nó trong package vpd.connection
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package vpd.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Nguyen Minh An
*/
public class DataAccess {
public static Connection getConnection() throws SQLException {
String url = "jdbc:sqlserver://localhost:1433;databaseName=VIEW_PRODUCT_DETAIL";
String username = "LUCKY";
String password = "123456";
try {
Connection conn = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(url, username, password);
return conn;
} catch (ClassNotFoundException ex) {
Logger.getLogger(DataAccess.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
Tiếp đó là lớp để sử lý nghiệp vụ với tên là ProductManagement.java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package vpd.bussiness;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import vpd.db.DataAccess;
import vpd.entity.Product;
/**
*
* @author Nguyen Minh An
*/
public class ProductManagement {
public List<Product> getAllProduct() {
List<Product> list = new ArrayList<>();
try {
Connection conn = DataAccess.getConnection();
PreparedStatement ps = conn.prepareCall("SELECT * FROM PRODUCT");
ResultSet res = ps.executeQuery();
while (res.next()) {
int id = res.getInt("id");
String name = res.getString("name");
String price = res.getString("price");
Product p = new Product(id, name, price);
list.add(p);
}
return list;
} catch (SQLException ex) {
Logger.getLogger(ProductManagement.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public List<Product> getProductDetailByName(String name) {
List<Product> list = new ArrayList<>();
try {
Connection conn = DataAccess.getConnection();
PreparedStatement ps = conn.prepareCall("SELECT * FROM PRODUCT WHERE name like '" + name + "'");
ResultSet res = ps.executeQuery();
while (res.next()) {
int id = res.getInt("id");
String names = res.getString("name");
String price = res.getString("price");
Product p = new Product(id, names, price);
list.add(p);
}
return list;
} catch (SQLException ex) {
Logger.getLogger(ProductManagement.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
Và một lớp để Product để đóng tạo ra đối tượng Product.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package vpd.entity;
/**
*
* @author Nguyen Minh An
*/
public class Product {
private int id;
private String name;
private String price;
public Product(int id, String name, String price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
Tiếp đó ta tạo một lớp JSF Managed Bean như dưới:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package vpd.managed.bean;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import vpd.bussiness.ProductManagement;
import vpd.entity.Product;
/**
*
* @author Nguyen Minh An
*/
@ManagedBean
@RequestScoped
public class GetAllProduct {
private int id;
private String name;
private String price;
private List<Product> allProduct = new ProductManagement().getAllProduct();
public GetAllProduct() {
}
public List<Product> getAllProduct() {
return allProduct;
}
public List<Product> getProductDetailByName() {
allProduct = new ProductManagement().getProductDetailByName(name);
return allProduct;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
Tiếp tới ta tạo 2 trang jsf với tên lần lượt là: index.xhtml và result.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:outputText value="Danh sách sản phẩm:"/>
<h:dataTable value="#{getAllProduct.allProduct}" var="l">
<h:column>
<f:facet name="header">
<h:outputText value=""/>
</f:facet>
<h:link outcome="result.xhtml" value="#{l.name}" includeViewParams="true">
<f:param name="name" value="#{l.name}"/>
</h:link>
</h:column>
</h:dataTable>
</h:body>
</html>
Trang result.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:metadata>
<f:viewParam name="name" value="#{getAllProduct.name}"/>
</f:metadata>
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:outputText value="#{getAllProduct.name}"/>
<h:dataTable value="#{getAllProduct.productDetailByName}" var="product" style="border: 1px solid blue">
<h:column>
Id: <h:outputText value="#{product.id}" style="border: 1px solid blue"/><br></br>
Name: <h:outputText value="#{product.name}" style="border: 1px solid blue"/><br></br>
Price: <h:outputText value="#{product.price}" style="border: 1px solid blue"/><br></br>
</h:column>
</h:dataTable>
</h:body>
</html>
Không có nhận xét nào:
Đăng nhận xét