A Guide to Learning the Java Programming Language (Part 2)

A Guide to Learning the Java Programming Language (Part 2)
Photo by Wan San Yip / Unsplash

In the first part of our guide, we built a simple yet usable student organization application. In this continuation, we will enhance our Student Management System by adding input validation, search functionality, improved error handling, and better styling for the user interface.

A Guide to Learning the Java Programming Language (Part 1)
In this guide, we’ll build a simple yet comprehensive Java application to help you learn the fundamentals and some advanced features of Java. We’ll create a “Student Management System” that allows users to perform CRUD (Create, Read, Update, Delete) operations on student records. This application will use Java SE, JDBC

Validation: Add Input Validation

To ensure the data entered by users is valid, we'll add input validation in the MainApp class.

private boolean validateInput() {
    if (nameField.getText().trim().isEmpty()) {
        JOptionPane.showMessageDialog(this, "Name is required.");
        return false;
    if (emailField.getText().trim().isEmpty()) {
        JOptionPane.showMessageDialog(this, "Email is required.");
        return false;
    if (courseField.getText().trim().isEmpty()) {
        JOptionPane.showMessageDialog(this, "Course is required.");
        return false;
    try {
        int age = Integer.parseInt(ageField.getText().trim());
        if (age <= 0) {
            JOptionPane.showMessageDialog(this, "Age must be a positive number.");
            return false;
    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(this, "Age must be a number.");
        return false;
    return true;

private void addStudent() {
    if (validateInput()) {
        Student student = new Student();

Implement Search Functionality

We will add a search bar to filter students based on their name, email, or course.

Adding Search Functionality

Modifying the user interface to include a search bar.

// In the MainApp class constructor
JLabel searchLabel = new JLabel("Search:");
JTextField searchField = new JTextField(20);
JButton searchButton = new JButton("Search");

searchButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {


Implementing the searchStudents method:

private void searchStudents() {
    String keyword = searchField.getText().trim();
    List<Student> students = studentDAO.searchStudents(keyword);

Modifying the StudentDAO class to support searching:

public List<Student> searchStudents(String keyword) {
    List<Student> students = new ArrayList<>();
    try (Connection conn = getConnection();
         PreparedStatement stmt = conn.prepareStatement("SELECT * FROM students WHERE name LIKE ? OR email LIKE ? OR course LIKE ?")) {
        String searchPattern = "%" + keyword + "%";
        stmt.setString(1, searchPattern);
        stmt.setString(2, searchPattern);
        stmt.setString(3, searchPattern);
        try (ResultSet rs = stmt.executeQuery()) {
            while (rs.next()) {
                Student student = new Student();
    } catch (SQLException e) {
    return students;

Enhanced Error Handling

We will improve error handling by catching exceptions and displaying user-friendly error messages.

Updating the StudentDAO methods:

public void addStudent(Student student) {
    try (Connection conn = getConnection();
         PreparedStatement stmt = conn.prepareStatement("INSERT INTO students (name, email, course, age) VALUES (?, ?, ?, ?)")) {
        stmt.setString(1, student.getName());
        stmt.setString(2, student.getEmail());
        stmt.setString(3, student.getCourse());
        stmt.setInt(4, student.getAge());
    } catch (SQLException e) {
        showErrorDialog("Error adding student: " + e.getMessage());

private void showErrorDialog(String message) {
    JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);

Apply a Better Styling to the Swing Components

To improve the UI/UX, we will apply better styling to the Swing components.

Setting up a custom Look and Feel:

import javax.swing.UIManager;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

public class MainApp extends JFrame {
    public MainApp() {
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (Exception e) {
        // Rest of the initialization code...

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MainApp().setVisible(true);

Let's add some custom styling also.

JPanel topPanel = new JPanel();
topPanel.setBorder(BorderFactory.createTitledBorder("Student Management"));
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

// Adding padding to text fields
        BorderFactory.createEmptyBorder(5, 5, 5, 5)));

        BorderFactory.createEmptyBorder(5, 5, 5, 5)));

// Other components initialization...

JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

// Adding the topPanel and bottomPanel to the main frame
add(topPanel, BorderLayout.NORTH);
add(bottomPanel, BorderLayout.SOUTH);

The Conclusion

By implementing these enhancements, we've significantly improved our Student Management System. Input validation ensures data integrity, the search functionality allows for easy filtering of students, enhanced error handling provides better user feedback, and improved styling makes the application more user-friendly.

These steps not only make the application more robust and user-friendly but also provide a deeper understanding of advanced Java programming techniques and best practices. Continue exploring and adding more features to further refine your Java skills.

In case you want to learn a new topic or subject, we offer recommendations on what to read next. Here is one of our recommendations to learn Kubernetes.

What is Kubernetes? The Next Major Application Platform
Containerization has revolutionized the way applications are developed, deployed, and managed in modern computing environments. Containers provide lightweight, portable, and isolated runtime environments, enabling developers to package applications and their dependencies into consistent units that can run reliably across different infrastructure platforms. However, as organizations adopt containerized architectures at scale,

The CoderOasis Community

Did you know we have a Community Forums and Discord Server? which we invite everyone to join us? Want to discuss this article with other members of our community? Want to join a laid back place to chill and discuss topics like programming, cybersecurity, web development, and Linux? Consider joining us today!

Article Comments