Skip to main content
The MySQL SQL Editor is built using a clean Model-View-Controller (MVC) architectural pattern, implemented in Java Swing. This architecture ensures separation of concerns, maintainability, and scalability.

Architecture Diagram

Core Components

Model

Manages database connections and SQL operations using JDBC

View

Two Swing-based views: login interface and SQL editor interface

Controller

Coordinates user actions, connects views to model logic

Package Structure

The application is organized into a clear package hierarchy:
Contains the Main.java entry point that bootstraps the entire application.
Contains Model.java - the data access layer that handles all database operations including:
  • Connection management
  • Query execution
  • Metadata retrieval
Contains two view classes:
  • View.java - Login interface for database connection
  • SqlEditorView.java - Main SQL editor interface with query execution
Contains Controller.java which orchestrates interactions between the model and views.

Component Interaction Flow

1

Application Initialization

Main.java creates instances of Model, View, SqlEditorView, and Controller. The editor view is initially hidden.
Model model = new Model();
View loginView = new View();
SqlEditorView editorView = new SqlEditorView();
editorView.setVisible(false);
new Controller(loginView, editorView, model);
2

User Authentication

The Controller registers event listeners on the login view. When the user clicks “Connect”, the Controller validates credentials and calls Model.conectar().
3

Database Connection

The Model establishes a JDBC connection to the MySQL database and retrieves available tables using Model.obtenerTablasDeBaseDatos().
4

Editor Display

Upon successful connection, the Controller hides the login view and displays the SqlEditorView with the table list populated.
5

Query Execution

User enters SQL queries in the editor. The Controller calls Model.ejecutarConsulta() which returns a DefaultTableModel displayed in the results table.

Data Flow

UI to Database

All database operations follow this unidirectional flow, ensuring data consistency and clear responsibility boundaries.

Database to UI

When loading tables or refreshing data:
  1. Controller initiates a request (e.g., refrescarTablas())
  2. Model queries MySQL metadata using DatabaseMetaData
  3. Model returns a List<String> of table names
  4. Controller passes the list to the view
  5. View updates the UI components (JList, JTable)

Asynchronous Operations with SwingWorker

All potentially long-running database operations are executed asynchronously using SwingWorker to prevent UI freezing.
The Controller uses SwingWorker for:
  • Loading databases: obtenerTodasLasBasesDatos()
  • Connecting to database: conectar()
  • Executing queries: ejecutarConsulta()
  • Refreshing table list: obtenerTablasDeBaseDatos()

SwingWorker Pattern Example

SwingWorker<DefaultTableModel, Void> worker = new SwingWorker<>() {
    @Override
    protected DefaultTableModel doInBackground() throws Exception {
        // Database operation runs in background thread
        return modelo.ejecutarConsulta(editorView.getConsulta());
    }

    @Override
    protected void done() {
        // UI update runs on Event Dispatch Thread
        try {
            DefaultTableModel modelo = get();
            editorView.setResultados(modelo);
            editorView.setMensajeSistema("Consulta ejecutada correctamente");
        } catch (Exception ex) {
            editorView.setMensajeSistema("Error: " + ex.getMessage());
        }
    }
};
worker.execute();
This pattern ensures:
  • UI remains responsive during database operations
  • Proper thread safety (UI updates only on EDT)
  • Clean error handling with user feedback

Key Architectural Benefits

Separation of Concerns

Each component has a single, well-defined responsibility

Testability

Model can be tested independently of UI components

Maintainability

Changes to UI don’t affect business logic and vice versa

Scalability

New views or database operations can be added without restructuring

Next Steps

MVC Pattern Deep Dive

Learn how the MVC pattern is implemented in detail

Component Documentation

Explore each component’s methods and responsibilities

Build docs developers (and LLMs) love