Deep dive into the Model-View-Controller architecture
The MySQL SQL Editor implements the classic Model-View-Controller (MVC) design pattern to separate data management, user interface, and application logic.
public class Model { private Connection conexion; // Establish connection to a specific database public void conectar(String host, String database, String usuario, String password) throws SQLException // Close active database connection public void desconectar() throws SQLException // Retrieve all available databases (excluding system DBs) public List<String> obtenerTodasLasBasesDatos(String host, String usuario, String password) throws SQLException // Get tables from currently connected database public List<String> obtenerTablasDeBaseDatos() throws SQLException // Execute SQL query and return results as table model public DefaultTableModel ejecutarConsulta(String query) throws SQLException}
public class View extends JFrame { // Getters for user input public String getUsuario() public String getPassword() public String getServidor() public String getBaseDatosSeleccionada() // Button accessors for controller public JButton getBtnConectar() public JButton getBtnSalir() public JButton getBtnActualizarBases() // UI update methods public void setBasesDatos(List<String> bases) public void mostrarError(String mensaje) public void limpiarEstado() public void bloquearInterfaz(boolean bloquear)}
Custom Styling
The view uses custom color scheme matching the brand:
private final Color COLOR_PRIMARIO = new Color(0, 120, 215); // #0078D7private final Color COLOR_SECUNDARIO = new Color(100, 180, 255);private final Color COLOR_TEXTO = new Color(60, 60, 60);
public class SqlEditorView extends JFrame { // Input retrieval public String getConsulta() // Button accessors public JButton getBtnEjecutar() public JButton getBtnLimpiar() public JButton getBtnRefrescarTablas() public JButton getBtnCambiarBD() // UI updates public void setResultados(javax.swing.table.TableModel model) public void setMensajeSistema(String mensaje) public void limpiar() public void setBaseDeDatos(String nombreBD) public void actualizarListaTablas(List<String> tablas)}
Interactive Features
Double-clicking a table name auto-generates a SELECT query:
listaTablas.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { if (evt.getClickCount() == 2) { String tablaSeleccionada = listaTablas.getSelectedValue(); if (tablaSeleccionada != null) { String consulta = "SELECT * FROM " + tablaSeleccionada + " LIMIT 100"; txtConsulta.setText(consulta); } } }});
public class Controller { private final View loginView; private final SqlEditorView editorView; private final Model modelo; public Controller(View loginView, SqlEditorView editorView, Model model) { this.loginView = loginView; this.editorView = editorView; this.modelo = model; configurarListeners(); loginView.setVisible(true); }}
The Controller receives references to both views and the model, then registers event listeners for all user actions.
editorView.getBtnEjecutar().addActionListener(e -> ejecutarConsulta());editorView.getBtnLimpiar().addActionListener(e -> limpiarEditor());editorView.getBtnRefrescarTablas().addActionListener(e -> refrescarTablas());editorView.getBtnCambiarBD().addActionListener(e -> { int confirm = JOptionPane.showConfirmDialog(editorView, "¿Desea desconectarse y cambiar de base de datos?", "Confirmar", JOptionPane.YES_NO_OPTION); if (confirm == JOptionPane.YES_OPTION) { desconectar(); }});
Location: com.app.MainThe Main class bootstraps the MVC components:
public class Main { public static void main(String[] args) { // Instantiate the model Model model = new Model(); // Instantiate the login view View loginView = new View(); // Instantiate the SQL editor view SqlEditorView editorView = new SqlEditorView(); // Hide the editor initially editorView.setVisible(false); // Create the controller, connecting model and views new Controller(loginView, editorView, model); }}
1
Model Creation
A single Model instance is created to manage all database operations.
2
View Instantiation
Both views are created upfront. The editor view is hidden initially.
3
Controller Wiring
The Controller receives all three components and sets up the event handling infrastructure.
4
Display Login
The Controller shows the login view, and the application is ready for user interaction.