lunes, 30 de septiembre de 2013

Mantenimiento de Articulo de deportivo



Mantenimiento de Articulo de deportivo
En esta primera ejemplo mostrar JTable para mostrar registros para poder manipular información


1. Ambiente de desarrollo
- JDK v1.6.0.27
- Netbeans v7.1.2
- MySql v5.1.61

2. Diseño de Formulario


3. Codigo de programación

public class Agendas extends javax.swing.JFrame {

    DefaultTableModel model;
    Connection conn;
    Statement sent;

    public Agendas() {

        initComponents();
        Desabilitar();
        Llenar();
        conn = Mysql.getConnection();
    }

    void Desabilitar() {
        txtCodigo.setEditable(false);
        txtArticulo.setEditable(false);
        txtPrecio.setEditable(false);
        txtMarca.setEditable(false);
    }
void limpiar(){
    txtCodigo.setText("");
    txtArticulo.setText("");
    txtPrecio.setText("");
    txtMarca.setText("");
     txtCodigo.requestFocus();
   
   
}
    void Habiliar() {
        txtCodigo.setEditable(true);
        txtArticulo.setEditable(true);
        txtPrecio.setEditable(true);
        txtMarca.setEditable(true);
        txtCodigo.requestFocus();
    }

    void Llenar() {
        try {
             conn = Mysql.getConnection();
            String[] titulos = {"CODIGO", "ARTICULO", "MARCA", "PRECIO"};
            String sql = "select * from contactos";
           model = new DefaultTableModel(null,titulos);
            sent = conn.createStatement();
            ResultSet rs = sent.executeQuery(sql);
            String[] fila = new String[5];          
            while (rs.next()) {
           fila[0] = rs.getString(1);
                fila[0] = rs.getString("CODIGO");
                fila[1] = rs.getString("ARTICULO");
                fila[2] = rs.getString("MARCA");
                fila[3] = rs.getString("PRECIO");
                model.addRow(fila);
            }
            jTable2.setModel(model);
        } catch (Exception ex) {
           ex.printStackTrace();
        }
    }

// guardar
    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        try {
            String sql = "Insert into contactos (CODIGO, ARTICULO, MARCA,PRECIO)"
                    + "values (?,?,?,?)";
            PreparedStatement ps = conn.prepareCall(sql);
            ps.setString(1, txtCodigo.getText());
            ps.setString(2, txtArticulo.getText());
            ps.setString(3, txtMarca.getText());
            ps.setString(4, txtPrecio.getText());
            int n = ps.executeUpdate();
            if (n > 0) {
                 Llenar();
                JOptionPane.showMessageDialog(null, "Datos Guardados Correctamente");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error" + e.getMessage());
        }
               limpiar();
    }   

mostrar en caja de texto

private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {                                    
        // TODO add your handling code here:
        if (evt.getButton()==1){
            int fila =jTable2.getSelectedRow();
            try {
                Habiliar();
                String sql = "select * from contactos where CODIGO=" + jTable2.getValueAt(fila, 0);
                sent= conn.createStatement();
                ResultSet rs = sent.executeQuery(sql);
                rs.next();
                txtCodigo.setText(rs.getString("CODIGO"));
                txtArticulo.setText(rs.getString("ARTICULO"));
                txtMarca.setText(rs.getString("MARCA"));
                txtPrecio.setText(rs.getString("PRECIO"));
            } catch (Exception ex) {
            ex.printStackTrace();
            }
        }
    }      

// Actualizar registro
    private void btnActualizarActionPerformed(java.awt.event.ActionEvent evt) {                                             
        // TODO add your handling code here:
        try {
            //conn = Mysql.getConnection();
            String sql ="Update contactos set ARTICULO =?, MARCA=?, PRECIO=?"
                    + "where CODIGO=?";
            int fila = jTable2.getSelectedRow();
            String dao = (String)jTable2.getValueAt(fila, 0);
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1, txtArticulo.getText());
            ps.setString(2, txtMarca.getText());
            ps.setString(3, txtPrecio.getText());
            ps.setString(4, dao);
            int n=ps.executeUpdate();
            if(n>0){
               limpiar();
               Desabilitar();
                Llenar();
                JOptionPane.showMessageDialog(null, "Datos Modificados Correctamente");
            }
        } catch (Exception e) {
                        JOptionPane.showMessageDialog(null, "Error   " + e.getMessage());
        }
    }

// Eliminar

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        try {
            int fila = jTable2.getSelectedRow();
            String sql="delete from contactos where CODIGO="+jTable2.getValueAt(fila, 0);
            sent=conn.createStatement();
            int n = sent.executeUpdate(sql);
            if (n>0){
                Llenar();
                JOptionPane.showMessageDialog(null, "Datos Eliminados Correctamente");
            }    
        } catch (Exception e) {
        }
    }

MANTENIMIENTO DE CLIENTES EN JAVA SWING



MANTENIMIENTO DE CLIENTES EN JAVA SWING


Crear una clase llamada Mysql

private static String db = "unalm";
    private static String user = "root";
    private static String pass = "";
    private static String url = "jdbc:mysql://localhost/" + db;
    private static Connection Conn;
   
    public static Connection geConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Conn=DriverManager.getConnection(url, user, pass);
           
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error  " + e.getMessage());
        }
        return Conn;
    }

Diseño de formularios



Código de los botones en los eventos

btnGuardarActionPerformed

     try {
            String sql = "insert into contactos (nombre, direccion, telefono, correo)"
                    + "values (?,?,?,?)";
            PreparedStatement ps = conn.prepareCall(sql);
            ps.setString(1, txtNombres.getText());
            ps.setString(2, txtDireccion.getText());
            ps.setString(3, txtTelefono.getText());
            ps.setString(4, txtCorreo.getText());
            int n = ps.executeUpdate();
            if (n > 0) {
                JOptionPane.showMessageDialog(null, "Datos Guardados correctamente");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error" + e.getMessage());
        }
        Llenar();
        Limpiar();

btnModificarActionPerformed

try {
           String sql ="Update contactos set nombre =?, direccion=?, telefono=?, correo=?"
                    + "where id=?";
            int fila = jTable1.getSelectedRow();
            String dao =(String)jTable1.getValueAt(fila, 0);
            PreparedStatement ps= conn.prepareStatement(sql);
            ps.setString(1, txtNombres.getText());
            ps.setString(2, txtDireccion.getText());
            ps.setString(3, txtTelefono.getText());
            ps.setString(4, txtCorreo.getText());
            ps.setString(5, dao);
           
            int n = ps.executeUpdate();
            if(n>0){
               Limpiar();
               Llenar();
               JOptionPane.showMessageDialog(null, "Datos modificados");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error " + e.getMessage());
        }

bntEliminarActionPerformed

try {
            int fila = jTable1.getSelectedRow();
            String sql ="delete from contactos where id=" + jTable1.getValueAt(fila, 0);
            sent= conn.createStatement();
            int n = sent.executeUpdate(sql);
            if(n>0){
                Llenar();
            JOptionPane.showMessageDialog(null, "Datos ELIMINADOS");
            Limpiar();
            }
           
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error " + e.getMessage());
        }


btnSalirActionPerformed

System.exit(0);


Funcion llenar

    void Llenar() {
        try {
            conn = Mysql.geConnection();
            String[] titulos = {"id", "Nombre", "Direccion", "Telefono", "Correo"};
            String sql = "select * from contactos";
            model = new DefaultTableModel(null, titulos);
            sent = conn.createStatement();
            ResultSet rs = sent.executeQuery(sql);

            String[] fila = new String[5];

            while (rs.next()) {
                fila[0] = rs.getString("id");
                fila[1] = rs.getString("nombre");
                fila[2] = rs.getString("direccion");
                fila[3] = rs.getString("telefono");
                fila[4] = rs.getString("correo");

                model.addRow(fila);

            }
            jTable1.setModel(model);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

DefaultTableModel model;
    Connection conn;
    Statement sent;

    /**
     * Creates new form Cliente
     */
    public Cliente() {
        initComponents();
        conn = Mysql.geConnection();
        Desabilitar();
        Llenar();
    }

    void Desabilitar() {
        txtNombres.setEditable(false);
        txtDireccion.setEditable(false);
        txtCorreo.setEditable(false);
        txtTelefono.setEditable(false);
    }

    void Limpiar() {
        txtNombres.setText("");
        txtDireccion.setText("");
        txtCorreo.setText("");
        txtTelefono.setText("");
    }

    void Habilitar() {
        txtNombres.setEditable(true);
        txtDireccion.setEditable(true);
        txtCorreo.setEditable(true);
        txtTelefono.setEditable(true);
        txtNombres.requestFocus();
    }

jTable1MouseClicked

if (evt.getButton()==1){
            int fila =jTable1.getSelectedRow();
            try {
                Habilitar();
                String sql = "select * from contactos where id=" + jTable1.getValueAt(fila, 0);
                sent= conn.createStatement();
                ResultSet rs = sent.executeQuery(sql);
                rs.next();
                txtNombres.setText(rs.getString("nombre"));
                txtDireccion.setText(rs.getString("direccion"));
                txtTelefono.setText(rs.getString("telefono"));
                txtCorreo.setText(rs.getString("correo"));
               
               
            } catch (Exception ex) {
            ex.printStackTrace();
            }
        }

RESULTADO