import java.sql.
*;
import java.util.*;
// Kết nối SQL Server
class Database {
private static final String URL =
"jdbc:sqlserver://localhost:1433;databaseName=EcommerceDB;encrypt=false";
private static final String USER = "sa";
private static final String PASSWORD = "your_password";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
// Class Product
class Product {
int id;
String name;
double price;
int stockQuantity;
public Product(int id, String name, double price, int stockQuantity) {
this.id = id;
this.name = name;
this.price = price;
this.stockQuantity = stockQuantity;
}
public void displayInfo() {
System.out.println(id + ". " + name + " - " + price + " VND (Stock: " + stockQuantity + ")");
// Class Customer
class Customer {
int customerId;
String password;
String name;
String role;
String email;
Map<Product, Integer> cart = new HashMap<>();
public Customer(int customerId, String name, String password, String role, String email) {
this.customerId = customerId;
this.name = name;
this.password = password;
this.role = role;
this.email = email;
// Class EcommerceSystem
class EcommerceSystem {
public void listProducts() {
try (Connection conn = Database.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Product")) {
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
double price = rs.getDouble("price");
int stock = rs.getInt("stockQuantity");
Product p = new Product(id, name, price, stock);
p.displayInfo();
} catch (SQLException e) {
e.printStackTrace();
public Product searchProduct(String name) {
try (Connection conn = Database.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM Product WHERE name = ?"))
{
stmt.setString(1, name);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return new Product(rs.getInt("id"), rs.getString("name"), rs.getDouble("price"),
rs.getInt("stockQuantity"));
} catch (SQLException e) {
e.printStackTrace();
return null;
}
public Customer login(String email, String password) {
try (Connection conn = Database.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM Customer WHERE email = ?
AND password = ?")) {
stmt.setString(1, email);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return new Customer(rs.getInt("customerId"), rs.getString("name"), rs.getString("password"),
rs.getString("role"), rs.getString("email"));
} catch (SQLException e) {
e.printStackTrace();
return null;
// Main Class
public class StoreManager {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
EcommerceSystem system = new EcommerceSystem();
System.out.println("Welcome to the store! Please log in.");
System.out.print("Enter email: ");
String email = scanner.nextLine();
System.out.print("Enter password: ");
String password = scanner.nextLine();
Customer loggedInCustomer = system.login(email, password);
if (loggedInCustomer == null) {
System.out.println("Invalid credentials. Exiting.");
return;
System.out.println("Welcome, " + loggedInCustomer.name + "!");
while (true) {
System.out.println("1. View Products");
System.out.println("2. Search Product");
System.out.println("3. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
system.listProducts();
break;
case 2:
System.out.print("Enter product name: ");
String productName = scanner.nextLine();
Product product = system.searchProduct(productName);
if (product != null) {
product.displayInfo();
} else {
System.out.println("Product not found.");
}
break;
case 3:
System.out.println("Thank you for shopping with us!");
return;