Component Overview
Main
Application entry point
Model
Data access layer
View
Login interface
SqlEditorView
SQL editor interface
Controller
Application coordinator
Main
Package:com.appFile:
Main.javaLine Reference: Main.java:15-38
Purpose
The Main class serves as the application entry point. It initializes all MVC components and establishes the initial application state.Class Structure
Methods
main(String[] args)
main(String[] args)
Signature:
public static void main(String[] args)Parameters:args- Command line arguments (not utilized in this implementation)
voidDescription:Entry point for the application. Instantiates the Model, both View components, hides the editor view initially, and creates the Controller to wire everything together.Implementation:Dependencies
com.app.Model.Modelcom.app.View.Viewcom.app.View.SqlEditorViewcom.app.Controller.Controller
Role in System
Main orchestrates the bootstrap sequence and ensures all components are created in the correct order. The Controller is created last so it can reference all other components when setting up event listeners.Model
Package:com.app.ModelFile:
Model.javaLine Reference: Model.java:12-192
Purpose
The Model class is the data access layer responsible for all database interactions. It manages JDBC connections, executes SQL queries, retrieves metadata, and transforms database results into UI-friendly formats.Class Structure
Instance Variables
private Connection conexion- Active JDBC connection to the MySQL database
Methods
obtenerTodasLasBasesDatos()
obtenerTodasLasBasesDatos()
Signature:
public List<String> obtenerTodasLasBasesDatos(String host, String usuario, String password) throws SQLExceptionParameters:host- MySQL server address (e.g., “localhost” or “127.0.0.1”)usuario- Database usernamepassword- Database password
List<String> - List of database names (excluding system databases)Throws: SQLException - If connection or query failsDescription:Connects to the MySQL server and retrieves all available databases, filtering out system databases (information_schema, mysql, performance_schema, sys).Implementation Detail:This method creates a temporary connection to retrieve the database list and doesn’t affect the instance’s
conexion field.conectar()
conectar()
Signature: Usage Example:
public void conectar(String host, String database, String usuario, String password) throws SQLExceptionParameters:host- MySQL server addressdatabase- Specific database name to connect tousuario- Database usernamepassword- Database password
voidThrows: SQLException - If connection failsDescription:Establishes a persistent connection to a specific MySQL database. The connection is stored in the conexion instance variable for subsequent queries.Implementation Detail:desconectar()
desconectar()
Signature:
public void desconectar() throws SQLExceptionParameters: NoneReturns: voidThrows: SQLException - If closing the connection failsDescription:Closes the active database connection if one exists and is not already closed.Implementation Detail:obtenerTablasDeBaseDatos()
obtenerTablasDeBaseDatos()
Signature:
public List<String> obtenerTablasDeBaseDatos() throws SQLExceptionParameters: NoneReturns: List<String> - List of table names in the currently connected databaseThrows: SQLException - If metadata retrieval failsDescription:Retrieves all table names from the currently connected database using JDBC metadata.Implementation Detail:This method requires an active connection established via
conectar().ejecutarConsulta()
ejecutarConsulta()
Signature: Usage Example:
public DefaultTableModel ejecutarConsulta(String query) throws SQLExceptionParameters:query- SQL query string to execute
DefaultTableModel - Swing table model containing query resultsThrows: SQLException - If query execution failsDescription:Executes any SQL query (SELECT, INSERT, UPDATE, DELETE, CREATE, etc.) and returns the results as a DefaultTableModel suitable for display in a JTable.Behavior:- SELECT queries: Returns the result set as a table model with columns and rows
- Modification queries (INSERT, UPDATE, DELETE): Executes the query, extracts the affected table name, and returns the updated table contents
- Other queries (CREATE, DROP, TRUNCATE): Returns a single-column table showing affected row count
extraerNombreTabla()
extraerNombreTabla()
Signature:
private String extraerNombreTabla(String query)Parameters:query- Lowercase SQL query string
String - Table name if identifiable, otherwise nullDescription:Helper method that extracts the table name from various SQL statements (CREATE TABLE, INSERT INTO, UPDATE, DELETE FROM, TRUNCATE TABLE) to enable showing updated table contents after modifications.Supported Patterns:CREATE TABLE table_name ...INSERT INTO table_name ...UPDATE table_name ...DELETE FROM table_name ...TRUNCATE TABLE table_name
Dependencies
java.sql.*- JDBC for database connectivityjavax.swing.table.DefaultTableModel- For UI data representation
Role in System
The Model is the single source of truth for all database operations. It abstracts away JDBC complexity and provides clean, high-level methods that the Controller can call without worrying about SQL implementation details.View
Package:com.app.ViewFile:
View.javaLine Reference: View.java:11-253
Purpose
The View class implements the login interface where users enter connection credentials and select a database.Class Structure
Constructor
View()
View()
Signature: UI Components Created:
public View()Description:Constructs the login window with all UI components, applies custom styling, and sets up the layout using GridBagLayout.Window Configuration:- Server selection combo box (localhost, 127.0.0.1)
- Username text field
- Password field
- Database selection combo box with refresh button
- Connect and Exit buttons
- Status label for error messages
Methods
Getter Methods
Getter Methods
User Input Retrieval:Button Access:These methods allow the Controller to retrieve user input and register event listeners.
setBasesDatos()
setBasesDatos()
Signature:
public void setBasesDatos(List<String> bases)Parameters:bases- List of database names to populate the dropdown
voidDescription:Updates the database selection combo box with a new list of databases.Implementation:mostrarError()
mostrarError()
Signature:
public void mostrarError(String mensaje)Parameters:mensaje- Error message to display
voidDescription:Displays an error message in red text below the form fields.Implementation:limpiarEstado()
limpiarEstado()
Signature:
public void limpiarEstado()Returns: voidDescription:Clears the error message label.Implementation:bloquearInterfaz()
bloquearInterfaz()
Signature:
public void bloquearInterfaz(boolean bloquear)Parameters:bloquear-trueto disable UI,falseto enable
voidDescription:Enables or disables all input components and buttons. Used during asynchronous operations to prevent multiple simultaneous requests.Implementation:estilizarBoton()
estilizarBoton()
Signature:
private void estilizarBoton(JButton boton, boolean primario)Parameters:boton- Button to styleprimario-truefor primary button style,falsefor secondary
voidDescription:Applies custom styling including colors, fonts, and hover effects to buttons.Features:- Primary buttons: #0078D7 background, white text, larger size
- Secondary buttons: Lighter blue background, dark text
- Hover effect: Darkens background on mouse enter
Dependencies
java.awt.*- Layout and color managementjavax.swing.*- UI componentsjava.util.List- Database list parameter
Role in System
The View provides the initial user interface for authentication. It collects credentials, allows database selection, and displays connection status. The Controller registers listeners on View’s buttons to handle user interactions.SqlEditorView
Package:com.app.ViewFile:
SqlEditorView.javaLine Reference: SqlEditorView.java:15-318
Purpose
The SqlEditorView class implements the SQL editor interface where users write queries, execute them, and view results.Class Structure
Constructor
SqlEditorView()
SqlEditorView()
Signature: Layout Structure:
public SqlEditorView()Description:Constructs the SQL editor window with a split-pane layout: top section for query input and controls, bottom section for results display.Window Configuration:- Top Panel: SQL query text area with monospace font, database selector, action buttons, and table list
- Bottom Panel: Results table and system message label
- Split Pane: Vertical divider allowing resizing between top and bottom sections
- Double-click on a table name generates
SELECT * FROM table_name LIMIT 100query - Monospace font (Consolas) for SQL query area
- Resizable columns in results table
Methods
Getter Methods
Getter Methods
Button Access:Query Text:Returns the current SQL query text from the text area.
setResultados()
setResultados()
Signature:
public void setResultados(javax.swing.table.TableModel model)Parameters:model- Table model containing query results
voidDescription:Updates the results table with query data and sets preferred column widths.Implementation:setMensajeSistema()
setMensajeSistema()
Signature:
public void setMensajeSistema(String mensaje)Parameters:mensaje- Status message to display
voidDescription:Displays a system message below the results table (e.g., “Query executed successfully” or error details).Implementation:limpiar()
limpiar()
Signature:
public void limpiar()Returns: voidDescription:Clears the query text area, results table, and system message.Implementation:setBaseDeDatos()
setBaseDeDatos()
Signature:
public void setBaseDeDatos(String nombreBD)Parameters:nombreBD- Database name ornullfor disconnected state
voidDescription:Updates the database status text field to show current connection.Implementation:actualizarListaTablas()
actualizarListaTablas()
Signature:
public void actualizarListaTablas(List<String> tablas)Parameters:tablas- List of table names to display
voidDescription:Refreshes the table list shown in the right panel.Implementation:estilizarBoton()
estilizarBoton()
Signature:
private void estilizarBoton(JButton boton, boolean primario)Parameters:boton- Button to styleprimario- Primary or secondary style
voidDescription:Applies consistent styling with hover effects. Includes hand cursor for better UX.Dependencies
java.awt.*- Layout and UI componentsjavax.swing.*- Swing componentsjavax.swing.table.DefaultTableModel- Results displayjava.util.List- Table list parameter
Role in System
SqlEditorView is the main working interface where users interact with the database. It provides query editing, execution feedback, and results visualization. The Controller wires up all button actions to Model operations.Controller
Package:com.app.ControllerFile:
Controller.javaLine Reference: Controller.java:20-272
Purpose
The Controller class is the central coordinator that connects the Model and Views, handles all user events, manages application state, and orchestrates asynchronous operations.Class Structure
Constructor
Controller(View, SqlEditorView, Model)
Controller(View, SqlEditorView, Model)
Signature:
public Controller(View loginView, SqlEditorView editorView, Model model)Parameters:loginView- Reference to the login interfaceeditorView- Reference to the SQL editor interfacemodel- Reference to the data access layer
Methods
configurarListeners()
configurarListeners()
Signature:
private void configurarListeners()Returns: voidDescription:Registers ActionListeners for all buttons in both views. This method is called once during initialization.Registered Listeners:btnActualizarBases→cargarBasesDatos()btnConectar→conectarABaseDatos()btnSalir→System.exit(0)btnEjecutar→ejecutarConsulta()btnLimpiar→limpiarEditor()btnRefrescarTablas→refrescarTablas()btnCambiarBD→desconectar()(with confirmation)
validarCamposLogin()
validarCamposLogin()
Signature:
private boolean validarCamposLogin(boolean soloCredenciales)Parameters:soloCredenciales- Iftrue, only validates username/password; iffalse, also validates database selection
boolean - true if validation passes, false otherwiseDescription:Validates user input in the login form before attempting database operations.Validation Rules:- Username must not be empty
- Password must not be empty
- If
soloCredencialesisfalse, a database must be selected
loginView.mostrarError() if validation fails.cargarBasesDatos()
cargarBasesDatos()
Signature:
private void cargarBasesDatos()Returns: voidDescription:Loads the list of available databases from the MySQL server. Uses SwingWorker for asynchronous execution.Process:- Validates credentials
- Disables UI (
bloquearInterfaz(true)) - Calls
modelo.obtenerTodasLasBasesDatos()in background thread - Updates dropdown on success or shows error on failure
- Re-enables UI
conectarABaseDatos()
conectarABaseDatos()
Signature:
private void conectarABaseDatos()Returns: voidDescription:Establishes connection to the selected database and transitions to the editor view. Uses SwingWorker.Process:- Disables login UI
- Calls
modelo.conectar()in background - On success:
- Loads table list via
modelo.obtenerTablasDeBaseDatos() - Updates editor view with database name and tables
- Hides login view, shows editor view
- Loads table list via
- On failure:
- Shows error in login view
- Clears editor view state
refrescarTablas()
refrescarTablas()
Signature:
private void refrescarTablas()Returns: voidDescription:Refreshes the list of tables in the current database. Uses SwingWorker.Implementation:desconectar()
desconectar()
Signature:
private void desconectar()Returns: voidDescription:Closes the database connection and returns to the login view.Implementation:ejecutarConsulta()
ejecutarConsulta()
Signature:
private void ejecutarConsulta()Returns: voidDescription:Executes the SQL query from the editor text area. Uses SwingWorker for async execution.Process:- Validates that query is not empty
- Calls
modelo.ejecutarConsulta()in background thread - On success:
- Displays results in table via
editorView.setResultados() - Shows success message
- Displays results in table via
- On failure:
- Displays error message in status label and dialog
limpiarEditor()
limpiarEditor()
Signature:
private void limpiarEditor()Returns: voidDescription:Clears the editor’s query text, results, and status message.Implementation:Dependencies
com.app.Model.Model- Data accesscom.app.View.View- Login interfacecom.app.View.SqlEditorView- Editor interfacejava.sql.SQLException- Exception handlingjavax.swing.SwingWorker- Asynchronous operationsjavax.swing.JOptionPane- User dialogs
Role in System
The Controller is the heart of application logic. It:- Translates user actions into Model operations
- Manages view transitions (login → editor)
- Handles validation and error display
- Ensures UI responsiveness via SwingWorker
- Maintains application state consistency
All database operations are executed asynchronously to prevent UI freezing, which is critical for a responsive desktop application.
Component Interaction Summary
Key Design Patterns
MVC Pattern
Clear separation between data (Model), UI (View), and logic (Controller)
Observer Pattern
Controller observes View events via ActionListeners
Facade Pattern
Model provides a simple interface hiding JDBC complexity
Worker Thread Pattern
SwingWorker handles background operations with EDT updates
Further Reading
Architecture Overview
Return to high-level architecture documentation
MVC Pattern Details
Deep dive into MVC implementation