Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Aprendiz
Bernabé Capera Loaiza
Instructor:
Introducción
Tablas
CITAS
Tabla
CONSULTORIOS
Tabla
MEDICO
Tabla
PACIENTES
Tabla
TRATAMIENTOS
Sesión 2: Desarrollando la Interfaz Gráfica de Usuario (GUI):
Sesión 3: Creando el Modelo de la Aplicación
CLASE PACIENTE
package Modelo;
public Paciente(String id, String nom, String ape, String fechaNac, String sex)
{
identificacion = id;
nombres = nom;
apellidos = ape;
fechaNacimiento = fechaNac;
sexo = sex;
}
public String getIdentificacion() {
return identificacion;
}
public void setIdentificacion(String identificacion) {
this.identificacion = identificacion;
}
public String getNombres() {
return nombres;
}
public void setNombres(String nombres) {
this.nombres = nombres;
}
public String getApellidos() {
return apellidos;
}
public void setApellidos(String apellidos) {
this.apellidos = apellidos;
}
switch(parametro)
{
case 1:
if(pac.getIdentificacion().equals(valor))
resultado.add(pac);
break;
case 2: if(pac.getNombres().equals(valor))
resultado.add(pac);
break;
case 3: if(pac.getApellidos().equals(valor))
resultado.add(pac);
break;
case 4: if(pac.getSexo().equals(valor))
resultado.add(pac);
break;
}
}
return resultado;
}
}
Sesión 4: Enlazando con el Controlador
package Modelo;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
*
* @author BERNA
*/
@Entity
@Table(name = "PACIENTES")
@NamedQueries({
@NamedQuery(name = "Pacientes.findAll", query = "SELECT p FROM Pacientes p")})
public class Pacientes implements Serializable {
public Pacientes() {
}
@Override
public int hashCode() {
int hash = 0;
hash += (pacidentificacion != null ? pacidentificacion.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Pacientes)) {
return false;
}
Pacientes other = (Pacientes) object;
if ((this.pacidentificacion == null && other.pacidentificacion != null) || (this.pacidentificacion
!= null && !this.pacidentificacion.equals(other.pacidentificacion))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Modelo.Pacientes[ pacidentificacion=" + pacidentificacion + " ]";
}
this.consultarPacienteVista= consultarPacienteVista;
EntityManagerFactory emf= Persistence.createEntityManagerFactory("ProyectoCitasPU");
pacientesModelo= new Modelo.PacientesJpaController(emf);
}
@Override
public void actionPerformed(ActionEvent e)
{
String valor= consultarPacienteVista.ValorTxt.getText();
int parametro=0;
consultarPacienteVista.getTableModel().setRowCount(0);
consultarPacienteVista.getTableModel().fireTableDataChanged();
if(consultarPacienteVista.IdentificacionOpt.isSelected())
parametro=1;
if(consultarPacienteVista.NombresOpt.isSelected())
parametro=2;
if(consultarPacienteVista.ApellidosOpt.isSelected())
parametro=3;
if(consultarPacienteVista.sexoOpt.isSelected())
parametro=4;
List<Modelo.Pacientes> pacientes= pacientesModelo.findPacientesEntities();
for(modelo.Pacientes p:pacientes)
{
switch (parametro)
{
case 1: if (p.getPacidentificacion().equals(valor))
mostrarEnTabla(p);
break;
case 2: if (p.getPacnombres().equals(valor))
mostrarEnTabla(p);
break;
case 3: if (p.getPacapellidos().equals(valor))
mostrarEnTabla(p);
break;
case 4: if (p.getPacsexo().toString().equals(valor))
mostrarEnTabla(p);
break;
}
}
}
private void mostrarEnTabla(modelo.Pacientes p)
{
String registro[] = new String [5];
registro[0]=p.getPacidentificacion();
registro[1]=p.getPacnombres();
registro[2]=p.getPacapellidos();
registro[3]=p.getPacfechaNacimiento().toString();
registro[4]=p.getPacsexo().toString();
consultarPacienteVista.getTableModel().addRow(registro);
consultarPacienteVista.getTableModel().fireTableDataChanged();
}
}
Sesión 5: Probando el Modelo Vista Controlador MVC
FORMULARIO PRINCIPAL
package VISTA;
/**
*
* @author BERNA
*/
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("GESTION DE CITAS");
setName(""); // NOI18N
jTextField1.setText("jTextField1");
jMenuBar1.setToolTipText("");
jMenu1.setText("archivo");
jMenuItem1.setText("SALIR");
jMenuItem1.setToolTipText("");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
jMenu2.setText("pacientes");
jMenuItem2.setText("REGISTRAR");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem2);
jMenuItem3.setText("CONSULTAR");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu2.add(jMenuItem3);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
pack();
}// </editor-fold>
/**
* @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(PrincipalJFrame.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PrincipalJFrame.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PrincipalJFrame.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PrincipalJFrame.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PrincipalJFrame().setVisible(true);
}
});
}
package VISTA;
/**
*
* @author BERNA
*/
import javax.swing.table.DefaultTableModel;
public ConsPacienteInternalFrame()
{
initComponents();
gestorpacientesControl=new controlador.GestorPacienteControl(this);
String titulosTabla[] ={"Identificacion","Nombres","Apellidos","Fecha Nac","Sexo"};
tabla = new DefaultTableModel(null, titulosTabla);
ResultadosTbl.setModel(tabla);
AceptarBtn.addActionListener(gestorpacientesControl);
}
public DefaultTableModel getTableModel()
{
return tabla;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
IdentificationOpt = new javax.swing.JRadioButton();
NombresOpt = new javax.swing.JRadioButton();
ApellidosOpt = new javax.swing.JRadioButton();
ValorTxt = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
AceptarBtn = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
ResultadosTbl = new javax.swing.JTable();
SexoOpt = new javax.swing.JRadioButton();
setClosable(true);
setTitle("Cosulta de pacientes");
setToolTipText("");
IdentificationOpt.setText("Identificacion");
NombresOpt.setText("Nombre");
ApellidosOpt.setText("Apellidos");
ValorTxt.setText("Valor a buscar");
AceptarBtn.setText("Aceptar");
ResultadosTbl.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{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"
}
));
jScrollPane1.setViewportView(ResultadosTbl);
SexoOpt.setText("Sexo");
package VISTA;
public class RegPacienteInternalFrame extends javax.swing.JInternalFrame {
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jTextField4.setText("jTextField4");
jTextField4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField4ActionPerformed(evt);
}
});
setClosable(true);
setIconifiable(true);
setMaximizable(true);
setTitle("Registro de Pacientes");
jLabel1.setText("Identificacion:");
jLabel2.setText("Nombres");
jLabel3.setText("Apellidos");
jLabel4.setText("Fecha de nacimiento");
jLabel5.setText("Sexo");
FemeninoOpt.setText("F");
MasculinoOpt.setText("M");
RegistrarBtn.setText("Registrar");
RegistrarBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RegistrarBtnActionPerformed(evt);
}
});
NuevoBtn.setText("Nuevo");
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
236, Short.MAX_VALUE)
.addComponent(RegistrarBtn)
.addGap(26, 26, 26)
.addComponent(NuevoBtn))
.addGroup(layout.createSequentialGroup()
.addGap(63, 63, 63)
.addComponent(MasculinoOpt)
.addGap(32, 32, 32)
.addComponent(FemeninoOpt)
.addGap(0, 0, Short.MAX_VALUE))))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 130,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(FechaNacimientoDtc,
javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(NombresTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 168,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGap(66, 66, 66)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(ApellidosTxt, javax.swing.GroupLayout.DEFAULT_SIZE,
168, Short.MAX_VALUE)
.addComponent(IdentificacionTxt)))))
.addGap(0, 0, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(IdentificacionTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(NombresTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(ApellidosTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel4)
.addComponent(FechaNacimientoDtc, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(17, 17, 17)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(FemeninoOpt)
.addComponent(MasculinoOpt))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(RegistrarBtn)
.addComponent(NuevoBtn))
.addContainerGap(32, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
CONEXIÓN
package recurso;
import java.sql.*;
import javax.swing.JOptionPane;
public class Conexion {
private String driver,url,ip,bd,usr,pass;
private Connection conexion;
public Conexion(String ip, String bd, String usr, String pass)
{
driver = "oracle.jdbc.driver.OracleDriver";
this.bd = bd;
this.usr = usr;
this.pass = pass;
url = "jdbc:oracle:thin:@" + ip + ":1521:" + bd;
try
{
Class.forName(driver).newInstance();
conexion = DriverManager.getConnection(url, usr, pass);
}
catch (Exception exc)
{
JOptionPane.showMessageDialog(null, "error de conexion xon la base de datos");
}
}
public Connection getConexion()
{
return conexion;
}
public Connection cerrarConexion() throws SQLException
{
conexion.close();
conexion = null;
return conexion;
}
}
Sesión 7: Accediendo a la Base de Datos con JPA
CLASE PACIENTES
package Modelo;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
*
* @author BERNA
*/
@Entity
@Table(name = "PACIENTES")
@NamedQueries({
@NamedQuery(name = "Pacientes.findAll", query = "SELECT p FROM Pacientes p")})
public class Pacientes implements Serializable {
public Pacientes() {
}
@Override
public int hashCode() {
int hash = 0;
hash += (pacidentificacion != null ? pacidentificacion.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Pacientes)) {
return false;
}
Pacientes other = (Pacientes) object;
if ((this.pacidentificacion == null && other.pacidentificacion != null) || (this.pacidentificacion
!= null && !this.pacidentificacion.equals(other.pacidentificacion))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Modelo.Pacientes[ pacidentificacion=" + pacidentificacion + " ]";
}
CLASE PACIENTESJPACONTROLLER:
package Modelo;
import Modelo.exceptions.NonexistentEntityException;
import Modelo.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
/**
*
* @author BERNA
*/
public class PacientesJpaController implements Serializable {
package reportes;
/**
*
* @author BERNA
*/
import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;
public GestorReportes()
{
String driver= "oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@licalhost:1521:XE";
try
{
Class.forName(driver).newInstance();
conexion=DriverManager.getConnection(url, "citas","citas");
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null,"error de conexion con la base de datos");
}
}
public void ejecutarReporte(String archivo)
{
try
{
String Reporte = System.getProperty("user.dir") + "/src/reportes/"+archivo;
JasperReport masterReport =(JasperReport) JRLoader.loadObject(Reporte);
JasperPrint jasperPrint = JasperFillManager.fillReport(masterReport, null, conexion);
JasperViewer jviewer=new JasperViewer(jasperPrint,false);
jviewer.setVisible(true);
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "Error:" + ex.getMessage());
}
}
}
CONTENIDOS