1
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
INTRODUCCIN 02
Sesin 1: Construyendo la base de datos 03
Sesin 2: Desarrollando la Interfaz Grfica de Usuario (GUI) 25
Sesin 3: Creando el Modelo de la Aplicacin 46
Sesin 4: Enlazando con el Controlador 56
Sesin 5: Probando el Modelo Vista Controlador MVC 70
Sesin 6: Accediendo a la Base de Datos con JDBC 78
Sesin 7: Accediendo a la Base de Datos con JPA 89
Sesin 8: Generando reportes impresos 108
2
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
TABLAS
1.1. CITAS
3
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
1.2. CONSULTORIOS
1.3. MEDICOS
4
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
1.4. PACIENTES
1.5. TRATAMIENTO
5
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
6
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
7
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
Clase paciente
/*
ADSI SENA
CARLOS ANDRES MARTINEZ CARO
*/
package modelo;
public Paciente (String id, String nom, String ape, String fechaNac, String sex)
{
identificacion=id;
nombres=nom;
apellidos=ape;
fechaNacimiento=fechaNac;
sexo=sex;
}
/**
* @return the identificacion
*/
public String getIdentificacion() {
return identificacion;
}
/**
* @param identificacion the identificacion to set
*/
public void setIdentificacion(String identificacion) {
this.identificacion = identificacion;
}
/**
* @return the nombres
*/
public String getNombres() {
return nombres;
}
/**
* @param nombres the nombres to set
*/
public void setNombres(String nombres) {
this.nombres = nombres;
8
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
/**
* @return the apellidos
*/
public String getApellidos() {
return apellidos;
}
/**
* @param apellidos the apellidos to set
*/
public void setApellidos(String apellidos) {
this.apellidos = apellidos;
}
/**
* @return the fechaNacimiento
*/
public String getFechaNacimiento() {
return fechaNacimiento;
}
/**
* @param fechaNacimiento the fechaNacimiento to set
*/
public void setFechaNacimiento(String fechaNacimiento) {
this.fechaNacimiento = fechaNacimiento;
}
/**
* @return the sexo
*/
public String getSexo() {
return sexo;
}
/**
* @param sexo the sexo to set
*/
public void setSexo(String sexo) {
this.sexo = sexo;
}
}
9
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
import java.util.LinkedList;
import java.sql.*;
import javax.swing.JOptionPane;
public GestorPaciente()
{
recurso.Conexion conexion= new recurso.Conexion ("localhost","XE", "citas", "citas");
conn = conexion.getConexion();
}
public void registrarPaciente (Paciente paciente)
{
try
{
PreparedStatement pst = conn.prepareStatement("insert into PACIENTES values (?,?,?,?,?)");
pst.setString(1, paciente.getIdentificacion());
pst.setString(2, paciente.getNombres());
pst.setString(3,paciente.getApellidos());
pst.setString(4,paciente.getFechaNacimiento());
pst.setString(5,paciente.getSexo());
pst.executeUpdate();
JOptionPane.showMessageDialog(null,"paciente Registrado");
}
catch(SQLException exc)
{
JOptionPane.showMessageDialog(null, exc.getMessage());
}
}
public LinkedList<Paciente> getPacientesBy (int parametro, String valor)
{
LinkedList<Paciente> resultado = new LinkedList<Paciente>();
String sql="";
switch (parametro)
{
case 1: sql = "select * from PACIENTES where pacIdentificacion ='"+valor+"`";
break;
case 2: sql ="select * from PACIENTES where pacNombres ='"+valor+"`";
break;
case 3: sql ="select * from PACIENTES where pacApellido ='"+valor+"`";
break;
case 4: sql ="select * from PACIENTES where pacSexo ='"+valor+"`";
break;
}
try
{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while(rs.next())
1
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
{
resultado.add(new Paciente (rs.getString("pacIdentificacion"),
rs.getString("pacNombres"),
rs.getString("pacApellidos"),
rs.getString("pacFechaNacimiento"),
rs.getString("pacSexo")));
}
st.close();
rs.close();
}
catch (SQLException exc)
{
JOptionPane.showMessageDialog(null, exc.getMessage());
}
finally
{
return resultado;
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(pacienteVista.RegistrarBtn))
1
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
{
String identificacion=pacienteVista.IdentificacionTxt.getText();
String nombres=pacienteVista.NombresTxt.getText();
String apellidos=pacienteVista.ApellidosTxt.getText();
SimpleDateFormat formato = new SimpleDateFormat ("dd/MM/yyyy");
String fechaNacimiento=formato.format(pacienteVista.FechaNacimientoDtc.getDate());
String sexo=null;
if(pacienteVista.MasculinoOpt.isSelected())
sexo="m";
else
sexo="f";
pacienteModelo=new modelo.Paciente(identificacion,nombres, apellidos,fechaNacimiento, sexo);
gestorPacienteModelo.registrarPaciente (pacienteModelo);
}
if(e.getSource().equals(pacienteVista.NuevoBtn))
{
pacienteVista.IdentificacionTxt.setText(null);
pacienteVista.NombresTxt.setText(null);
pacienteVista.ApellidosTxt.setText(null);
pacienteVista.FechaNacimientoDtc.setDate(null);
pacienteVista.MasculinoOpt.setSelected(false);
pacienteVista.FemeninoOpt.setSelected(false);
pacienteVista.IdentificacionTxt.requestFocus();
}
}
}
/*
SENA ADSI 2015
CARLOS ANDRES MARTINEZ CARO
*/
package controlador;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
1
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
{
this.consultarPacienteVista=consultarPacienteVista;
pacientesModelo=new modelo.GestorPaciente();
}
@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;
LinkedList<modelo.Paciente> pacientes = pacientesModelo.getPacientesBy (parametro, valor);
String registro[] = new String [5];
for(modelo.Paciente p:pacientes)
{
registro[0]=p.getIdentificacion();
registro[1]=p.getNombres();
registro[2]=p.getApellidos();
registro[3]=p.getFechaNacimiento();
registro[4]=p.getSexo();
consultarPacienteVista.getTableModel().addRow (registro);
consultarPacienteVista.getTableModel().fireTableDataChanged();
}
}
}
Formulario principal
/*
SENA ADSI 2015
CARLOS ANDRES MARTINEZ CARO
*/
package vista;
1
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
RegPacienteInternalFrame regPacienteInternalFrame;
ConsPacienteInternalFrame consPacienteInternalFrame;
public PrincipalJFrame()
{
regPacienteInternalFrame = new RegPacienteInternalFrame();
consPacienteInternalFrame = new ConsPacienteInternalFrame();
add (regPacienteInternalFrame);
add (consPacienteInternalFrame);
initComponents();
setExtendedState (MAXIMIZED_BOTH);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("GESTIN DE CITAS");
setName("PrincipalJFrame"); // NOI18N
jMenu1.setText("Archivo");
jMenu1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu1ActionPerformed(evt);
}
});
jMenuItem1.setText("Salir");
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");
jMenu2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenu2ActionPerformed(evt);
}
});
jMenuItem2.setText("Registrar");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
1
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
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>
1
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
/**
* @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.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PrincipalJFrame.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PrincipalJFrame.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PrincipalJFrame.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>
1
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
ConsPacienteInternalFrame
/*
SENA ADSI 2015
CARLOS ANDRES MARTINEZ CARO
*/
package vista;
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() {
IdentificacionOpt.setText("Identificacin");
IdentificacionOpt.setName("IdentificacionOpt"); // NOI18N
IdentificacionOpt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
IdentificacionOptActionPerformed(evt);
}
1
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
});
NombresOpt.setText("Nombres");
NombresOpt.setName("NombresOpt"); // NOI18N
ApellidosOpt.setText("Apellidos");
ApellidosOpt.setName("ApellidosOpt"); // NOI18N
SexoOpt.setText("Sexo");
SexoOpt.setName("SexoOpt"); // NOI18N
jLabel1.setText("Valor a Buscar");
ValorTxt.setName("ValorTxt"); // NOI18N
AceptarBtn.setText("Aceptar");
AceptarBtn.setName("AceptarBtn"); // NOI18N
ResultadosTbl.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"
}
));
ResultadosTbl.setName("ResultadosTbl"); // NOI18N
jScrollPane1.setViewportView(ResultadosTbl);
1
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
pack();
}// </editor-fold>
1
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
RegPacienteInternalFrame
/*
SENA ADSI 2015
CARLOS ANDRES MARTINEZ CARO
*/
package vista;
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jCheckBoxMenuItem1.setSelected(true);
jCheckBoxMenuItem1.setText("jCheckBoxMenuItem1");
setTitle("Registro de Pacientes");
IdentificacionTxt.setName("IdentificacionTxt"); // NOI18N
IdentificacionTxt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
IdentificacionTxtActionPerformed(evt);
}
});
2
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
jLabel1.setText("Identificacin:");
jLabel2.setText("Nombre: ");
jLabel3.setText("Apellidos");
jLabel4.setText("Fecha de Nacimiento");
jLabel5.setText("Sexo");
NombresTxt.setName("NombresTxt"); // NOI18N
ApellidosTxt.setName("ApellidosTxt"); // NOI18N
ApellidosTxt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ApellidosTxtActionPerformed(evt);
}
});
MasculinoOpt.setText("M");
MasculinoOpt.setName("MasculinoOpt"); // NOI18N
MasculinoOpt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
MasculinoOptActionPerformed(evt);
}
});
FemeninoOpt.setText("F");
FemeninoOpt.setName("FemeninoOpt"); // NOI18N
RegistrarBtn.setText("Registrar");
RegistrarBtn.setName("RegistrarBtn"); // NOI18N
RegistrarBtn.setOpaque(false);
RegistrarBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
RegistrarBtnActionPerformed(evt);
}
});
NuevoBtn.setText("Nuevo");
NuevoBtn.setName("NuevoBtn"); // NOI18N
NuevoBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NuevoBtnActionPerformed(evt);
}
});
FechaNacimientoDtc.setName("FechaNacimientoDtc"); // NOI18N
FechaNacimientoDtc.setOpaque(false);
2
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(43, 43, 43)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85,
Short.MAX_VALUE)
.addComponent(IdentificacionTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 159,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(58, 58, 58))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel4)
.addComponent(jLabel3))
.addGap(54, 54, 54)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(ApellidosTxt)
.addComponent(NombresTxt)
.addComponent(FechaNacimientoDtc, javax.swing.GroupLayout.DEFAULT_SIZE, 159,
Short.MAX_VALUE))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addGap(80, 80, 80)
.addComponent(MasculinoOpt)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(FemeninoOpt)
.addGroup(layout.createSequentialGroup()
.addComponent(RegistrarBtn)
.addGap(18, 18, 18)
.addComponent(NuevoBtn)))
.addGap(47, 47, 47))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(IdentificacionTxt, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.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(18, 18, 18)
2
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
.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(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(FechaNacimientoDtc, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(MasculinoOpt)
.addComponent(FemeninoOpt))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34,
Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(RegistrarBtn)
.addComponent(NuevoBtn))
.addGap(22, 22, 22))
);
pack();
}// </editor-fold>
2
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
CONEXION
/*
SENA ADSI 2015
CARLOS ANDRES MARTINEZ CARO
*/
package recurso;
import java.sql.*;
import javax.swing.JOptionPane;
}
public Connection getConexion()
{
return conexion;
2
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
}
public Connection CerrarConexion() throws SQLException
{
conexion.close();
conexion = null;
return conexion;
}
}
PACIENTES
/*
SENA ADSI
CARLOS ANDRES MARTINEZ CARO
*/
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 bibli
*/
@Entity
@Table(name = "PACIENTES")
@NamedQueries({
@NamedQuery(name = "Pacientes.findAll", query = "SELECT p FROM Pacientes p")})
public class Pacientes implements Serializable {
2
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
@Column(name = "PACFECHANACIMIENTO")
@Temporal(TemporalType.TIMESTAMP)
private Date pacfechanacimiento;
@Column(name = "PACSEXO")
private Character pacsexo;
public Pacientes() {
}
2
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
return pacsexo;
}
@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 + " ]";
}
PACIENTESJPACONTROLER
/*
SENA ADSI
CARLOS ANDRES MARTINEZ CARO
*/
package modelo;
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;
2
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
import javax.persistence.criteria.Root;
import modelo.exceptions.NonexistentEntityException;
import modelo.exceptions.PreexistingEntityException;
/**
*
* @author bibli
*/
public class PacientesJpaController implements Serializable {
2
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
2
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
PACIENTECONTROL
/*
SENA ADSI 2015
CARLOS ANDRES MARTINEZ CARO
*/
package controlador;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;
import modelo.exceptions.PreexistingEntityException;
3
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(pacienteVista.RegistrarBtn))
{
String identificacion=pacienteVista.IdentificacionTxt.getText();
String nombres=pacienteVista.NombresTxt.getText();
String apellidos=pacienteVista.ApellidosTxt.getText();
SimpleDateFormat formato = new SimpleDateFormat ("dd/MM/yyyy");
String fechaNacimiento=formato.format(pacienteVista.FechaNacimientoDtc.getDate());
char sexo='\0';
if(pacienteVista.MasculinoOpt.isSelected())
sexo='m';
else
sexo='f';
pacienteModelo=new modelo.Pacientes();
pacienteModelo.setPacidentificacion(identificacion);
pacienteModelo.setPacapellidos(apellidos);
pacienteModelo.setPacnombres(nombres);
pacienteModelo.setPacfechanacimiento(new Date (fechaNacimiento));
pacienteModelo.setPacsexo(sexo);
try
{
gestorPacienteModelo.create(pacienteModelo);
JOptionPane.showMessageDialog(pacienteVista, "Paciente registrado correctamente");
}
catch (PreexistingEntityException ex)
{
JOptionPane.showMessageDialog(pacienteVista,"El paciente ya existe");
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(pacienteVista, ex.getMessage());
}
}
if(e.getSource().equals(pacienteVista.NuevoBtn))
{
pacienteVista.IdentificacionTxt.setText(null);
pacienteVista.NombresTxt.setText(null);
pacienteVista.ApellidosTxt.setText(null);
pacienteVista.FechaNacimientoDtc.setDate(null);
pacienteVista.MasculinoOpt.setSelected(false);
pacienteVista.FemeninoOpt.setSelected(false);
pacienteVista.IdentificacionTxt.requestFocus();
3
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
}
}
GestorPacienteControl
/*
SENA ADSI 2015
CARLOS ANDRES MARTINEZ CARO
*/
package controlador;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
}
@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;
3
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
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();
}
}
INFORME
/*
SENA ADSI
CARLOS ANDRES MARTINEZ CARO
*/
package reportes;
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:@localhost:1521:XE";
try
3
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje
Tutorial Crear Aplicacin JAVA
{
Class.forName(driver).newInstance();
conexion=DriverManager.getConnection(url,"citas","citas");
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null,"Error dseconexion database");
}
}
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());
}
}
}
3
FAVA- Formacin en Ambientes Virtuales de Aprendizaje SENA - Servicio Nacional de Aprendizaje