import javax.swing.
*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class VeterinariaAppGUI {
private static final String URL =
"jdbc:mariadb://localhost:3306/SistemaVeterinaria";
private static final String USER = "root";
private static final String PASSWORD = "password";
private Connection connection;
public static void main(String[] args) {
SwingUtilities.invokeLater(VeterinariaAppGUI::new);
}
public VeterinariaAppGUI() {
try {
connection = DriverManager.getConnection(URL, USER, PASSWORD);
System.out.println("Conexión exitosa a la base de datos.");
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Error en la conexión: " +
e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
return;
}
JFrame frame = new JFrame("Sistema Veterinaria");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 2, 10, 10));
JButton btnVerClientes = new JButton("Ver Clientes");
JButton btnVerMascotas = new JButton("Ver Mascotas");
JButton btnAgregarCliente = new JButton("Agregar Cliente");
JButton btnAgregarMascota = new JButton("Agregar Mascota");
panel.add(btnVerClientes);
panel.add(btnVerMascotas);
panel.add(btnAgregarCliente);
panel.add(btnAgregarMascota);
frame.add(panel, BorderLayout.CENTER);
// Acciones de los botones
btnVerClientes.addActionListener(e -> mostrarDatos("Cliente", "SELECT *
FROM Cliente"));
btnVerMascotas.addActionListener(e -> mostrarDatos("Mascota", "SELECT *
FROM Mascota"));
btnAgregarCliente.addActionListener(e -> agregarCliente());
btnAgregarMascota.addActionListener(e -> agregarMascota());
frame.setVisible(true);
}
private void mostrarDatos(String titulo, String query) {
try (Statement stmt = connection.createStatement(); ResultSet rs =
stmt.executeQuery(query)) {
JTable table = new JTable(buildTableModel(rs));
JOptionPane.showMessageDialog(null, new JScrollPane(table), "Lista de "
+ titulo, JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Error al obtener datos: " +
e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void agregarCliente() {
JPanel panel = new JPanel(new GridLayout(0, 1));
JTextField txtNombre = new JTextField();
JTextField txtApellido = new JTextField();
JTextField txtDireccion = new JTextField();
JTextField txtTelefono = new JTextField();
JTextField txtEmail = new JTextField();
panel.add(new JLabel("Nombre:"));
panel.add(txtNombre);
panel.add(new JLabel("Apellido:"));
panel.add(txtApellido);
panel.add(new JLabel("Dirección:"));
panel.add(txtDireccion);
panel.add(new JLabel("Teléfono:"));
panel.add(txtTelefono);
panel.add(new JLabel("Email:"));
panel.add(txtEmail);
int result = JOptionPane.showConfirmDialog(null, panel, "Agregar Cliente",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String sql = "INSERT INTO Cliente (nombre, apellido, direccion,
telefono, email) VALUES (?, ?, ?, ?, ?)";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, txtNombre.getText());
stmt.setString(2, txtApellido.getText());
stmt.setString(3, txtDireccion.getText());
stmt.setString(4, txtTelefono.getText());
stmt.setString(5, txtEmail.getText());
stmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Cliente agregado
correctamente.");
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Error al agregar cliente: " +
e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private void agregarMascota() {
JPanel panel = new JPanel(new GridLayout(0, 1));
JTextField txtNombre = new JTextField();
JTextField txtEspecie = new JTextField();
JTextField txtRaza = new JTextField();
JTextField txtEdad = new JTextField();
JTextField txtIdCliente = new JTextField();
panel.add(new JLabel("Nombre:"));
panel.add(txtNombre);
panel.add(new JLabel("Especie:"));
panel.add(txtEspecie);
panel.add(new JLabel("Raza:"));
panel.add(txtRaza);
panel.add(new JLabel("Edad:"));
panel.add(txtEdad);
panel.add(new JLabel("ID Cliente:"));
panel.add(txtIdCliente);
int result = JOptionPane.showConfirmDialog(null, panel, "Agregar Mascota",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
String sql = "INSERT INTO Mascota (nombre, especie, raza, edad,
id_cliente) VALUES (?, ?, ?, ?, ?)";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, txtNombre.getText());
stmt.setString(2, txtEspecie.getText());
stmt.setString(3, txtRaza.getText());
stmt.setInt(4, Integer.parseInt(txtEdad.getText()));
stmt.setInt(5, Integer.parseInt(txtIdCliente.getText()));
stmt.executeUpdate();
JOptionPane.showMessageDialog(null, "Mascota agregada
correctamente.");
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Error al agregar mascota: " +
e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
private static javax.swing.table.DefaultTableModel buildTableModel(ResultSet
rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
String[] columnNames = new String[columnCount];
for (int i = 1; i <= columnCount; i++) {
columnNames[i - 1] = metaData.getColumnName(i);
}
java.util.Vector<Object[]> data = new java.util.Vector<>();
while (rs.next()) {
Object[] row = new Object[columnCount];
for (int i = 1; i <= columnCount; i++) {
row[i - 1] = rs.getObject(i);
}
data.add(row);
}
return new javax.swing.table.DefaultTableModel(data.toArray(new Object[0]
[]), columnNames);
}
}