commit c130218fa1025354cf70e24eb51a4fab644629a4 Author: Your Name Date: Tue Apr 7 19:42:25 2026 +0700 up diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..13275f1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,30 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ +.kotlin + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..ab1f416 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..5cb71ef --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/libraries/mysql_connector_j_9_6_0.xml b/.idea/libraries/mysql_connector_j_9_6_0.xml new file mode 100644 index 0000000..3cf6d80 --- /dev/null +++ b/.idea/libraries/mysql_connector_j_9_6_0.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..03f397c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6dbfbbc --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/lib/mysql-connector-j-9.6.0.jar b/lib/mysql-connector-j-9.6.0.jar new file mode 100644 index 0000000..9c62b72 Binary files /dev/null and b/lib/mysql-connector-j-9.6.0.jar differ diff --git a/qlsp.iml b/qlsp.iml new file mode 100644 index 0000000..d095425 --- /dev/null +++ b/qlsp.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..beb7035 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,8 @@ +import menus.ProductMgMenu; + +public class Main { + public static void main(String[] args) { + ProductMgMenu productMgMenu = new ProductMgMenu(); + productMgMenu.load(); + } +} \ No newline at end of file diff --git a/src/dao/ProductDAO.java b/src/dao/ProductDAO.java new file mode 100644 index 0000000..1921264 --- /dev/null +++ b/src/dao/ProductDAO.java @@ -0,0 +1,44 @@ +package dao; + +import model.Product; +import utils.MysqlConnect; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class ProductDAO { + + public List getProductList() throws SQLException { + List result = new ArrayList<>(); + + String sql = "SELECT * FROM products"; + + Connection c = MysqlConnect.getConnecton(); + + PreparedStatement pt = c.prepareStatement(sql); + + ResultSet rs = pt.executeQuery(); + + while (rs.next()) { + + Product p = new Product( + rs.getInt("id"), + rs.getString("name"), + rs.getDouble("price"), + rs.getString("title"), + rs.getDate("created"), + rs.getString("catalog"), + rs.getBoolean("status") + ); + + result.add(p); + + } + + return result; + } +} diff --git a/src/db.sql b/src/db.sql new file mode 100644 index 0000000..d5b3330 --- /dev/null +++ b/src/db.sql @@ -0,0 +1,12 @@ +create database product_management_db; + +use database product_management_db; + +CREATE TABLE `products` ( + `id` int NOT NULL, + `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, + `price` float NOT NULL, + `title` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL, + `created` date NOT NULL, + `status` tinyint(1) NOT NULL DEFAULT '1' +); \ No newline at end of file diff --git a/src/menus/ProductMgMenu.java b/src/menus/ProductMgMenu.java new file mode 100644 index 0000000..c3140b0 --- /dev/null +++ b/src/menus/ProductMgMenu.java @@ -0,0 +1,41 @@ +package menus; + +import services.ProductService; + +import java.util.Scanner; + +public class ProductMgMenu { + + ProductService ps = new ProductService(); + + public void load() { + int choice; + Scanner sc = new Scanner(System.in); + + do { + + System.out.println("1/ Danh sách sản phẩm"); + System.out.println("2/ Thêm mới sản phẩm"); + System.out.println("3/ Cập nhật sản phẩm"); + System.out.println("4/ Xóa sản phẩm"); + System.out.println("5/ Tìm kiếm sản phẩm theo tên sản phẩm"); + System.out.println("6/ Sắp xếp sản phẩm theo giá tăng dần"); + System.out.println("7/ Thống kê số lượng sản phẩm theo danh mục"); + System.out.println("0. Thoát"); + + System.out.println("Lựa chọn của bạn: "); + choice = sc.nextInt(); + + switch (choice) { + case 1: + ps.showProductList(); + System.out.println("DSSP"); + break; + default: + System.out.println("Lựa chọn kg phù hợp!"); + } + + + }while (choice != 0); + } +} diff --git a/src/model/Product.java b/src/model/Product.java new file mode 100644 index 0000000..82d6ae9 --- /dev/null +++ b/src/model/Product.java @@ -0,0 +1,89 @@ +package model; + +import java.util.Date; + +public class Product { + int id; + String name; + Double price; + String title; + Date created; + String catalog; + Boolean status; + + public Product(){ + + }; + + + public Product(int id, String name, Double price, String title, Date created, String catalog, Boolean status) { + this.id = id; + this.name = name; + this.price = price; + this.title = title; + this.created = created; + this.catalog = catalog; + this.status = status; + } + + 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 String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Date getCreated() { + return created; + } + + public void setCreated(Date created) { + this.created = created; + } + + public String getCatalog() { + return catalog; + } + + public void setCatalog(String catalog) { + this.catalog = catalog; + } + + public Boolean getStatus() { + return status; + } + + public void setStatus(Boolean status) { + this.status = status; + } + + @Override + public String toString() { + return "Tên SP " + this.name + " giá: " + this.price; + } +} diff --git a/src/services/ProductService.java b/src/services/ProductService.java new file mode 100644 index 0000000..99b9a9a --- /dev/null +++ b/src/services/ProductService.java @@ -0,0 +1,16 @@ +package services; + +import dao.ProductDAO; + +public class ProductService { + ProductDAO productDAO = new ProductDAO(); + + public void showProductList() { + try { + System.out.println(productDAO.getProductList()); + }catch (Exception e) { + System.out.println("Không thể đọc dữ liệu từ CSDL!"); + } + } + +} diff --git a/src/utils/MysqlConnect.java b/src/utils/MysqlConnect.java new file mode 100644 index 0000000..b85e5d9 --- /dev/null +++ b/src/utils/MysqlConnect.java @@ -0,0 +1,18 @@ +package utils; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class MysqlConnect { + + public static Connection getConnecton() { + try { + return DriverManager.getConnection("jdbc:mysql://localhost:3306/product_management_db?" + "user=root&password="); + } catch (SQLException ex) { + System.out.println("loi " + ex); + } + return null; + } + +}