LeanJavaJDBCwithRealApps学习JDBC(jdbc)

entities.Product

package entities;


import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;


@SuppressWarnings("serial")
public class Product implements Serializable {
private int id;
private String name;
private BigDecimal price;
private int quantity;
private String description;
private boolean featured;
private Date dateCreated;
private String manufacturer;


public String getManufacturer() {
return manufacturer;
}


public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}


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 BigDecimal getPrice() {
return price;
}


public void setPrice(BigDecimal price) {
this.price = price;
}


public int getQuantity() {
return quantity;
}


public void setQuantity(int quantity) {
this.quantity = quantity;
}


public String getDescription() {
return description;
}


public void setDescription(String description) {
this.description = description;
}


public boolean isFeatured() {
return featured;
}


public void setFeatured(boolean featured) {
this.featured = featured;
}


public Date getDateCreated() {
return dateCreated;
}


public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}


}

models.ProductModel

package models;


import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import entities.Group;
import entities.Product;


public class ProductModel {
/**

* 总结:本演示表有八个字段,有多条数据 , 包含的数据类型有int,String,BigDecimal,boolean,Date,double


*
*/
// 分组时所用的类型
//
// private String manufacturer;
// private BigDecimal minPrice;
// private BigDecimal maxPrice;
// private int sumQuantities;
// private int countProduct;
// private double avgPrice;
//
// 演示表所用的类型
//
// private int id;
// private String name;
// private BigDecimal price;
// private int quantity;
// private String description;
// private boolean featured;
// private Date dateCreated;
// private String manufacturer;
//
//
// CREATE TABLE `product` (
// `id` int(11) NOT NULL AUTO_INCREMENT,
// `name` varchar(250) COLLATE utf8_unicode_ci NOT NULL,
// `price` decimal(10,1) NOT NULL,
// `quantity` int(11) NOT NULL,
// `description` text COLLATE utf8_unicode_ci NOT NULL,
// `featured` tinyint(1) NOT NULL,
// `dateCreated` date NOT NULL,
// `manufacturer` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
// PRIMARY KEY (`id`)
// ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
// 排序专用符号
public enum Direction {
ASC, DESC
}


// 22.删除一条记录
public boolean delete(int id) {
boolean result = true;
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("delete from product where id = ?");
preparedStatement.setInt(1, id);
result = preparedStatement.executeUpdate() > 0;
} catch (Exception e) {
result = false;
}
return result;
}


// 21.更新一条记录
public boolean update(Product product) {
boolean result = true;
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection().prepareStatement(
"update product set name = ?, price = ?, quantity = ?, description = ?, featured = ?, dateCreated = ?,manufacturer=? where id = ?");
preparedStatement.setString(1, product.getName());
preparedStatement.setBigDecimal(2, product.getPrice());
preparedStatement.setInt(3, product.getQuantity());
preparedStatement.setString(4, product.getDescription());
preparedStatement.setBoolean(5, product.isFeatured());
preparedStatement.setDate(6, new java.sql.Date(product.getDateCreated().getTime()));
preparedStatement.setString(7, product.getManufacturer());
preparedStatement.setInt(8, product.getId());
result = preparedStatement.executeUpdate() > 0;
} catch (Exception e) {
result = false;
}
return result;
}


// 20.插入一条记录
public boolean create(Product product) {
boolean result = true;
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection().prepareStatement(
"insert into product(name, price, quantity, description, featured, dateCreated) values(?, ?, ?, ?, ?, ?)");
preparedStatement.setString(1, product.getName());
preparedStatement.setBigDecimal(2, product.getPrice());
preparedStatement.setInt(3, product.getQuantity());
preparedStatement.setString(4, product.getDescription());
preparedStatement.setBoolean(5, product.isFeatured());
preparedStatement.setDate(6, new java.sql.Date(product.getDateCreated().getTime()));
result = preparedStatement.executeUpdate() > 0;


} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;


}


// 19.调用有参数的存储过程
public List<Product> WithParambetween(BigDecimal min, BigDecimal max) {
List<Product> products = new ArrayList<Product>();
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("{call findBetween(?,?)}");
preparedStatement.setBigDecimal(1, min);
preparedStatement.setBigDecimal(2, max);
ResultSet resultSet = preparedStatement.executeQuery();


while (resultSet.next()) {
Product product = new Product();
product.setId(resultSet.getInt("id"));
product.setName(resultSet.getString("name"));
product.setPrice(resultSet.getBigDecimal("price"));


product.setQuantity(resultSet.getInt("price"));
product.setDescription(resultSet.getString("description"));
product.setDateCreated(resultSet.getDate("dateCreated"));
product.setFeatured(resultSet.getBoolean("featured"));


products.add(product);
}
} catch (Exception e) {
products = null;
}
return products;
}


// 18.调用无参数的存储过程
public List<Product> NoParamfindAll() {
List<Product> products = new ArrayList<Product>();
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection().prepareStatement("{call findAll()}");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Product product = new Product();
product.setId(resultSet.getInt("id"));
product.setName(resultSet.getString("name"));
product.setPrice(resultSet.getBigDecimal("price"));


product.setQuantity(resultSet.getInt("price"));
product.setDescription(resultSet.getString("description"));
product.setDateCreated(resultSet.getDate("dateCreated"));
product.setFeatured(resultSet.getBoolean("featured"));


products.add(product);
}
} catch (Exception e) {
products = null;
}
return products;
}


// 17.分组查询
public List<Group> groupBy() {
List<Group> groups = new ArrayList<Group>();
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection().prepareStatement(
"SELECT manufacturer, MIN(price) AS minPrice, MAX(price) AS maxPrice, SUM(quantity) AS sumQuantities, COUNT(id) AS countProduct, AVG(price) AS avgPrice FROM product GROUP BY manufacturer");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Group group = new Group();
group.setManufacturer(resultSet.getString("manufacturer"));
group.setMinPrice(resultSet.getBigDecimal("minPrice"));
group.setMaxPrice(resultSet.getBigDecimal("maxPrice"));
group.setSumQuantities(resultSet.getInt("sumQuantities"));
group.setCountProduct(resultSet.getInt("countProduct"));
group.setAvgPrice(resultSet.getDouble("avgPrice"));
groups.add(group);
}
} catch (Exception e) {
groups = null;
}
return groups;
}


// 16.全部商品的平均价格
public double avg() {
double result = 0;
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("SELECT AVG(price) FROM product");
ResultSet resultSet = preparedStatement.executeQuery();
// 因为查询结果集中只有一行数据,所以只运行一次next就行
resultSet.next();
// 将结果集中的一个数据取出来放入临时变量result中
result = resultSet.getDouble(1);
} catch (SQLException e) {
result = 0;
}
return result;
}


// 15.全部商品的最高价格
public BigDecimal max() {
BigDecimal result = null;
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("SELECT MAX(price) FROM product");
ResultSet resultSet = preparedStatement.executeQuery();
// 因为查询结果集中只有一行数据,所以只运行一次next就行
resultSet.next();
// 将结果集中的一个数据取出来放入临时变量result中
result = resultSet.getBigDecimal(1);
} catch (SQLException e) {
result = null;
}
return result;
}


// 14.全部商品的最低价格
public BigDecimal min() {
BigDecimal result = null;
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("SELECT MIN(price) FROM product");
ResultSet resultSet = preparedStatement.executeQuery();
// 因为查询结果集中只有一行数据,所以只运行一次next就行
resultSet.next();
// 将结果集中的一个数据取出来放入临时变量result中
result = resultSet.getBigDecimal(1);
} catch (SQLException e) {
result = null;
}
return result;
}


// 13.统计商品种类个数
public int count() {
int result = 0;
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("SELECT count(id) FROM product");
ResultSet resultSet = preparedStatement.executeQuery();
// 因为查询结果集中只有一行数据,所以只运行一次next就行
resultSet.next();
// 将结果集中的一个数据取出来放入临时变量result中
result = resultSet.getInt(1);
} catch (SQLException e) {
result = 0;
}
return result;
}


// 12.(a商品个数*a商品价格),(b商品个数*b商品价格),(c商品个数*c商品价格),……全部各个种类商品的总价
public List<Product> productTotal() {
List<Product> products = new ArrayList<Product>();
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("SELECT quantity*price AS price1 FROM product");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Product product = new Product();
// 指定查询出来的结果集这一列的别名为"price1"
product.setPrice(resultSet.getBigDecimal("price1"));
products.add(product);
}
} catch (Exception e) {
products = null;
}
return products;
}


// 11.(a商品个数*a商品价格)+(b商品个数*b商品价格)+(c商品个数*c商品价格)+……全部商品的总价
public BigDecimal total() {
BigDecimal result = null;
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("SELECT SUM(quantity*price) FROM product");
ResultSet resultSet = preparedStatement.executeQuery();
// 因为查询结果集中只有一行数据,所以只运行一次next就行
resultSet.next();
// 将结果集中的一个数据取出来放入临时变量result中
result = resultSet.getBigDecimal(1);
} catch (SQLException e) {
result = null;
}
return result;
}


// 10.商品求和,即商品总个数(列内数据相加)
public int sumQuantities() {
int result = 0;
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("SELECT SUM(quantity) FROM product");
ResultSet resultSet = preparedStatement.executeQuery();
// 因为查询结果集中只有一行数据,所以只运行一次next就行
resultSet.next();
// 将结果集中的一个数据取出来放入临时变量result中
result = resultSet.getInt(1);
} catch (SQLException e) {
result = 0;
}
return result;
}


// 9.根据ID查询单个商品
public List<Product> find(int id) {
List<Product> products = new ArrayList<Product>();
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("SELECT * FROM product WHERE id = ?");
// 参数设置
preparedStatement.setInt(1, id);


ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Product product = new Product();
product.setId(resultSet.getInt("id"));
product.setName(resultSet.getString("name"));
product.setPrice(resultSet.getBigDecimal("price"));


product.setQuantity(resultSet.getInt("price"));
product.setDescription(resultSet.getString("description"));
product.setDateCreated(resultSet.getDate("dateCreated"));
product.setFeatured(resultSet.getBoolean("featured"));


products.add(product);
}
} catch (Exception e) {
products = null;
}


return products;
}


// 8.查询日期范围的商品
public List<Product> findByDate(Date from, Date to) {
List<Product> products = new ArrayList<Product>();
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("SELECT * FROM product WHERE dateCreated BETWEEN ? AND ?");
// 参数设置
preparedStatement.setDate(1, new java.sql.Date(from.getTime()));
preparedStatement.setDate(2, new java.sql.Date(to.getTime()));


ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Product product = new Product();
product.setId(resultSet.getInt("id"));
product.setName(resultSet.getString("name"));
product.setPrice(resultSet.getBigDecimal("price"));


product.setQuantity(resultSet.getInt("price"));
product.setDescription(resultSet.getString("description"));
product.setDateCreated(resultSet.getDate("dateCreated"));
product.setFeatured(resultSet.getBoolean("featured"));


products.add(product);
}
} catch (Exception e) {
products = null;
}


return products;
}


// 7.分页查询(数据库中索引为0)
// 例如,从第二个查询,返回三条记录
public List<Product> limit(int offset, int count) {
List<Product> products = new ArrayList<Product>();
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("select * from product limit ?,?");
// 参数设置
preparedStatement.setInt(1, offset);
preparedStatement.setInt(2, count);


ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Product product = new Product();
product.setId(resultSet.getInt("id"));
product.setName(resultSet.getString("name"));
product.setPrice(resultSet.getBigDecimal("price"));


product.setQuantity(resultSet.getInt("price"));
product.setDescription(resultSet.getString("description"));
product.setDateCreated(resultSet.getDate("dateCreated"));
product.setFeatured(resultSet.getBoolean("featured"));


products.add(product);
}
} catch (Exception e) {
products = null;
}


return products;
}


// 6.商品按价格排序
public List<Product> orderBy(Direction direction) {
List<Product> products = new ArrayList<Product>();
try {
PreparedStatement preparedStatement = null;
// 参数判断
if (direction == Direction.ASC) {
preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("select * from product order by price asc");
} else if (direction == Direction.DESC) {
preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("select * from product order by price desc");
}


ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Product product = new Product();
product.setId(resultSet.getInt("id"));
product.setName(resultSet.getString("name"));
product.setPrice(resultSet.getBigDecimal("price"));


product.setQuantity(resultSet.getInt("price"));
product.setDescription(resultSet.getString("description"));
product.setDateCreated(resultSet.getDate("dateCreated"));
product.setFeatured(resultSet.getBoolean("featured"));


products.add(product);
}
} catch (Exception e) {
products = null;
}


return products;
}


// 5.商品中间模糊查询
public List<Product> contains(String keyword) {
List<Product> products = new ArrayList<Product>();
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("SELECT * FROM product WHERE NAME LIKE ?");
// 参数设置
preparedStatement.setString(1, "%" + keyword + "%");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Product product = new Product();
product.setId(resultSet.getInt("id"));
product.setName(resultSet.getString("name"));
product.setPrice(resultSet.getBigDecimal("price"));


product.setQuantity(resultSet.getInt("price"));
product.setDescription(resultSet.getString("description"));
product.setDateCreated(resultSet.getDate("dateCreated"));
product.setFeatured(resultSet.getBoolean("featured"));


products.add(product);
}
} catch (Exception e) {
products = null;
}


return products;
}


// 4.商品后面模糊查询
public List<Product> endtWith(String keyword) {
List<Product> products = new ArrayList<Product>();
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("SELECT * FROM product WHERE NAME LIKE ?");
// 参数设置
preparedStatement.setString(1, "%" + keyword);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Product product = new Product();
product.setId(resultSet.getInt("id"));
product.setName(resultSet.getString("name"));
product.setPrice(resultSet.getBigDecimal("price"));


product.setQuantity(resultSet.getInt("price"));
product.setDescription(resultSet.getString("description"));
product.setDateCreated(resultSet.getDate("dateCreated"));
product.setFeatured(resultSet.getBoolean("featured"));


products.add(product);
}
} catch (Exception e) {
products = null;
}


return products;
}


// 3.商品前面模糊查询
public List<Product> startWith(String keyword) {
List<Product> products = new ArrayList<Product>();
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("SELECT * FROM product WHERE NAME LIKE ?");
// 参数设置
preparedStatement.setString(1, keyword + "%");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Product product = new Product();
product.setId(resultSet.getInt("id"));
product.setName(resultSet.getString("name"));
product.setPrice(resultSet.getBigDecimal("price"));


product.setQuantity(resultSet.getInt("price"));
product.setDescription(resultSet.getString("description"));
product.setDateCreated(resultSet.getDate("dateCreated"));
product.setFeatured(resultSet.getBoolean("featured"));


products.add(product);
}
} catch (Exception e) {
products = null;
}


return products;
}


// 2.查询价格区间
public List<Product> between(BigDecimal min, BigDecimal max) {
List<Product> products = new ArrayList<Product>();
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("SELECT * FROM product WHERE price BETWEEN ? AND ?");
preparedStatement.setBigDecimal(1, min);
preparedStatement.setBigDecimal(2, max);
ResultSet resultSet = preparedStatement.executeQuery();


while (resultSet.next()) {
Product product = new Product();
product.setId(resultSet.getInt("id"));
product.setName(resultSet.getString("name"));
product.setPrice(resultSet.getBigDecimal("price"));


product.setQuantity(resultSet.getInt("price"));
product.setDescription(resultSet.getString("description"));
product.setDateCreated(resultSet.getDate("dateCreated"));
product.setFeatured(resultSet.getBoolean("featured"));


products.add(product);
}
} catch (Exception e) {
products = null;
}
return products;
}


// 1.查询所有的商品
public List<Product> findAll() {
List<Product> products = new ArrayList<Product>();
try {
PreparedStatement preparedStatement = ConnectDatabase.getConnection()
.prepareStatement("select * from product");
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Product product = new Product();
product.setId(resultSet.getInt("id"));
product.setName(resultSet.getString("name"));
product.setPrice(resultSet.getBigDecimal("price"));


product.setQuantity(resultSet.getInt("price"));
product.setDescription(resultSet.getString("description"));
product.setDateCreated(resultSet.getDate("dateCreated"));
product.setFeatured(resultSet.getBoolean("featured"));


products.add(product);
}
} catch (Exception e) {
products = null;
}
return products;
}

}

entities.Group

package entities;


import java.io.Serializable;
import java.math.BigDecimal;


public class Group implements Serializable {
private String manufacturer;
private BigDecimal minPrice;
private BigDecimal maxPrice;
private int sumQuantities;
private int countProduct;
private double avgPrice;


public String getManufacturer() {
return manufacturer;
}


public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}


public BigDecimal getMinPrice() {
return minPrice;
}


public void setMinPrice(BigDecimal minPrice) {
this.minPrice = minPrice;
}


public BigDecimal getMaxPrice() {
return maxPrice;
}


public void setMaxPrice(BigDecimal maxPrice) {
this.maxPrice = maxPrice;
}


public int getSumQuantities() {
return sumQuantities;
}


public void setSumQuantities(int sumQuantities) {
this.sumQuantities = sumQuantities;
}


public int getCountProduct() {
return countProduct;
}


public void setCountProduct(int countProduct) {
this.countProduct = countProduct;
}


public double getAvgPrice() {
return avgPrice;
}


public void setAvgPrice(double avgPrice) {
this.avgPrice = avgPrice;
}


}

models.ConnectDatabase

package models;


import java.sql.Connection;
import java.sql.DriverManager;


public class ConnectDatabase {
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/advancedjava", "root", "root");
} catch (Exception e) {
connection = null;
}
return connection;
}

}

-----------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------

package demo;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import entities.Group;
import entities.Product;
import models.ProductModel;
import models.ProductModel.Direction;


public class Main {
public static void main(String args[]) {
@SuppressWarnings("unused")
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
ProductModel productModel = new ProductModel();
// 1.查询所有的商品
// List<Product> products = productModel.findAll();
//
// 2.查询价格内的商品
// List<Product> products = productModel.between(BigDecimal.valueOf(2),
// BigDecimal.valueOf(10));
//
// 3.商品以"com"字符串开头
// List<Product> products = productModel.startWith("com");
//
// 4.商品以"ter 2"字符串结尾
// List<Product> products = productModel.endtWith("ter 2");
//
// 5.商品中间包含"pu"字符串
// List<Product> products = productModel.contains("pu");
//
// 6.商品按价格排序
// List<Product> products = productModel.orderBy(Direction.ASC);
//
// 7.分页查询(数据库中索引为0)(例如,从第二个查询,返回三条记录)
// List<Product> products = productModel.limit(2, 3);
// *************************************************************
// 1-7方法共用的遍历操作
// *************************************************************
// for (Product product : products) {
// System.out.println("ID: " + product.getId());
// System.out.println("Name: " + product.getName());
// System.out.println("Price: " + product.getPrice());
//
// System.out.println("Quantity: " + product.getQuantity());
// System.out.println("Description: " + product.getDescription());
// System.out.println("DateCreated: " +
// simpleDateFormat.format(product.getDateCreated()));
// System.out.println("Featured: " + product.isFeatured());
// System.out.println("***********************************");
// }
// *************************************************************
//
// 8.查询日期范围的商品( 先将字符日期,作为输入参数,进行转换)
// try {
// Date from = simpleDateFormat.parse("2018-03-23");
// Date to = simpleDateFormat.parse("2018-03-30");
// List<Product> products = productModel.findByDate(from, to);
// for (Product product : products) {
// System.out.println("ID: " + product.getId());
// System.out.println("Name: " + product.getName());
// System.out.println("Price: " + product.getPrice());
//
// System.out.println("Quantity: " + product.getQuantity());
// System.out.println("Description: " + product.getDescription());
// System.out.println("DateCreated: " +
// simpleDateFormat.format(product.getDateCreated()));
// System.out.println("Featured: " + product.isFeatured());
// System.out.println("***********************************");
// }
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// 9.根据ID查询单个商品
// List<Product> products = productModel.find(3);
// for (Product product : products) {
// System.out.println("ID: " + product.getId());
// System.out.println("Name: " + product.getName());
// System.out.println("Price: " + product.getPrice());
//
// System.out.println("Quantity: " + product.getQuantity());
// System.out.println("Description: " + product.getDescription());
// System.out.println("DateCreated: " +
// simpleDateFormat.format(product.getDateCreated()));
// System.out.println("Featured: " + product.isFeatured());
// System.out.println("***********************************");
// }
//
// 10.商品求和,即商品总个数(列内数据相加)
// int sumQuantities = productModel.sumQuantities();
// System.out.println("商品求和,即商品总个数(列内数据相加):" + sumQuantities);
//
// 11.(a商品个数*a商品价格)+(b商品个数*b商品价格)+(c商品个数*c商品价格)+……全部商品的总价
// BigDecimal total = productModel.total();
// System.out.println("全部商品的总价:" + total);
//
// 12.(a商品个数*a商品价格),(b商品个数*b商品价格),(c商品个数*c商品价格),……全部各个种类商品的总价
List<Product> products = productModel.productTotal();
System.out.println("全部商品,各个种类的总价:");


// *************************************************************
for (Product product : products) {
System.out.println("Price: " + product.getPrice());
}
// *************************************************************


// 13.统计商品种类个数
// int count = productModel.count();
// System.out.println("全部商品的总个数(统计个数,就是数个数):" + count);
//
// 14.全部商品的最低价格
// BigDecimal min = productModel.min();
// System.out.println("全部商品的最低价格:" + min);
//
// 15.全部商品的最高价格
// BigDecimal max = productModel.max();
// System.out.println("全部商品的最高价格:" + max);
//
// 16.全部商品的平均价格
// double avg = productModel.avg();
// System.out.println("全部商品的平均价格:" + avg);
//
// 17.分组查询
// System.out.println("==============================");
// for (Group group : productModel.groupBy()) {
// System.out.println(group.getManufacturer());
// System.out.println("Min Price: " + group.getMinPrice());
// System.out.println("Max Price: " + group.getMaxPrice());
// System.out.println("Avg Price: " + group.getAvgPrice());
// System.out.println("Count Product: " + group.getCountProduct());
// System.out.println("Sum Quantities: " + group.getSumQuantities());
// System.out.println("==============================");
// }
//
// 18.调用无参数的存储过程
// List<Product> products = productModel.NoParamfindAll();
// for (Product product : products) {
// System.out.println("ID: " + product.getId());
// System.out.println("Name: " + product.getName());
// System.out.println("Price: " + product.getPrice());
//
// System.out.println("Quantity: " + product.getQuantity());
// System.out.println("Description: " + product.getDescription());
// System.out.println("DateCreated: " +
// simpleDateFormat.format(product.getDateCreated()));
// System.out.println("Featured: " + product.isFeatured());
// System.out.println("***********************************");
// }
//
// 19.调用有参数的存储过程
// List<Product> products = productModel.WithParambetween(BigDecimal.valueOf(2),
// BigDecimal.valueOf(10));
// for (Product product : products) {
// System.out.println("ID: " + product.getId());
// System.out.println("Name: " + product.getName());
// System.out.println("Price: " + product.getPrice());
//
// System.out.println("Quantity: " + product.getQuantity());
// System.out.println("Description: " + product.getDescription());
// System.out.println("DateCreated: " +
// simpleDateFormat.format(product.getDateCreated()));
// System.out.println("Featured: " + product.isFeatured());
// System.out.println("***********************************");
// }
//
// 20.插入一条记录
// Product product = new Product();
// product.setName("Product 4");
// product.setPrice(BigDecimal.valueOf(10));
// product.setQuantity(6);
// product.setDescription("Good");
// product.setFeatured(true);
// product.setDateCreated(new Date());
// boolean result = productModel.create(product);
// System.out.println("Result: " + result);
//
// 21.更新记录
// Product product = new Product();
// product.setId(2);
// product.setName("Product 4");
// product.setPrice(BigDecimal.valueOf(10));
// product.setQuantity(6);
// product.setDescription("Good");
// product.setFeatured(true);
// product.setDateCreated(new Date());
// product.setManufacturer("Manufacturer 4");
// boolean result = productModel.update(product);
// System.out.println("Result: " + result);
//
// 22.删除一条记录
// boolean result = productModel.delete(2);
// System.out.println("Result: " + result);
}
}

猜你喜欢

转载自blog.csdn.net/jake_Aaron/article/details/80559282