Every semester, students buy expensive textbooks, use them for four months, and then let them gather dust. The Book Marketplace I built fixes that: a platform where students donate, buy, or exchange second-hand books. Here is how I designed it with Java and MySQL, and what I would tell anyone starting a similar project.

Start with the data

Before writing any Java, I sketched the database on paper. A marketplace lives or dies on its data model, and getting the tables right first made everything after it easier. The core was three tables:

CREATE TABLE users (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(150) NOT NULL UNIQUE,
  password_hash VARCHAR(255) NOT NULL,
  role ENUM('STUDENT', 'ADMIN') NOT NULL DEFAULT 'STUDENT',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE books (
  id BIGINT PRIMARY KEY AUTO_INCREMENT,
  title VARCHAR(200) NOT NULL,
  author VARCHAR(150),
  price DECIMAL(10, 2) NOT NULL,
  listing_type ENUM('SELL', 'EXCHANGE', 'DONATE') NOT NULL,
  status ENUM('AVAILABLE', 'RESERVED', 'GONE') DEFAULT 'AVAILABLE',
  owner_id BIGINT NOT NULL,
  FOREIGN KEY (owner_id) REFERENCES users(id)
);

Two decisions here paid off later. The listing_type column meant one table served selling, exchanging, and donating instead of three parallel features. And the status column gave every book a simple lifecycle: available, reserved while a deal is discussed, gone when it changes hands.

Auth before features

It is tempting to build the fun parts first and bolt on login later. Do not. Authentication touches every feature, so build it on day one. My rules were simple: never store plain-text passwords, never trust the client about who is logged in, and expire sessions.

Passwords were hashed with BCrypt, which is deliberately slow to make brute force attacks expensive. On login, the server checked the hash, then created a session holding only the user id and role. Every protected endpoint started by asking one question: is there a valid session, and does this user own this resource? That single check, applied consistently, prevented the entire category of "user A edits user B's listing" bugs.

Roles change everything

The moment I added an ADMIN role, the app got noticeably better. Admins could remove spam listings and settle disputes, which meant I needed an admin view, role checks on sensitive endpoints, and a clean way to say "forbidden" versus "not logged in". Returning 401 for unauthenticated and 403 for unauthorized sounds like a small detail, but it made the frontend logic dramatically simpler: 401 means show the login page, 403 means show an error.

Validate on both sides

Client-side validation gives instant feedback, but it is a courtesy, not security. Anyone can bypass it with curl, so the server must validate everything again. My rule was simple: the frontend validates for kindness, the backend validates for truth. A price must be positive, an email must look like an email, and a listing must belong to the logged-in user before any write happens:

public void createListing(BookListing listing, long userId) {
    if (listing.getPrice() < 0) {
        throw new IllegalArgumentException("Price cannot be negative");
    }
    if (listing.getTitle() == null || listing.getTitle().isBlank()) {
        throw new IllegalArgumentException("Title is required");
    }
    listing.setOwnerId(userId); // never trust the client for this
    bookRepository.save(listing);
}

Notice the last line. The owner id comes from the session, never from the request body, because a malicious client could otherwise list books under someone else's name. Any field the user should not control must be set server-side, every time, without exception.

What held up in production

The parts of the project I am proudest of are not features at all. They are the boring things: form validation on both client and server, so bad data never reached the database; parameterized queries everywhere, so SQL injection was never possible; and a consistent JSON shape for every API response, so the frontend never had to guess.

If you are building your first Java and MySQL project, my advice is to pick a problem you genuinely understand. I knew exactly how students trade books because I am one. That understanding made every design decision, from the listing types to the roles, feel obvious instead of arbitrary. Build what you know, model the data first, and secure it before you decorate it.