Stop 13 · Databases, JDBC & JPAProjects · 1/1

Student Database App with JDBC

Part of the Java Roadmap

In short

Build a full CRUD app with nothing but JDBC and you'll never again be confused by what an ORM does for you. It is also a solid, honest first database project for your GitHub.

A console app that stores students, courses and marks in PostgreSQL or MySQL and supports add, search, update, delete and a report of toppers per course, all through JDBC and PreparedStatement.

The jaadu sits behind a college office counter with a huge open register, a rubber stamp in one hand and a stack of admission forms beside it.

Think of it like this

The college office register

A college office in Bhopal keeps a big register of students, courses and marks. The clerk adds new admissions, looks up a roll number, corrects a misspelt name, strikes out a transfer and, at the end of term, lists the topper in each course. Your console app is that clerk, and the database is the register.

The big ideas

What you need to know

Separate layers

Keep SQL in DAO classes and menu code in the main class. It mirrors how Spring apps are structured.

PreparedStatement only

Every query that takes input uses ? placeholders. Mention it in your README.

Transactions

Saving a student and their marks together uses setAutoCommit(false), commit and rollback on error.

A real report

The toppers report shows you can write more than CRUD: joins and ranking.

How it works

Build it in layers

  1. 1

    Schema

    Tables students, courses and marks with primary and foreign keys, created by a schema.sql script.

  2. 2

    Model

    Java records like Student(long id, String name, String city) to hold rows.

  3. 3

    DAO

    A StudentDao class with add, findById, search, update and delete, all using PreparedStatement.

  4. 4

    Report

    One query with a window function or GROUP BY for the topper per course.

  5. 5

    Menu

    A Scanner-based menu loop that calls the DAO and prints neat tables.

See it in code

Walk through the code

java
1import java.sql.*;2import java.util.*;3 4public record Student(long id, String name, String city) {}5 6public class StudentDao {7    private final Connection conn;8    public StudentDao(Connection conn) { this.conn = conn; }9 10    public long add(String name, String city) throws SQLException {11        String sql = "INSERT INTO students (name, city) VALUES (?, ?)";12        try (PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {13            ps.setString(1, name);14            ps.setString(2, city);15            ps.executeUpdate();16            try (ResultSet keys = ps.getGeneratedKeys()) {17                keys.next();18                return keys.getLong(1);19            }20        }21    }22 23    public List<Student> searchByCity(String city) throws SQLException {24        String sql = "SELECT id, name, city FROM students WHERE city = ? ORDER BY name";25        List<Student> result = new ArrayList<>();26        try (PreparedStatement ps = conn.prepareStatement(sql)) {27            ps.setString(1, city);28            try (ResultSet rs = ps.executeQuery()) {29                while (rs.next()) {30                    result.add(new Student(rs.getLong("id"), rs.getString("name"), rs.getString("city")));31                }32            }33        }34        return result;35    }36 37    public void addWithMarks(String name, String city, long courseId, int marks) throws SQLException {38        conn.setAutoCommit(false);39        try {40            long id = add(name, city);41            try (PreparedStatement ps = conn.prepareStatement(42                    "INSERT INTO marks (student_id, course_id, marks) VALUES (?, ?, ?)")) {43                ps.setLong(1, id);44                ps.setLong(2, courseId);45                ps.setInt(3, marks);46                ps.executeUpdate();47            }48            conn.commit();49        } catch (SQLException e) {50            conn.rollback();51            throw e;52        } finally {53            conn.setAutoCommit(true);54        }55    }56}

1/5A record is a compact, immutable holder for one row.

Part of the DAO. Student and StudentDao go in separate files; the menu class creates one Connection and passes it in.

Save yourself the pain

Common mistakes

Oops

Putting SQL, Scanner input and printing in one giant main method.

Do this instead

Split into model, DAO and menu classes. It's easier to test and shows design sense.

Oops

Saving related rows in separate auto-committed statements.

Do this instead

Wrap them in one transaction so a failure can't leave half-saved data.

Oops

A README that only says "Student app in Java".

Do this instead

Add the schema diagram, setup steps, sample output and what you learned about JDBC.

Your mission

Ship the project

~180 min

Quick check

Why does addWithMarks call conn.setAutoCommit(false) first?

Go deeper

  1. 1Create the schema with a Flyway migration or an SQL script in the repo.
  2. 2Put all SQL in a StudentDao class and keep the menu code separate.
  3. 3Wrap multi-step operations like enrolling a student in a transaction.

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge