Programming & Development / April 11, 2025

How to Represent Canadian Provinces in Java and SQL with Province Codes

Java enum Canadian provinces province codes SQL table enum with code Spring Boot database seeding Canada ENUM in Java INSERT SQL province list

Java Enum: Canadian Provinces with Province Codes

When working with Canadian addresses in Java applications, representing provinces as an enum ensures type safety and consistency. Below is an example of a Java enum that includes both the province name and its official two-letter postal code.

java

public enum Province {
    ALBERTA("AB"),
    BRITISH_COLUMBIA("BC"),
    MANITOBA("MB"),
    NEW_BRUNSWICK("NB"),
    NEWFOUNDLAND_AND_LABRADOR("NL"),
    NORTHWEST_TERRITORIES("NT"),
    NOVA_SCOTIA("NS"),
    NUNAVUT("NU"),
    ONTARIO("ON"),
    PRINCE_EDWARD_ISLAND("PE"),
    QUEBEC("QC"),
    SASKATCHEWAN("SK"),
    YUKON("YT");

    private final String code;

    Province(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }
}

Usage Example:

java

Province province = Province.ONTARIO;
System.out.println(province.getCode()); // Outputs: ON

This approach simplifies validation and integration with dropdowns, forms, or APIs requiring province information.

SQL Version: Canadian Provinces Table and Data

To store province data in a database, you can create a provinces table with name and code columns. Here’s the SQL schema and insert statements:

sql

CREATE TABLE provinces (
    id SERIAL PRIMARY KEY,
    name VARCHAR(64) NOT NULL,
    code CHAR(2) NOT NULL UNIQUE
);

Insert Statements:

sql

INSERT INTO provinces (name, code) VALUES
('Alberta', 'AB'),
('British Columbia', 'BC'),
('Manitoba', 'MB'),
('New Brunswick', 'NB'),
('Newfoundland and Labrador', 'NL'),
('Northwest Territories', 'NT'),
('Nova Scotia', 'NS'),
('Nunavut', 'NU'),
('Ontario', 'ON'),
('Prince Edward Island', 'PE'),
('Quebec', 'QC'),
('Saskatchewan', 'SK'),
('Yukon', 'YT');

This structure supports dropdowns, relational integrity (via foreign keys), and filtering in your backend services.

Summary

Using an enum in Java for Canadian provinces with codes ensures code safety, while the SQL version allows persistence and use in web or enterprise applications. Together, they form a solid foundation for address handling in Canadian software systems.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex