Agenda



El programa puede actualizar, guardar, listar y eliminar contactos. Ademas permite una busqueda de un contacto por su DNI y ordenar la tabla por un criterio a eleccion.

BDManagement.java


package BackEnd;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class BDManagement {

 private String user = "root";
 private String pass = "riverplate";
 private String db = "jdbc:mysql://localhost:3306/contactos";
 private String driver = "com.mysql.jdbc.Driver";
 private Connection con;
 private Statement st;
 
 public BDManagement(){
  try{
   Class.forName(driver);
   con = DriverManager.getConnection(db,user,pass);
   st = con.createStatement();
  }catch (Exception ex){
   ex.printStackTrace();
  }
 }
 
 public ResultSet listar(){
  String query = "select * from agenda";
  ResultSet rs = null;
  try {
   rs = st.executeQuery(query);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return rs;
 }
 
 public ResultSet ordenar(String criterio,String ascDesc){
  String query = "select * from agenda order by "+criterio+" "+ascDesc;
  ResultSet rs = null;
  try {
   rs = st.executeQuery(query);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return rs;
 }
 
 public ResultSet buscar(int dni){
  String query = "select * from agenda where dni="+dni;
  ResultSet rs = null;
  try {
   rs = st.executeQuery(query);
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return rs;
 }
 
 public void agregar(String nom,String ape,int dni,String tel,String cel,String mail,String dir){
  String query = "insert into agenda(nom,ape,dni,tel,cel,mail,dir) values";
  query+="('"+nom+"','"+ape+"','"+dni+"','"+tel+"','"+cel+"','"+mail+"','"+dir+"')";
  try {
   st.executeUpdate(query);
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 
 public void modificar(String nom,String ape,int dni,String tel,String cel,String mail,String dir){
  String query="update agenda set nom='"+nom+"',ape='"+ape+"',"+"tel='"+tel;
  query+="',cel='"+cel+"',mail='"+mail+"',dir='"+dir+"' where dni="+dni;
  try {
   st.executeUpdate(query);
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 
 public void borrar(int dni){
  String query="delete from agenda where dni= "+dni;
  try {
   st.executeUpdate(query);
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 
}

Contacto.java


package BackEnd;

public class Contacto {

 private String nom,ape,mail,dir,tel,cel;
 private int dni;
 
 public Contacto(String nom,String ape,int dni,String tel,String cel,String mail,String dir){
  this.nom = nom;
  this.ape = ape;
  this.dni = dni;
  this.tel = tel;
  this.cel = cel;
  this.mail = mail;
  this.dir = dir;
 }
 
 public Contacto(){
  
 }
 
 public String getNom() {
  return nom;
 }
 public void setNom(String nom) {
  this.nom = nom;
 }
 public String getApe() {
  return ape;
 }
 public void setApe(String ape) {
  this.ape = ape;
 }
 public String getMail() {
  return mail;
 }
 public void setMail(String mail) {
  this.mail = mail;
 }
 public String getDir() {
  return dir;
 }
 public void setDir(String dir) {
  this.dir = dir;
 }
 public int getDni() {
  return dni;
 }
 public void setDni(int dni) {
  this.dni = dni;
 }
 public String getTel() {
  return tel;
 }
 public void setTel(String tel) {
  this.tel = tel;
 }
 public String getCel() {
  return cel;
 }
 public void setCel(String cel) {
  this.cel = cel;
 }
 
}

VentanaPrincipal.java


package FrontEnd;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import BackEnd.BDManagement;
import BackEnd.Contacto;

public class VentanaPrincipal extends JFrame implements ActionListener{

 private BDManagement bdm;
 private JPanel panelSup,panelInf;
 private JButton listar,agregar,modificar,eliminar,buscar,ordenar;
 private JComboBox opcionesOrdenar,opcionesAscDesc; 
 private String titulos[] = {"Nombre","Apellido","D.N.I","Telefono","Celular","E-mail","Direccion"};;
 private JTable tabla;
 private DefaultTableModel dtm;
 private JScrollPane scroll;
 private JLabel labelBuscarDNI,labelOrdenar;
 private JTextField campoDni;
 private ArrayList<Integer> listaDni;
 
 public VentanaPrincipal(){
  super("Gestor de base de datos");
  Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  this.setBounds(0, 0, d.width, 800);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  listaDni = new ArrayList<Integer>();
  
  bdm = new BDManagement();
  panelSup = new JPanel(new FlowLayout());
  labelBuscarDNI = new JLabel("D.N.I. a buscar: ");
  labelBuscarDNI.setFont(new Font("Monospaced", Font.BOLD, 16));
  panelSup.add(labelBuscarDNI);
  campoDni = new JTextField(15);
  panelSup.add(campoDni);
  buscar = new JButton("Buscar");
  panelSup.add(buscar);
  labelOrdenar = new JLabel("Ordenar segun criterio: ");
  labelOrdenar.setFont(new Font("Monospaced", Font.BOLD, 16));
  panelSup.add(labelOrdenar);
  opcionesOrdenar = new JComboBox();
  opcionesOrdenar.addItem("nom");
  opcionesOrdenar.addItem("ape");
  opcionesOrdenar.addItem("dni");
  panelSup.add(opcionesOrdenar);
  opcionesAscDesc = new JComboBox();
  opcionesAscDesc.addItem("asc");
  opcionesAscDesc.addItem("desc");
  panelSup.add(opcionesAscDesc);
  ordenar = new JButton("ordenar");
  panelSup.add(ordenar);
  this.add(panelSup,BorderLayout.NORTH);
  
  //Defino la tabla
  dtm = new DefaultTableModel(null,titulos);
  tabla = new JTable(dtm);
  scroll = new JScrollPane(tabla);
  this.add(scroll,BorderLayout.CENTER);
  
  panelInf = new JPanel(new FlowLayout());
  listar = new JButton("LISTAR");
  agregar = new JButton("AGREGAR");
  modificar = new JButton("MODIFICAR");
  eliminar = new JButton("ELIMINAR");
  panelInf.add(listar);
  panelInf.add(agregar);
  panelInf.add(modificar);
  panelInf.add(eliminar);
  this.add(panelInf,BorderLayout.SOUTH);
  listar.addActionListener(this);
  agregar.addActionListener(this);
  modificar.addActionListener(this);
  eliminar.addActionListener(this);
  buscar.addActionListener(this);
  ordenar.addActionListener(this);
  this.setVisible(true);
 }


 public void actionPerformed(ActionEvent ae) {
  if(ae.getSource()==listar){
   llenarTabla();
  }else if(ae.getSource()==agregar){
   agregar();
  }else if(ae.getSource()==modificar){
   actualizar();
  }else if(ae.getSource()==eliminar){
   borrarContacto();
  }else if(ae.getSource()==buscar){
   buscarDNI();
  }else if(ae.getSource()==ordenar){
   ordenarContactos();
  }
 }
 
 
 public void llenarTabla(){
  dtm.setRowCount(0);
  dtm.setColumnCount(0);
  dtm.setColumnIdentifiers(titulos);
  centrarTexto();
  listaDni.clear();
  ResultSet rs = bdm.listar();
  try {
   while(rs.next()){
    Object[] fila = {rs.getObject(1),rs.getObject(2),rs.getObject(3),rs.getObject(4),rs.getObject(5),rs.getObject(6),rs.getObject(7)};
    dtm.addRow(fila);
    listaDni.add((Integer) rs.getObject(3));
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 
 public void borrarContacto(){
  int [] filasSel = tabla.getSelectedRows();
  for(int i=0;i<filasSel.length;i++){
   int dniSel = listaDni.get(filasSel[i]);
   bdm.borrar(dniSel);
  }
  llenarTabla();
 }
 
 public void agregar(){
  Contacto c = new Contacto();
  VentanaAgregar va = new VentanaAgregar(c);
  if(!c.equals(null)){
   bdm.agregar(c.getNom(), c.getApe(), c.getDni(), c.getTel(), c.getCel(), c.getMail(), c.getDir());
  }
  llenarTabla();
 }
 
 public void actualizar(){
  int fila = tabla.getSelectedRow();
  int dni = (Integer) tabla.getValueAt(fila, 2);
  String tel = (String) tabla.getValueAt(fila, 3);
  String cel = (String) tabla.getValueAt(fila, 4);
  String nom = (String) tabla.getValueAt(fila, 0);
  String ape = (String) tabla.getValueAt(fila, 1);
  String mail = (String) tabla.getValueAt(fila, 5);
  String dir = (String) tabla.getValueAt(fila, 6);
  Contacto c = new Contacto(nom,ape,dni,tel,cel,mail,dir);
  VentanaActualizar vac = new VentanaActualizar(c);
  bdm.modificar(c.getNom(), c.getApe(), c.getDni(), c.getTel(), c.getCel(), c.getMail(), c.getDir());
  llenarTabla();
 }
 
 public void buscarDNI(){
  dtm.setRowCount(0);
  dtm.setColumnCount(0);
  dtm.setColumnIdentifiers(titulos);
  centrarTexto();
  listaDni.clear();
  ResultSet rs = bdm.buscar(Integer.parseInt(campoDni.getText()));
  try {
   while(rs.next()){
    Object[] fila = {rs.getObject(1),rs.getObject(2),rs.getObject(3),rs.getObject(4),rs.getObject(5),rs.getObject(6),rs.getObject(7)};
    dtm.addRow(fila);
    listaDni.add((Integer) rs.getObject(3));
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 
 public void ordenarContactos(){
  dtm.setRowCount(0);
  dtm.setColumnCount(0);
  dtm.setColumnIdentifiers(titulos);
  centrarTexto();
  listaDni.clear();
  ResultSet rs = bdm.ordenar((String) opcionesOrdenar.getSelectedItem(),(String) opcionesAscDesc.getSelectedItem());
  try {
   while(rs.next()){
    Object[] fila = {rs.getObject(1),rs.getObject(2),rs.getObject(3),rs.getObject(4),rs.getObject(5),rs.getObject(6),rs.getObject(7)};
    dtm.addRow(fila);
    listaDni.add((Integer) rs.getObject(3));
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
 
 public void centrarTexto(){
  DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
  tcr.setHorizontalAlignment(SwingConstants.CENTER);
  for(int i=0;i<7;i++){
  tabla.getColumnModel().getColumn(i).setCellRenderer(tcr);
  tabla.getColumnModel().getColumn(i).setResizable(true);
  }
 }
}

VentanaAgregar.java


package FrontEnd;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import BackEnd.Contacto;

public class VentanaAgregar extends JDialog implements ActionListener{

 Contacto c;
 JLabel labelDni, labelNom,labelApe,labelTel,labelCel,labelMail,labelDir;
 JTextField textDni,textNom,textApe,textTel,textCel,textMail,textDir;
 JButton btnAceptar,btnCancelar;
 
 public VentanaAgregar(Contacto c){
  this.c = c;
  this.setModal(true);
  this.setBounds(10, 10, 600, 400);
  this.setLayout(new GridLayout(9,2,10,10));
  this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  labelDni = new JLabel("D.N.I.: ");
  labelNom = new JLabel("Nombre: ");
  labelApe = new JLabel("Apellido: ");
  labelTel = new JLabel("Telefono: ");
  labelCel = new JLabel("Celular: ");
  labelMail = new JLabel("E-mail: ");
  labelDir = new JLabel("Direccion: ");
  textDni = new JTextField(30);
  textNom = new JTextField(30);
  textApe = new JTextField(30);
  textTel = new JTextField(30);
  textCel = new JTextField(30);
  textMail = new JTextField(30);
  textDir = new JTextField(30);
  this.add(labelDni);
  this.add(textDni);
  this.add(labelNom);
  this.add(textNom);
  this.add(labelApe);
  this.add(textApe);
  this.add(labelTel);
  this.add(textTel);
  this.add(labelCel);
  this.add(textCel);
  this.add(labelMail);
  this.add(textMail);
  this.add(labelDir);
  this.add(textDir);
  btnAceptar = new JButton("OK");
  btnCancelar = new JButton("Cancelar");
  this.add(btnCancelar);
  this.add(btnAceptar);
  btnAceptar.addActionListener(this);
  btnCancelar.addActionListener(this);
  this.setVisible(true);
 }


 public void actionPerformed(ActionEvent e) {
  if(e.getSource()==btnAceptar){
   GuardarDatos();
   this.dispose();
  }else if(e.getSource()==btnCancelar){
   this.c=null;
   this.dispose();
  }
 }
 
 public void GuardarDatos(){
  if(!textDni.getText().isEmpty())c.setDni(Integer.parseInt(textDni.getText()));
  if(!textTel.getText().isEmpty())c.setTel(textTel.getText());
  if(!textCel.getText().isEmpty())c.setCel(textCel.getText());
  if(!textNom.getText().isEmpty())c.setNom(textNom.getText());
  if(!textApe.getText().isEmpty())c.setApe(textApe.getText());
  if(!textMail.getText().isEmpty())c.setMail(textMail.getText());
  if(!textDir.getText().isEmpty())c.setDir(textDir.getText());
 }
 
}

VentanaActualizar.java


package FrontEnd;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import BackEnd.Contacto;

public class VentanaActualizar extends JDialog implements ActionListener{

 Contacto c;
 JLabel labelDni, labelNom,labelApe,labelTel,labelCel,labelMail,labelDir;
 JTextField textDni,textNom,textApe,textTel,textCel,textMail,textDir;
 JButton btnAceptar,btnCancelar;
 
 public VentanaActualizar(Contacto c){
  this.c = c;
  this.setModal(true);
  this.setBounds(10, 10, 600, 400);
  this.setLayout(new GridLayout(9,2,10,10));
  this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  labelDni = new JLabel("D.N.I.: ");
  labelNom = new JLabel("Nombre: ");
  labelApe = new JLabel("Apellido: ");
  labelTel = new JLabel("Telefono: ");
  labelCel = new JLabel("Celular: ");
  labelMail = new JLabel("E-mail: ");
  labelDir = new JLabel("Direccion: ");
  textDni = new JTextField(30);
  textDni.setEditable(false);
  textNom = new JTextField(30);
  textApe = new JTextField(30);
  textTel = new JTextField(30);
  textCel = new JTextField(30);
  textMail = new JTextField(30);
  textDir = new JTextField(30);
  this.add(labelDni);
  this.add(textDni);
  this.add(labelNom);
  this.add(textNom);
  this.add(labelApe);
  this.add(textApe);
  this.add(labelTel);
  this.add(textTel);
  this.add(labelCel);
  this.add(textCel);
  this.add(labelMail);
  this.add(textMail);
  this.add(labelDir);
  this.add(textDir);
  btnAceptar = new JButton("OK");
  btnCancelar = new JButton("Cancelar");
  this.add(btnCancelar);
  this.add(btnAceptar);
  cargarDatos();
  btnAceptar.addActionListener(this);
  btnCancelar.addActionListener(this);
  this.setVisible(true);
 }


 public void actionPerformed(ActionEvent e) {
  if(e.getSource()==btnAceptar){
   GuardarDatos();
   this.dispose();
  }else if(e.getSource()==btnCancelar){
   this.c=null;
   this.dispose();
  }
 }
 
 public void cargarDatos(){
  textDni.setText(""+c.getDni());
  textDir.setText(""+c.getDir());
  textApe.setText(""+c.getApe());
  textNom.setText(""+c.getNom());
  textCel.setText(""+c.getCel());
  textTel.setText(""+c.getTel());
  textMail.setText(""+c.getMail());
 }
 
 public void GuardarDatos(){
  if(!textDni.getText().isEmpty())c.setDni(Integer.parseInt(textDni.getText()));
  if(!textTel.getText().isEmpty())c.setTel(textTel.getText());
  if(!textCel.getText().isEmpty())c.setCel(textCel.getText());
  if(!textNom.getText().isEmpty())c.setNom(textNom.getText());
  if(!textApe.getText().isEmpty())c.setApe(textApe.getText());
  if(!textMail.getText().isEmpty())c.setMail(textMail.getText());
  if(!textDir.getText().isEmpty())c.setDir(textDir.getText());
 }
 
}

Main.java


package FrontEnd;

public class Main {

 public static void main(String[] args) {
  VentanaPrincipal v = new VentanaPrincipal();
 }

}

Diagrama generado con ObjectAid


El programa en ejecucion


el programa agenda.jar

https://drive.google.com/file/d/0B0sBXdZ8aJgdTWlBa3BpVzFXc0U/edit?usp=sharing



Para ejecutar en el directorio del archivo tipear:

java -jar agenda.jar

Comentarios

  1. Casino in Las Vegas: Guide & Info on the Best Casinos in
    Find a Casino in septcasino Las Vegas and play games like kadangpintar blackjack, roulette, craps and more! We've septcasino.com got 출장샵 the complete gaming experience, https://sol.edu.kg/ exclusive restaurants,

    ResponderBorrar

Publicar un comentario