Conexion.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/*
* 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.
*/
/**
*
* @author Uer
*/
public class Conexion {
private Connection con;
private ResultSet rs;
private PreparedStatement st;
JFrame form;
public Conexion(JFrame form){
this.form=form;
conectar();
}
private void conectar() {
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
con=DriverManager.getConnection("jdbc:ucanaccess://C:/Clinica.accdb");
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(form,"No se pudo cargar el driver :\n"+e.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
}catch (SQLException sqle){
JOptionPane.showMessageDialog(form,"No se pudo Establecer la conexion :\n"+sqle.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
}
}
public boolean eliminar(int cod){
try {
st=con.prepareStatement("DELETE FROM pacientes WHERE COD=?");
st.setInt(1, cod);
return st.executeUpdate()==1;
} catch (SQLException ex) {
JOptionPane.showMessageDialog(form, "Error Al eliminar registro : "+ex.getMessage(), "Error"
, JOptionPane.ERROR_MESSAGE);
}return false;
}
public boolean Insertar(PacienteVo pv){
try {
st=con.prepareStatement("INSERT INTO pacientes(COD,APELLIDOS,NOMBRE,CIUDAD,DIAGNOSTICO) VALUES(?,?,?,?,?)");
st.setInt(1, pv.getCod());
st.setString(2,pv.getApellidos());
st.setString(3, pv.getNombre());
st.setString(4, pv.getCiudad());
st.setString(5, pv.getDiagnostico());
st.executeUpdate();
st.close();
return true;
} catch (SQLException ex) {
JOptionPane.showMessageDialog(form,"No se pudo insertar el registro : \n"+ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
return false; }
}
public PacienteVo buscar(int cod){
try {
st=con.prepareStatement("SELECT APELLIDOS,NOMBRE,CIUDAD,DIAGNOSTICO FROM pacientes WHERE COD='"+cod+"'");
rs=st.executeQuery();
if(rs.next()){
PacienteVo p=new PacienteVo();
p.setApellidos(rs.getString("APELLIDOS"));
p.setNombre(rs.getString("NOMBRE"));
p.setCiudad(rs.getString("CIUDAD"));
p.setDiagnostico(rs.getString("DIAGNOSTICO"));
p.setCod(cod);
st.close();
rs.close();
return p;
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(form, "Error al hacer la consulta :\n"+e.getMessage(), "Error", cod);
} return null;
}
public boolean actualizar(PacienteVo p){
try {
st=con.prepareStatement("UPDATE pacientes SET NOMBRE=?,APELLIDOS=?,CIUDAD=?,DIAGNOSTICO=? WHERE COD='"+p.getCod()+"'");
st.setString(1, p.getNombre());
st.setString(2, p.getApellidos());
st.setString(3, p.getCiudad());
st.setString(4, p.getDiagnostico());
return (st.executeUpdate()==1);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(form, "No se pudo actualizar el registro : \n "+ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}return false;
}
@Override
public void finalize(){
try {
st.close();
rs.close();
con.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(form, "No se pudo actualizar el registro : \n "+ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
PacienteVo.java
/*
* 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.
*/
/**
*
* @author Uer
*/
public class PacienteVo {
private int cod;private String apellidos;
private String nombre;
private String ciudad;
private String diagnostico;
/**
* @return the cod
*/
public int getCod() {
return cod;
}
/**
* @param cod the cod to set
*/
public void setCod(int cod) {
this.cod = cod;
}
/**
* @return the apellidos
*/
public String getApellidos() {
return apellidos;
}
/**
* @param apellidos the apellidos to set
*/
public void setApellidos(String apellidos) {
this.apellidos = apellidos;
}
/**
* @return the nombre
*/
public String getNombre() {
return nombre;
}
/**
* @param nombre the nombre to set
*/
public void setNombre(String nombre) {
this.nombre = nombre;
}
/**
* @return the ciudad
*/
public String getCiudad() {
return ciudad;
}
/**
* @param ciudad the ciudad to set
*/
public void setCiudad(String ciudad) {
this.ciudad = ciudad;
}
/**
* @return the diagnostico
*/
public String getDiagnostico() {
return diagnostico;
}
/**
* @param diagnostico the diagnostico to set
*/
public void setDiagnostico(String diagnostico) {
this.diagnostico = diagnostico;
}
/**
* @return the cod
*/
}
Formulario.java
import javax.swing.JOptionPane;
/*
* 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.
*/
/**
*
* @author uer
*/
public class Formulario extends javax.swing.JFrame {
/**
* Creates new form Formulario
*/
Conexion con;
public Formulario() {
initComponents();
con=new Conexion(this);
}
private boolean validarCodigo(String cod){
try {
Integer.parseInt(cod);
return true;
} catch (Exception e) {
}
return false;
}
/**
* 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")
//
//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
cCod = new javax.swing.JTextField();
cApe = new javax.swing.JTextField();
cNom = new javax.swing.JTextField();
cCiu = new javax.swing.JTextField();
cDia = new javax.swing.JTextField();
bInAc = new javax.swing.JButton();
bEli = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setFont(new java.awt.Font("Arial Black", 0, 24)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Clinica");
jLabel2.setText("Codigo:");
jLabel3.setText("Apellidos:");
jLabel4.setText("Nombres:");
jLabel5.setText("Ciudad:");
jLabel6.setText("Diagnostico:");
cCod.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
cCodFocusLost(evt);
}
});
bInAc.setText("Ingresar");
bInAc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bInAcActionPerformed(evt);
}
});
bEli.setText("Eliminar");
bEli.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bEliActionPerformed(evt);
}
});
jButton3.setText("Limpiar");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(69, 69, 69)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bInAc, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5)
.addComponent(jLabel6))
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(cCod)
.addComponent(cApe)
.addComponent(cNom)
.addComponent(cCiu)
.addComponent(cDia, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))))))
.addGap(0, 50, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(bEli, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(14, 14, 14)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(cCod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(cApe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(cNom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(22, 22, 22)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(cCiu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(22, 22, 22)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(cDia, 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.TRAILING)
.addComponent(bInAc, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
.addComponent(bEli, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(23, 23, 23))
);
pack();
}// //GEN-END:initComponents
private void bEliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEliActionPerformed
// TODO add your handling code here:
if(con.eliminar(Integer.parseInt(cCod.getText()))){
limpiarCampos();
JOptionPane.showMessageDialog(this, "Eliminacion de registro exitoso!", "Eliminacion", JOptionPane.INFORMATION_MESSAGE);
}
else JOptionPane.showMessageDialog(this, "Error:\n Regristro no encontrado","Eliminacion",JOptionPane.ERROR_MESSAGE);
}//GEN-LAST:event_bEliActionPerformed
private void bInAcActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bInAcActionPerformed
// TODO add your handling code here:
if(validarCodigo(cCod.getText())){
PacienteVo p=new PacienteVo();
p.setApellidos(cApe.getText());
p.setCiudad(cCiu.getText());
p.setCod(Integer.parseInt(cCod.getText()));
p.setDiagnostico(cDia.getText());
p.setNombre(cNom.getText());
if(!bInAc.getText().equals("Actualizar")){
if(con.Insertar(p)){
JOptionPane.showMessageDialog(this, "Registro Exitoso!", "Registro", JOptionPane.INFORMATION_MESSAGE);
} }else
if(con.actualizar(p))
JOptionPane.showMessageDialog(this, "Actualizacion Exitosa!", "Actualizacion", JOptionPane.INFORMATION_MESSAGE);
}limpiarCampos();
}//GEN-LAST:event_bInAcActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
limpiarCampos();
}//GEN-LAST:event_jButton3ActionPerformed
private void cCodFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cCodFocusLost
// TODO add your handling code here:
PacienteVo p;
if(validarCodigo(cCod.getText()))
if((p=con.buscar(Integer.parseInt(cCod.getText())))!=null){
cApe.setText(p.getApellidos());
cNom.setText(p.getNombre());
cCiu.setText(p.getCiudad());
cDia.setText(p.getDiagnostico());
bInAc.setText("Actualizar");
}
else bInAc.setText("Ingresar");
}//GEN-LAST:event_cCodFocusLost
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* 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(Formulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Formulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Formulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Formulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
// /* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Formulario().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton bEli;
private javax.swing.JButton bInAc;
private javax.swing.JTextField cApe;
private javax.swing.JTextField cCiu;
private javax.swing.JTextField cCod;
private javax.swing.JTextField cDia;
private javax.swing.JTextField cNom;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
// End of variables declaration//GEN-END:variables
private void limpiarCampos() {
cApe.setText("");
cCiu.setText("");
cCod.setText("");
cDia.setText("");
cNom.setText("");
}
}