02 September 2016

JSP Servlet Phân trang sản phẩm hiển thị chưa có next previous với Mysql Java NetBeans

http://localhost:8084/AssignmentJsp/index.jsp?pages=2
=========================Exampe========================
Database
packages Util: DBUtil.java
Java 2016
public class DBUtil {

    private static Connection connection = null;

    public static Connection getConnection() {
        if (connection != null) {
            return connection;
        } else {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/product", "root", "1234567");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connection;
        }
    }
    //Test connection
//    public static void main(String[] args) {
//        System.out.println(getConnection());
//    }

}
packages dao: CarDAO.java
Java 2016
public class CarDAO {

    public ArrayList<Car> getAllCar(int first, int last) {
        ArrayList<Car> list = new ArrayList();
        Connection conn = DBUtil.getConnection();
        try {
            PreparedStatement stmt = conn.prepareStatement("SELECT * FROM product.car Limit ?,?");
            stmt.setInt(1, first);
            stmt.setInt(2, last);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id_car");
                String name = rs.getString("name_car");
                String detail = rs.getString("detail_car");
                String image = rs.getString("image_car");
                Car c = new Car(id, name, detail, image);
                list.add(c);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

  //Count product
    public int getNumberProduct() {
        Connection conn = DBUtil.getConnection();
        int count = 0;
        try {
            PreparedStatement stmt = conn.prepareStatement("SELECT count(id_car) FROM product.car");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }
}
packages model: Car.java
Java 2016
package model;

/**
 *
 * @author Lonely
 */
public class Car {

    private int idCar;
    private String nameCar;
    private String detailCar;
    private String imageCar;

    public Car(String nameCar, String detailCar, String imageCar) {
        this.nameCar = nameCar;
        this.detailCar = detailCar;
        this.imageCar = imageCar;
    }

    public Car(int idCar, String nameCar, String detailCar, String imageCar) {
        this.idCar = idCar;
        this.nameCar = nameCar;
        this.detailCar = detailCar;
        this.imageCar = imageCar;
    }

    public int getIdCar() {
        return idCar;
    }

    public void setIdCar(int idCar) {
        this.idCar = idCar;
    }

    public String getNameCar() {
        return nameCar;
    }

    public void setNameCar(String nameCar) {
        this.nameCar = nameCar;
    }

    public String getDetailCar() {
        return detailCar;
    }

    public void setDetailCar(String detailCar) {
        this.detailCar = detailCar;
    }

    public String getImageCar() {
        return imageCar;
    }

    public void setImageCar(String imageCar) {
        this.imageCar = imageCar;
    }

}
web-pages: content.jsp
Java 2016
<%@page import="dao.CarDAO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="model.Car"%>
<%@page import="model.Car"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Content</title>
        <link href="css/style_product.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
        <%
            int first = 0, last = 0, pages = 0;
            if (request.getParameter("pages") != null) {
                pages = (int) Integer.parseInt(request.getParameter("pages"));
            }
            int total = new CarDAO().getNumberProduct();
            if (total <= 3) {
                first = 0;
                last = total;
            //SELECT * FROM product.car limit 0,3
            } else {
                first = (pages - 1) * 3;
                last = 3;
            }
            //SELECT * FROM product.car limit 3,3
            ArrayList<Car> list = new CarDAO().getAllCar(first, last);
        %>
        <section>

            <div id="line">
                <div class="dline"></div>
                <h1>Luxury</h1>
                <div class="dline"></div>
            </div>
            <div id="ourserv">
                <%
                    for (Car c : list) {
                %>
                <article>
                    <h1><%=c.getNameCar()%></h1>
                    <img src="<%=c.getImageCar()%>" alt="" />
                    <p><%=c.getDetailCar()%></p>
                    <a href="" class="rm">Read More</a>
                </article>
                <%}%>
            </div>
            <div id="pages">
                <ul id="pagination-flickr">
                    <a href="content?pages=2">«Previous</a>
                    <%for (int i = 1; i < (total / 3) + 1; i++) {
                            if (pages == i) {%>
                    <a id="cool" href="index.jsp?pages=<%=i%>"><%=i%></a>
                    <%} else {%>
                    <a href="index.jsp?pages=<%=i%>"><%=i%></a>   
                    <%}
                        }%>
                    <a href="content?pages=2">Next »</a>
                </ul>
            </div>
        </section>  
    </body>
</html>
web-pages =>folder css: style_product.css
Java 2016
//CSS phân trang 
#pages{padding: 25px; text-align: center}

#pages a{    
    color: #0063e3;
    display: inline;
    padding: 3px 6px;
    text-decoration: none;
    border:solid 1px #DDDDDD;
    margin-right:2px;
}
#pages a:hover{    
    color: #0063e3;
    background-color: gold;
}
#pages #cool{ background-color: gold;}
Chú ý trên đây chỉ nhắc đến phần liên qua đến phân trang còn hiển thị như trang index và các css thì vui lòng tải toàn bộ examp và import vào netbeans của các bạn để chạy

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang