import [Link].
*;
import [Link].*;
// 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 [Link](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) {
[Link] = id;
[Link] = name;
[Link] = price;
[Link] = stockQuantity;
}
public void displayInfo() {
[Link](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) {
[Link] = customerId;
[Link] = name;
[Link] = password;
[Link] = role;
[Link] = email;
// Class EcommerceSystem
class EcommerceSystem {
public void listProducts() {
try (Connection conn = [Link]();
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT * FROM Product")) {
while ([Link]()) {
int id = [Link]("id");
String name = [Link]("name");
double price = [Link]("price");
int stock = [Link]("stockQuantity");
Product p = new Product(id, name, price, stock);
[Link]();
} catch (SQLException e) {
[Link]();
public Product searchProduct(String name) {
try (Connection conn = [Link]();
PreparedStatement stmt = [Link]("SELECT * FROM Product WHERE name = ?"))
{
[Link](1, name);
ResultSet rs = [Link]();
if ([Link]()) {
return new Product([Link]("id"), [Link]("name"), [Link]("price"),
[Link]("stockQuantity"));
} catch (SQLException e) {
[Link]();
return null;
}
public Customer login(String email, String password) {
try (Connection conn = [Link]();
PreparedStatement stmt = [Link]("SELECT * FROM Customer WHERE email = ?
AND password = ?")) {
[Link](1, email);
[Link](2, password);
ResultSet rs = [Link]();
if ([Link]()) {
return new Customer([Link]("customerId"), [Link]("name"), [Link]("password"),
[Link]("role"), [Link]("email"));
} catch (SQLException e) {
[Link]();
return null;
// Main Class
public class StoreManager {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
EcommerceSystem system = new EcommerceSystem();
[Link]("Welcome to the store! Please log in.");
[Link]("Enter email: ");
String email = [Link]();
[Link]("Enter password: ");
String password = [Link]();
Customer loggedInCustomer = [Link](email, password);
if (loggedInCustomer == null) {
[Link]("Invalid credentials. Exiting.");
return;
[Link]("Welcome, " + [Link] + "!");
while (true) {
[Link]("1. View Products");
[Link]("2. Search Product");
[Link]("3. Exit");
[Link]("Choose an option: ");
int choice = [Link]();
[Link]();
switch (choice) {
case 1:
[Link]();
break;
case 2:
[Link]("Enter product name: ");
String productName = [Link]();
Product product = [Link](productName);
if (product != null) {
[Link]();
} else {
[Link]("Product not found.");
}
break;
case 3:
[Link]("Thank you for shopping with us!");
return;