add feature create product
This commit is contained in:
@@ -3,10 +3,7 @@ package dao;
|
|||||||
import model.Product;
|
import model.Product;
|
||||||
import utils.MysqlConnect;
|
import utils.MysqlConnect;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.*;
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -41,4 +38,27 @@ public class ProductDAO {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Boolean createNewProduct(String name, Double price, String title, Date date, String catelog, Boolean status) throws SQLException {
|
||||||
|
String sql = "INSERT INTO `products` (`id`, `name`, `price`, `title`, `created`, `catalog`, `status`) VALUES (NULL, ?, ?, ?, ?, ?, ?);";
|
||||||
|
|
||||||
|
Connection c = MysqlConnect.getConnecton();
|
||||||
|
|
||||||
|
PreparedStatement ps = c.prepareStatement(sql);
|
||||||
|
|
||||||
|
ps.setString(1, name);
|
||||||
|
ps.setDouble(2, price);
|
||||||
|
ps.setString(3, title);
|
||||||
|
ps.setDate(4, date);
|
||||||
|
ps.setString(5, catelog);
|
||||||
|
ps.setBoolean(6, status);
|
||||||
|
|
||||||
|
int rows = ps.executeUpdate();
|
||||||
|
|
||||||
|
if(rows > 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,10 @@ public class ProductMgMenu {
|
|||||||
ps.showProductList();
|
ps.showProductList();
|
||||||
System.out.println("DSSP");
|
System.out.println("DSSP");
|
||||||
break;
|
break;
|
||||||
|
case 2:
|
||||||
|
System.out.println("thêm sản phẩm");
|
||||||
|
ps.addProduct();
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
System.out.println("Lựa chọn kg phù hợp!");
|
System.out.println("Lựa chọn kg phù hợp!");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,12 @@ package services;
|
|||||||
|
|
||||||
import dao.ProductDAO;
|
import dao.ProductDAO;
|
||||||
|
|
||||||
|
import java.sql.Date;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class ProductService {
|
public class ProductService {
|
||||||
ProductDAO productDAO = new ProductDAO();
|
ProductDAO productDAO = new ProductDAO();
|
||||||
|
|
||||||
@@ -13,4 +19,33 @@ public class ProductService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addProduct() {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("Nhập tên sản phẩm");
|
||||||
|
String name = sc.nextLine();
|
||||||
|
System.out.println("Nhập giá bán");
|
||||||
|
Double price = sc.nextDouble();
|
||||||
|
System.out.println("Nhập tiêu đề sản phẩm");
|
||||||
|
String title = sc.nextLine();
|
||||||
|
System.out.println("Ngày tạo");
|
||||||
|
Date created = Date.valueOf(LocalDate.now());
|
||||||
|
System.out.println("Danh mục");
|
||||||
|
String catalog = sc.nextLine();
|
||||||
|
System.out.println("Nhập trạng thái");
|
||||||
|
Boolean status = sc.nextBoolean();
|
||||||
|
System.out.println("created" + created);
|
||||||
|
|
||||||
|
try {
|
||||||
|
if(productDAO.createNewProduct(name, price, title, created, catalog, status)) {
|
||||||
|
System.out.println("thêm sp thành công!");
|
||||||
|
}else {
|
||||||
|
System.out.println("Thêm sp thất bại");
|
||||||
|
}
|
||||||
|
}catch (SQLException e) {
|
||||||
|
System.out.println("E" + e);
|
||||||
|
System.out.println("lỗi xử lý CSDL");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user