java web入门到精通java调用mysql分页查询
1、效果图
2、后台代码
2.1 Product.java
package com.lyq.bean;
/**
* 商品
* @author whh
*
*/
public class Product {
public static final int PAGE_SIZE = 2;
// 编号
private int id;
// 名称
private String name;
// 价格
private double price;
// 数量
private int num;
// 单位
private String unit;
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
}
2.2 ProductDao
package com.lyq.bean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.mysql.jdbc.Statement;
public class ProductDao {
public static void main(String[] args) {
List<Product> listProducts=new ProductDao().find(1);
}
public Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/hh";
String username = "root";
String password = "root";
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQ