Вы находитесь на странице: 1из 4

Mantenimiento de Clientes

Codigo

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(); } } private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Limpiar(); Habilitar(); } private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: 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(); } private void jTable1MouseClicked(java.awt.event.MouseEvent evt) { // TODO add your handling code here: 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(); } } } private void bntEliminarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: 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()); } } private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: 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()); } } private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: // funcion para buscar try { conn = Mysql.geConnection(); String[] titulos = {"id", "Nombre", "Direccion", "Telefono", "Correo"}; String sql = "select * from contactos where nombre like '%" + txtBusqueda.getText()+"%'"; 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(); } } Resultado

Вам также может понравиться