Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package aseguradora; import java.io.Serializable; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Iterator; import java.util.List; import java.util.Vector; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.table.DefaultTableModel; import org.hibernate.Hibernate; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import pojo.Hospital; import pojo.Medico; import net.proteanit.sql.DbUtils; /** * * @author Exode-P */ public class HospitalVentana extends javax.swing.JFrame implements Serializable { /** * Creates new form Hospital */ private ConnectOracle conection; private SessionFactory sesion = SessionFactoryUtil.getSessionFactory(); public HospitalVentana(ConnectOracle co) { conection = co; initComponents(); cargarHospital(); } public HospitalVentana() { } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); tablaHospital = new javax.swing.JTable() { public boolean isCellEditable(int row, int column) { return false; } }; lblCodHospital = new javax.swing.JLabel(); etCodHospital = new javax.swing.JTextField(); lblNomHospital = new javax.swing.JLabel(); etNomHospital = new javax.swing.JTextField(); lblNumC = new javax.swing.JLabel(); etNumC = new javax.swing.JTextField(); btnActualizar = new javax.swing.JButton(); btnEliminar = new javax.swing.JButton(); btnBuscar = new javax.swing.JButton(); btnCerrar = new javax.swing.JButton(); btnRefrescar = new javax.swing.JButton(); btnInsertar = new javax.swing.JButton(); lblValidaCodHospital = new javax.swing.JLabel(); lblValidaNomHospital = new javax.swing.JLabel(); lblValidaNumC = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Aseguradora La Vida - Hospitales"); tablaHospital .setModel(new javax.swing.table.DefaultTableModel( new Object[][] { { null, null, null, null }, { null, null, null, null }, { null, null, null, null }, { null, null, null, null } }, new String[] { "Title 1", "Title 2", "Title 3", "Title 4" })); tablaHospital.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { tablaHospitalMouseClicked(evt); } }); jScrollPane1.setViewportView(tablaHospital); lblCodHospital.setText("Cdigo de Hospital:"); etCodHospital.setHorizontalAlignment(javax.swing.JTextField.CENTER); etCodHospital.setToolTipText("Inserta un cdigo de mdico"); etCodHospital.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { etCodHospitalFocusGained(evt); } public void focusLost(java.awt.event.FocusEvent evt) { etCodHospitalFocusLost(evt); } }); etCodHospital.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { etCodHospitalKeyTyped(evt); } }); lblNomHospital.setText("Nombre de Hospital:"); etNomHospital.setHorizontalAlignment(javax.swing.JTextField.CENTER); etNomHospital.setToolTipText("Inserta un nombre de mdico"); etNomHospital.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { etNomHospitalFocusGained(evt); } public void focusLost(java.awt.event.FocusEvent evt) { etNomHospitalFocusLost(evt); } }); lblNumC.setText("Nmero de C:"); etNumC.setHorizontalAlignment(javax.swing.JTextField.CENTER); etNumC.setToolTipText("Inserta un cdigo de hospital"); etNumC.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { etNumCFocusGained(evt); } public void focusLost(java.awt.event.FocusEvent evt) { etNumCFocusLost(evt); } }); etNumC.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { etNumCKeyTyped(evt); } }); btnActualizar.setText("Actualizar"); btnActualizar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnActualizarActionPerformed(evt); } }); btnEliminar.setText("Eliminar"); btnEliminar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEliminarActionPerformed(evt); } }); btnBuscar.setText("Buscar"); btnBuscar.setMaximumSize(new java.awt.Dimension(79, 23)); btnBuscar.setMinimumSize(new java.awt.Dimension(79, 23)); btnBuscar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnBuscarActionPerformed(evt); } }); btnCerrar.setText("Cerrar"); btnCerrar.setMaximumSize(new java.awt.Dimension(79, 23)); btnCerrar.setMinimumSize(new java.awt.Dimension(79, 23)); btnCerrar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCerrarActionPerformed(evt); } }); btnRefrescar.setText("Refrescar"); btnRefrescar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnRefrescarActionPerformed(evt); } }); btnInsertar.setText("Insertar"); btnInsertar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnInsertarActionPerformed(evt); } }); lblValidaCodHospital.setForeground(new java.awt.Color(255, 0, 0)); lblValidaNomHospital.setForeground(new java.awt.Color(255, 0, 0)); lblValidaNumC.setForeground(new java.awt.Color(255, 0, 0)); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout .createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(lblCodHospital) .addComponent(lblNomHospital).addComponent(lblNumC)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent( etNomHospital, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE) .addComponent(etNumC).addComponent(etCodHospital)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lblValidaNomHospital) .addComponent(lblValidaNumC)) .addGap(0, 0, Short.MAX_VALUE)) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(btnRefrescar)) .addGroup(jPanel1Layout .createSequentialGroup() .addComponent(lblValidaCodHospital) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout .createSequentialGroup() .addComponent(btnInsertar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18).addComponent(btnActualizar) .addGap(18, 18, 18) .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(7, 7, 7) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(etCodHospital, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblCodHospital)) .addGap(7, 7, 7) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(etNomHospital, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblValidaNomHospital).addComponent(lblNomHospital)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(etNumC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblNumC).addComponent(lblValidaNumC))) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btnBuscar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lblValidaCodHospital)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnRefrescar))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnInsertar).addComponent(btnActualizar) .addComponent(btnCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnEliminar)) .addContainerGap())); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void tablaHospitalMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaHospitalMouseClicked int row = tablaHospital.getSelectedRow(); etCodHospital.setText(tablaHospital.getModel().getValueAt(row, 0).toString()); etNomHospital.setText(tablaHospital.getModel().getValueAt(row, 1).toString()); etNumC.setText(tablaHospital.getModel().getValueAt(row, 2).toString()); etCodHospital.setHorizontalAlignment(JTextField.CENTER); etNomHospital.setHorizontalAlignment(JTextField.CENTER); etNumC.setHorizontalAlignment(JTextField.CENTER); }//GEN-LAST:event_tablaHospitalMouseClicked private void btnActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnActualizarActionPerformed if (validarCampos()) { actualizarHospital(); resetCampos(); } }//GEN-LAST:event_btnActualizarActionPerformed private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed JLabel label = new JLabel("Deseas eliminar este hospital?"); JTextField tidhospital = new JTextField(etCodHospital.getText().toString()); JTextField tnombre = new JTextField(etNomHospital.getText().toString()); JTextField tnumc = new JTextField(etNumC.getText().toString()); tidhospital.setEditable(false); tnombre.setEditable(false); tnumc.setEditable(false); Object[] componentsArray = { label, tidhospital, tnombre, tnumc }; confirmarEliminar = JOptionPane.showConfirmDialog(null, componentsArray, "Eliminar Hospital", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE); if (confirmarEliminar == JOptionPane.OK_OPTION) { eliminarHospital(); resetCampos(); } }//GEN-LAST:event_btnEliminarActionPerformed private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed JLabel lnombre = new JLabel("Introduzca el nombre del Hospital: \n"); JTextField tnombre = new JTextField(null); Object[] componentsArray = { lnombre, tnombre }; busqueda = JOptionPane.showConfirmDialog(null, componentsArray, "BSQUEDA HOSPITALES:", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE); if (busqueda == JOptionPane.OK_OPTION) { String nombreBusqueda = tnombre.getText().toString(); if (!nombreBusqueda.equals("")) { buscarHospital(nombreBusqueda); } } }//GEN-LAST:event_btnBuscarActionPerformed private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed this.dispose(); }//GEN-LAST:event_btnCerrarActionPerformed private void btnRefrescarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefrescarActionPerformed cargarHospital(); }//GEN-LAST:event_btnRefrescarActionPerformed private void btnInsertarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsertarActionPerformed if (validarCampos()) { insertarHospital(); resetCampos(); } }//GEN-LAST:event_btnInsertarActionPerformed private void etCodHospitalFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_etCodHospitalFocusGained etCodHospital.setHorizontalAlignment(JTextField.LEADING); }//GEN-LAST:event_etCodHospitalFocusGained private void etNomHospitalFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_etNomHospitalFocusGained etNomHospital.setHorizontalAlignment(JTextField.LEADING); }//GEN-LAST:event_etNomHospitalFocusGained private void etNumCFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_etNumCFocusGained etNumC.setHorizontalAlignment(JTextField.LEADING); }//GEN-LAST:event_etNumCFocusGained private void etCodHospitalFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_etCodHospitalFocusLost etCodHospital.setHorizontalAlignment(JTextField.CENTER); }//GEN-LAST:event_etCodHospitalFocusLost private void etNomHospitalFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_etNomHospitalFocusLost etNomHospital.setHorizontalAlignment(JTextField.CENTER); }//GEN-LAST:event_etNomHospitalFocusLost private void etNumCFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_etNumCFocusLost etNumC.setHorizontalAlignment(JTextField.CENTER); }//GEN-LAST:event_etNumCFocusLost private void etCodHospitalKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_etCodHospitalKeyTyped char car = evt.getKeyChar(); if (!(Character.isDigit(car)) && (car != evt.VK_BACK_SPACE) && (car != evt.VK_DELETE)) { evt.consume(); JOptionPane.showMessageDialog(null, "Solo se permite introducir nmeros."); } }//GEN-LAST:event_etCodHospitalKeyTyped private void etNumCKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_etNumCKeyTyped char car = evt.getKeyChar(); if (!(Character.isDigit(car)) && (car != evt.VK_BACK_SPACE) && (car != evt.VK_DELETE)) { evt.consume(); JOptionPane.showMessageDialog(null, "Solo se permite introducir nmeros."); } }//GEN-LAST:event_etNumCKeyTyped /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(HospitalVentana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(HospitalVentana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(HospitalVentana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(HospitalVentana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new HospitalVentana().setVisible(true); } }); } public void cargarHospital() { try { String[] columnNames = new String[3]; DefaultTableModel model = new DefaultTableModel(columnNames, 0); Session session = sesion.openSession(); Hospital hos = new Hospital(); Query cons = session.createQuery("from pojo.Hospital"); List<Hospital> lista = cons.list(); Iterator<Hospital> iter = lista.iterator(); while (iter.hasNext()) { hos = (Hospital) iter.next(); Hibernate.initialize(hos.getCodH()); Vector row = new Vector(); row.add(hos.getCodH()); row.add(hos.getNH()); row.add(hos.getNumC()); model.addRow(row); } tablaHospital.setModel(model); session.close(); } catch (HibernateException e) { e.printStackTrace(); if (e.getMessage().contains("" + TABLA_NO_ENCONTRADA)) { JOptionPane.showMessageDialog(rootPane, "Tabla no encontrada. Pongase en contacto con el administrador."); } } } private void buscarHospital(String nombre) { Session session = sesion.openSession(); Hospital hos; Query cons = session.createQuery("from pojo.Hospital as hos " + "where upper(hos.NH) LIKE ?"); cons.setString(0, "%" + nombre.toUpperCase() + "%"); List<Hospital> lista = cons.list(); DefaultTableModel model = (DefaultTableModel) tablaHospital.getModel(); if (!lista.isEmpty()) { model.getDataVector().removeAllElements(); model.fireTableDataChanged(); Iterator<Hospital> iter = lista.iterator(); while (iter.hasNext()) { hos = (Hospital) iter.next(); Hibernate.initialize(hos.getCodH()); Vector row = new Vector(); row.add(hos.getCodH()); row.add(hos.getNH()); row.add(hos.getNumC()); model.addRow(row); } tablaHospital.setModel(model); session.close(); } else { model.getDataVector().removeAllElements(); model.fireTableDataChanged(); JOptionPane.showMessageDialog(null, "No hay resultados para su bsqueda", "Informacin", JOptionPane.ERROR_MESSAGE); cargarHospital(); } } private void insertarHospital() { try { Session session = sesion.openSession(); Transaction tx = session.beginTransaction(); Hospital hospital = new Hospital(); hospital.setCodH(Short.parseShort(etCodHospital.getText().toString())); hospital.setNH(etNomHospital.getText().toString()); hospital.setNumC(Short.parseShort(etNumC.getText())); session.save(hospital); tx.commit(); session.close(); cargarHospital(); } catch (HibernateException e) { e.printStackTrace(); if (e.getMessage().contains("" + CLAVE_PRIMARIA_DUPLICADA)) { JOptionPane.showMessageDialog(rootPane, "Existe un hospital con ese identificador (ID)"); } if (e.getMessage().contains("" + VALOR_DEMASIADO_LARGO)) { JOptionPane.showMessageDialog(rootPane, "Has introducido un valor demasiado largo"); } } } private void actualizarHospital() { int row = tablaHospital.getSelectedRow(); try { Session session = sesion.openSession(); Transaction tx = session.beginTransaction(); Hospital hospital = new Hospital(); Short cod_h = (row == -1) ? Short.parseShort(etCodHospital.getText().toString()) : Short.parseShort(tablaHospital.getModel().getValueAt(row, 0).toString()); hospital = (Hospital) session.load(Hospital.class, cod_h); hospital.setNH(etNomHospital.getText().toString()); hospital.setNumC(Short.parseShort(etNumC.getText().toString())); session.update(hospital); tx.commit(); session.close(); cargarHospital(); } catch (HibernateException e) { e.printStackTrace(); if (e.getMessage().contains("" + CLAVE_PRIMARIA_DUPLICADA)) { JOptionPane.showMessageDialog(rootPane, "Existe un hospital con ese identificador (ID)"); } if (e.getMessage().contains("" + VALOR_DEMASIADO_LARGO)) { JOptionPane.showMessageDialog(rootPane, "Has introducido un valor demasiado largo"); } ; if (e.getMessage().contains("" + ERROR_EDITAR_CLAVE_AJENA)) { JOptionPane.showMessageDialog(rootPane, "No puedes editar el id de este hospital"); } ; } } private void eliminarHospital() { int row = tablaHospital.getSelectedRow(); try { Session session = sesion.openSession(); Transaction tx = session.beginTransaction(); Hospital hospital = new Hospital(); Short cod_h = (row == -1) ? Short.parseShort(etCodHospital.getText().toString()) : Short.parseShort(tablaHospital.getModel().getValueAt(row, 0).toString()); hospital = (Hospital) session.load(Hospital.class, cod_h); session.delete(hospital); tx.commit(); session.close(); cargarHospital(); } catch (HibernateException e) { e.printStackTrace(); if (e.getMessage().contains("" + ERROR_EDITAR_CLAVE_AJENA)) { JOptionPane.showMessageDialog(rootPane, "No puedes borrar este hospital, restriccin clave primeria/ajena"); } } } private boolean validarCampos() { boolean CodHospitalVacio = etCodHospital.getText().equals(""); boolean NomHospitalVacio = etNomHospital.getText().equals(""); boolean CodNumC = etNumC.getText().equals(""); if (CodHospitalVacio) { lblValidaCodHospital.setText("Rellena el campo cdigo"); } else { lblValidaCodHospital.setText(""); } if (NomHospitalVacio) { lblValidaNomHospital.setText("Rellena el campo nombre"); } else { lblValidaNomHospital.setText(""); } if (CodNumC) { lblValidaNumC.setText("Rellena el campo"); } else { lblValidaNumC.setText(""); } if (CodHospitalVacio || NomHospitalVacio || CodNumC) { return false; } else { return true; } } private void resetCampos() { etCodHospital.setText(""); etNomHospital.setText(""); etNumC.setText(""); tablaHospital.clearSelection(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnActualizar; private javax.swing.JButton btnBuscar; private javax.swing.JButton btnCerrar; private javax.swing.JButton btnEliminar; private javax.swing.JButton btnInsertar; private javax.swing.JButton btnRefrescar; private javax.swing.JTextField etCodHospital; private javax.swing.JTextField etNomHospital; private javax.swing.JTextField etNumC; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JLabel lblCodHospital; private javax.swing.JLabel lblNomHospital; private javax.swing.JLabel lblNumC; private javax.swing.JLabel lblValidaCodHospital; private javax.swing.JLabel lblValidaNomHospital; private javax.swing.JLabel lblValidaNumC; private javax.swing.JTable tablaHospital; // End of variables declaration//GEN-END:variables private int busqueda; private int confirmarEliminar; private final int CLAVE_PRIMARIA_DUPLICADA = 1; private final int CLAVE_AJENA_NO_ENCONTRADA = 2291; private final int VALOR_DEMASIADO_LARGO = 1438; private final int TABLA_NO_ENCONTRADA = 942; private final int ERROR_EDITAR_CLAVE_AJENA = 2292; // private final int CAMPO_NO_NULO = }