Skip to main content
título editado
Enlace
user22539
user22539

Ayuda: No me muesta nada la hora de consultar la información en la interfaz de Java

se añadieron 35 caracteres en el cuerpo
Origen Enlace
public class datos extends javax.swing.JFrame { <- Error

/**
 * Creates new form Datos
 */
String filtro="id";
DefaultTableModel model;

@Override
public Image getIconImage(){
  Image retValue = Toolkit.getDefaultToolkit().
  getImage(ClassLoader.getSystemResource("imagenes/agregar-usuario.png"));
  return retValue;
    
}

public datos() {
    initComponents();
    tablaDatos("");
}
void tablaDatos(String Valor){
Connection conectar = null;
 
 try{
 
     Class.forName("com.mysql.jdbc.Driver");
     conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");

 
 }  catch (ClassNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
 
  if(combo.getSelectedItem()== "id"){
 filtro= "id";
 
 }
 
 
 if(combo.getSelectedItem()== "nombre"){
 filtro= "Nombre";
 
 }
 if(combo.getSelectedItem()== "apellido"){
 filtro= "Apellido";
 
 }
 

 if(combo.getSelectedItem()== "compañia"){
 filtro= "Compañia";
 
 }
 String sql="SELECT * FROM datos WHERE "+filtro+" LIKE '%"+Valor+"%'";
 
    DefaultTableModel modelo = new DefaultTableModel();
     modelo.addColumn("ID");
    modelo.addColumn("Nombre");
      modelo.addColumn("Apellido");
        modelo.addColumn("Email");
          modelo.addColumn("Telefono");
            modelo.addColumn("Compañia");
              modelo.addColumn("Posicion");
                modelo.addColumn("Nota");
                
                jTable1.setModel(modelo);
                String []datos= new String [8];
                try{
                Statement st=conectar.createStatement();
                ResultSet rs=st.executeQuery(sql);
                while(rs.next()){
                datos[0]=rs.getString(1);
                datos[1]=rs.getString(2);
                datos[2]=rs.getString(3);
                datos[3]=rs.getString(4);
                datos[4]=rs.getString(5);
                datos[5]=rs.getString(6);
                datos[6]=rs.getString(7);
                datos[7]=rs.getString(8);
               
                modelo.addRow(datos);
                }
                jTable1.setModel(modelo);
                } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
                        
}


   

    pack();
}// </editor-fold>                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
 Connection conectar = null;
 
 try{
 
     Class.forName("com.mysql.jdbc.Driver");
     conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");

 
 }  catch (ClassNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
 try{
 String sql= "UPDATE datos SET id=?," +"Nombre=?,"
         + "Apellido=?,"
         + "Email=?,"
         +"Telefono=?,"
         +"Compañia=?,"
         +"Posicion=?,"
         +"Foto=?,"
         +"Nota=?,"
         +"Ruta=?" 
         +"WHERE id='"+jTextField1.getText()+"'";
  
 PreparedStatement pst= conectar.prepareStatement(sql);
        
        pst.setString(1, jTextField1.getText());
        pst.setString(2, jTextField2.getText());
        pst.setString(3, jTextField3.getText());
        pst.setString(4, jTextField4.getText());
        pst.setString(5, jTextField5.getText());
        pst.setString(6, jTextField6.getText());
        pst.setString(7, jTextField7.getText());
       pst.setString(8, jTextField8.getText());
        FileInputStream Archivofoto = new FileInputStream(jTextField10.getText());
        pst.setBinaryStream(10, Archivofoto);
        pst.setString(9, jTextField10.getText());
        int n = pst.executeUpdate();
        if (n > 0) {
            JOptionPane.showMessageDialog(null, "Se modificaron los datos de manera correcta :D");
            tablaDatos("");
            
        } else {

            JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo modificar los datos :c");

        }
 }  catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
}                                        

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    PreparedStatement pst = null;
    try {
        Connection conectar = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");

        } catch (SQLException | ClassNotFoundException ex) {
            Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
        }
        String insertar = "INSERT INTO datos( id, Nombre, Apellido,Email, Telefono, Compañia, Posicion, Nota, Foto, Ruta)VALUES(?,?,?,?,?,?,?,?,?,?)";
        pst = conectar.prepareStatement(insertar);
        pst.setString(1, jTextField1.getText());
        pst.setString(2, jTextField2.getText());
        pst.setString(3, jTextField3.getText());
        pst.setString(4, jTextField4.getText());
        pst.setString(5, jTextField5.getText());
        pst.setString(6, jTextField6.getText());
        pst.setString(7, jTextField7.getText());
        pst.setString(8, jTextField8.getText());
        FileInputStream ArchivosFoto;
        FileInputStream Archivofoto = new FileInputStream(jTextField10.getText());
        pst.setBinaryStream(9, Archivofoto);
        pst.setString(10, jTextField10.getText());
        int i = pst.executeUpdate();
        if (i > 0) {
            JOptionPane.showMessageDialog(null, "Se ha guardadado correctamente :D");
             tablaDatos("");
        } 
        else {

            JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo guardar :c");

        }

    } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            pst.close();
        } catch (SQLException ex) {
            Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}                                        

private void jLabel10MouseClicked(java.awt.event.MouseEvent evt) {                                      
    JFileChooser archivo = new JFileChooser();
    int ventana = archivo.showOpenDialog(null);
    if (ventana == JFileChooser.APPROVE_OPTION) {
        File file = archivo.getSelectedFile();
        jTextField10.setText(String.valueOf(file));
        Image foto = getToolkit().getImage(jTextField10.getText());
        jLabel10.setIcon(new ImageIcon(foto));

    }
}                                     

private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
}                                           

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
int fx=JOptionPane.showConfirmDialog(null,"¿Esta realmente seguro de eliminar este registro?"+jTextField1.getText()+"?");
if(fx==JOptionPane.YES_OPTION){
    Connection conectar = null;
 
 try{
 
     Class.forName("com.mysql.jdbc.Driver");
     conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");

 
 }  catch (ClassNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
      
 try{
 Statement s=conectar.createStatement();
 int i=s.executeUpdate("DELETE FROM datos WHERE id='"+jTextField1.getText()+"'");
 if(i==1){
  jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");
    jTextField4.setText("");
    jTextField5.setText("");
    jTextField6.setText("");
    jTextField7.setText("");
    jTextField8.setText("");
  jLabel10.setIcon(null);
  jTextField10.setText("");
  if (i > 0) {
            JOptionPane.showMessageDialog(null, "Se elimino el registro de forma exitosa :D");
            tablaDatos("");
        } else {

            JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo eliminar el registro :(");

        }
 }
 }  catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}                                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");
    jTextField4.setText("");
    jTextField5.setText("");
    jTextField6.setText("");
    jTextField7.setText("");
  jTextField8.setText("");
  jLabel10.setIcon(null);
  jTextField10.setText("");

}                                        

    

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  int row=jTable1.getSelectedRow();
  String id= String
   Error ->String.valueOf(model.getValueAt(jTable1.getSelectedRow(), 0)); <- ERROR
  if(row >= 0){
  jTextField1.setText(jTable1.getValueAt(row, 0).toString());
  jTextField2.setText(jTable1.getValueAt(row, 1).toString());
   jTextField3.setText(jTable1.getValueAt(row, 2).toString());
   jTextField6.setText(jTable1.getValueAt(row, 3).toString()); 
   
   Connection conectar=null;
   try{
   Class.forName("com.mysql.jdbc.Driver");   
   DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");   
       
   }  catch (ClassNotFoundException ex) {
          Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
      } catch (SQLException ex) {
          Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
      }
   try{
    Statement s=conectar.createStatement();
    ResultSet rs=s.executeQuery("SELECT Nombre,Apellido,Email,Telefono,Compañia,Posicion,Foto,Nota,Ruta  FROM datos WHERE id='"+id+"'");
    Image i=null;
    Blob blob=rs.getBlob("Foto");
    i=javax.imageio.ImageIO.read(blob.getBinaryStream());
    ImageIcon image = new ImageIcon(i); 
    jLabel10.setIcon(image);
    jTextField10.setText(rs.getString(3));
    
       
   }  catch (SQLException ex) {
          Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IOException ex) {
          Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
      }

  
  
  }else{
  JOptionPane.showMessageDialog(null, "No se selecciono fila");    
  }
}                                          


public static void main(String args[]) {
   
    //<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(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
   
public class datos extends javax.swing.JFrame {

/**
 * Creates new form Datos
 */
String filtro="id";
DefaultTableModel model;

@Override
public Image getIconImage(){
  Image retValue = Toolkit.getDefaultToolkit().
  getImage(ClassLoader.getSystemResource("imagenes/agregar-usuario.png"));
  return retValue;
    
}

public datos() {
    initComponents();
    tablaDatos("");
}
void tablaDatos(String Valor){
Connection conectar = null;
 
 try{
 
     Class.forName("com.mysql.jdbc.Driver");
     conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");

 
 }  catch (ClassNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
 
  if(combo.getSelectedItem()== "id"){
 filtro= "id";
 
 }
 
 
 if(combo.getSelectedItem()== "nombre"){
 filtro= "Nombre";
 
 }
 if(combo.getSelectedItem()== "apellido"){
 filtro= "Apellido";
 
 }
 

 if(combo.getSelectedItem()== "compañia"){
 filtro= "Compañia";
 
 }
 String sql="SELECT * FROM datos WHERE "+filtro+" LIKE '%"+Valor+"%'";
 
    DefaultTableModel modelo = new DefaultTableModel();
     modelo.addColumn("ID");
    modelo.addColumn("Nombre");
      modelo.addColumn("Apellido");
        modelo.addColumn("Email");
          modelo.addColumn("Telefono");
            modelo.addColumn("Compañia");
              modelo.addColumn("Posicion");
                modelo.addColumn("Nota");
                
                jTable1.setModel(modelo);
                String []datos= new String [8];
                try{
                Statement st=conectar.createStatement();
                ResultSet rs=st.executeQuery(sql);
                while(rs.next()){
                datos[0]=rs.getString(1);
                datos[1]=rs.getString(2);
                datos[2]=rs.getString(3);
                datos[3]=rs.getString(4);
                datos[4]=rs.getString(5);
                datos[5]=rs.getString(6);
                datos[6]=rs.getString(7);
                datos[7]=rs.getString(8);
               
                modelo.addRow(datos);
                }
                jTable1.setModel(modelo);
                } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
                        
}


   

    pack();
}// </editor-fold>                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
 Connection conectar = null;
 
 try{
 
     Class.forName("com.mysql.jdbc.Driver");
     conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");

 
 }  catch (ClassNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
 try{
 String sql= "UPDATE datos SET id=?," +"Nombre=?,"
         + "Apellido=?,"
         + "Email=?,"
         +"Telefono=?,"
         +"Compañia=?,"
         +"Posicion=?,"
         +"Foto=?,"
         +"Nota=?,"
         +"Ruta=?" 
         +"WHERE id='"+jTextField1.getText()+"'";
  
 PreparedStatement pst= conectar.prepareStatement(sql);
        
        pst.setString(1, jTextField1.getText());
        pst.setString(2, jTextField2.getText());
        pst.setString(3, jTextField3.getText());
        pst.setString(4, jTextField4.getText());
        pst.setString(5, jTextField5.getText());
        pst.setString(6, jTextField6.getText());
        pst.setString(7, jTextField7.getText());
       pst.setString(8, jTextField8.getText());
        FileInputStream Archivofoto = new FileInputStream(jTextField10.getText());
        pst.setBinaryStream(10, Archivofoto);
        pst.setString(9, jTextField10.getText());
        int n = pst.executeUpdate();
        if (n > 0) {
            JOptionPane.showMessageDialog(null, "Se modificaron los datos de manera correcta :D");
            tablaDatos("");
            
        } else {

            JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo modificar los datos :c");

        }
 }  catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
}                                        

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    PreparedStatement pst = null;
    try {
        Connection conectar = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");

        } catch (SQLException | ClassNotFoundException ex) {
            Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
        }
        String insertar = "INSERT INTO datos( id, Nombre, Apellido,Email, Telefono, Compañia, Posicion, Nota, Foto, Ruta)VALUES(?,?,?,?,?,?,?,?,?,?)";
        pst = conectar.prepareStatement(insertar);
        pst.setString(1, jTextField1.getText());
        pst.setString(2, jTextField2.getText());
        pst.setString(3, jTextField3.getText());
        pst.setString(4, jTextField4.getText());
        pst.setString(5, jTextField5.getText());
        pst.setString(6, jTextField6.getText());
        pst.setString(7, jTextField7.getText());
        pst.setString(8, jTextField8.getText());
        FileInputStream ArchivosFoto;
        FileInputStream Archivofoto = new FileInputStream(jTextField10.getText());
        pst.setBinaryStream(9, Archivofoto);
        pst.setString(10, jTextField10.getText());
        int i = pst.executeUpdate();
        if (i > 0) {
            JOptionPane.showMessageDialog(null, "Se ha guardadado correctamente :D");
             tablaDatos("");
        } 
        else {

            JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo guardar :c");

        }

    } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            pst.close();
        } catch (SQLException ex) {
            Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}                                        

private void jLabel10MouseClicked(java.awt.event.MouseEvent evt) {                                      
    JFileChooser archivo = new JFileChooser();
    int ventana = archivo.showOpenDialog(null);
    if (ventana == JFileChooser.APPROVE_OPTION) {
        File file = archivo.getSelectedFile();
        jTextField10.setText(String.valueOf(file));
        Image foto = getToolkit().getImage(jTextField10.getText());
        jLabel10.setIcon(new ImageIcon(foto));

    }
}                                     

private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
}                                           

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
int fx=JOptionPane.showConfirmDialog(null,"¿Esta realmente seguro de eliminar este registro?"+jTextField1.getText()+"?");
if(fx==JOptionPane.YES_OPTION){
    Connection conectar = null;
 
 try{
 
     Class.forName("com.mysql.jdbc.Driver");
     conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");

 
 }  catch (ClassNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
      
 try{
 Statement s=conectar.createStatement();
 int i=s.executeUpdate("DELETE FROM datos WHERE id='"+jTextField1.getText()+"'");
 if(i==1){
  jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");
    jTextField4.setText("");
    jTextField5.setText("");
    jTextField6.setText("");
    jTextField7.setText("");
    jTextField8.setText("");
  jLabel10.setIcon(null);
  jTextField10.setText("");
  if (i > 0) {
            JOptionPane.showMessageDialog(null, "Se elimino el registro de forma exitosa :D");
            tablaDatos("");
        } else {

            JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo eliminar el registro :(");

        }
 }
 }  catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}                                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");
    jTextField4.setText("");
    jTextField5.setText("");
    jTextField6.setText("");
    jTextField7.setText("");
  jTextField8.setText("");
  jLabel10.setIcon(null);
  jTextField10.setText("");

}                                        

    

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  int row=jTable1.getSelectedRow();
  String id= String.valueOf(model.getValueAt(jTable1.getSelectedRow(), 0));
  if(row >= 0){
  jTextField1.setText(jTable1.getValueAt(row, 0).toString());
  jTextField2.setText(jTable1.getValueAt(row, 1).toString());
   jTextField3.setText(jTable1.getValueAt(row, 2).toString());
   jTextField6.setText(jTable1.getValueAt(row, 3).toString()); 
   
   Connection conectar=null;
   try{
   Class.forName("com.mysql.jdbc.Driver");   
   DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");   
       
   }  catch (ClassNotFoundException ex) {
          Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
      } catch (SQLException ex) {
          Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
      }
   try{
    Statement s=conectar.createStatement();
    ResultSet rs=s.executeQuery("SELECT Nombre,Apellido,Email,Telefono,Compañia,Posicion,Foto,Nota,Ruta  FROM datos WHERE id='"+id+"'");
    Image i=null;
    Blob blob=rs.getBlob("Foto");
    i=javax.imageio.ImageIO.read(blob.getBinaryStream());
    ImageIcon image = new ImageIcon(i); 
    jLabel10.setIcon(image);
    jTextField10.setText(rs.getString(3));
    
       
   }  catch (SQLException ex) {
          Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IOException ex) {
          Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
      }

  
  
  }else{
  JOptionPane.showMessageDialog(null, "No se selecciono fila");    
  }
}                                          


public static void main(String args[]) {
   
    //<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(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
   
public class datos extends javax.swing.JFrame { <- Error

/**
 * Creates new form Datos
 */
String filtro="id";
DefaultTableModel model;

@Override
public Image getIconImage(){
  Image retValue = Toolkit.getDefaultToolkit().
  getImage(ClassLoader.getSystemResource("imagenes/agregar-usuario.png"));
  return retValue;
    
}

public datos() {
    initComponents();
    tablaDatos("");
}
void tablaDatos(String Valor){
Connection conectar = null;
 
 try{
 
     Class.forName("com.mysql.jdbc.Driver");
     conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");

 
 }  catch (ClassNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
 
  if(combo.getSelectedItem()== "id"){
 filtro= "id";
 
 }
 
 
 if(combo.getSelectedItem()== "nombre"){
 filtro= "Nombre";
 
 }
 if(combo.getSelectedItem()== "apellido"){
 filtro= "Apellido";
 
 }
 

 if(combo.getSelectedItem()== "compañia"){
 filtro= "Compañia";
 
 }
 String sql="SELECT * FROM datos WHERE "+filtro+" LIKE '%"+Valor+"%'";
 
    DefaultTableModel modelo = new DefaultTableModel();
     modelo.addColumn("ID");
    modelo.addColumn("Nombre");
      modelo.addColumn("Apellido");
        modelo.addColumn("Email");
          modelo.addColumn("Telefono");
            modelo.addColumn("Compañia");
              modelo.addColumn("Posicion");
                modelo.addColumn("Nota");
                
                jTable1.setModel(modelo);
                String []datos= new String [8];
                try{
                Statement st=conectar.createStatement();
                ResultSet rs=st.executeQuery(sql);
                while(rs.next()){
                datos[0]=rs.getString(1);
                datos[1]=rs.getString(2);
                datos[2]=rs.getString(3);
                datos[3]=rs.getString(4);
                datos[4]=rs.getString(5);
                datos[5]=rs.getString(6);
                datos[6]=rs.getString(7);
                datos[7]=rs.getString(8);
               
                modelo.addRow(datos);
                }
                jTable1.setModel(modelo);
                } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
                        
}


   

    pack();
}// </editor-fold>                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
 Connection conectar = null;
 
 try{
 
     Class.forName("com.mysql.jdbc.Driver");
     conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");

 
 }  catch (ClassNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
 try{
 String sql= "UPDATE datos SET id=?," +"Nombre=?,"
         + "Apellido=?,"
         + "Email=?,"
         +"Telefono=?,"
         +"Compañia=?,"
         +"Posicion=?,"
         +"Foto=?,"
         +"Nota=?,"
         +"Ruta=?" 
         +"WHERE id='"+jTextField1.getText()+"'";
  
 PreparedStatement pst= conectar.prepareStatement(sql);
        
        pst.setString(1, jTextField1.getText());
        pst.setString(2, jTextField2.getText());
        pst.setString(3, jTextField3.getText());
        pst.setString(4, jTextField4.getText());
        pst.setString(5, jTextField5.getText());
        pst.setString(6, jTextField6.getText());
        pst.setString(7, jTextField7.getText());
       pst.setString(8, jTextField8.getText());
        FileInputStream Archivofoto = new FileInputStream(jTextField10.getText());
        pst.setBinaryStream(10, Archivofoto);
        pst.setString(9, jTextField10.getText());
        int n = pst.executeUpdate();
        if (n > 0) {
            JOptionPane.showMessageDialog(null, "Se modificaron los datos de manera correcta :D");
            tablaDatos("");
            
        } else {

            JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo modificar los datos :c");

        }
 }  catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
}                                        

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    PreparedStatement pst = null;
    try {
        Connection conectar = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");

        } catch (SQLException | ClassNotFoundException ex) {
            Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
        }
        String insertar = "INSERT INTO datos( id, Nombre, Apellido,Email, Telefono, Compañia, Posicion, Nota, Foto, Ruta)VALUES(?,?,?,?,?,?,?,?,?,?)";
        pst = conectar.prepareStatement(insertar);
        pst.setString(1, jTextField1.getText());
        pst.setString(2, jTextField2.getText());
        pst.setString(3, jTextField3.getText());
        pst.setString(4, jTextField4.getText());
        pst.setString(5, jTextField5.getText());
        pst.setString(6, jTextField6.getText());
        pst.setString(7, jTextField7.getText());
        pst.setString(8, jTextField8.getText());
        FileInputStream ArchivosFoto;
        FileInputStream Archivofoto = new FileInputStream(jTextField10.getText());
        pst.setBinaryStream(9, Archivofoto);
        pst.setString(10, jTextField10.getText());
        int i = pst.executeUpdate();
        if (i > 0) {
            JOptionPane.showMessageDialog(null, "Se ha guardadado correctamente :D");
             tablaDatos("");
        } 
        else {

            JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo guardar :c");

        }

    } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            pst.close();
        } catch (SQLException ex) {
            Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}                                        

private void jLabel10MouseClicked(java.awt.event.MouseEvent evt) {                                      
    JFileChooser archivo = new JFileChooser();
    int ventana = archivo.showOpenDialog(null);
    if (ventana == JFileChooser.APPROVE_OPTION) {
        File file = archivo.getSelectedFile();
        jTextField10.setText(String.valueOf(file));
        Image foto = getToolkit().getImage(jTextField10.getText());
        jLabel10.setIcon(new ImageIcon(foto));

    }
}                                     

private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
}                                           

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
int fx=JOptionPane.showConfirmDialog(null,"¿Esta realmente seguro de eliminar este registro?"+jTextField1.getText()+"?");
if(fx==JOptionPane.YES_OPTION){
    Connection conectar = null;
 
 try{
 
     Class.forName("com.mysql.jdbc.Driver");
     conectar = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");

 
 }  catch (ClassNotFoundException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
      
 try{
 Statement s=conectar.createStatement();
 int i=s.executeUpdate("DELETE FROM datos WHERE id='"+jTextField1.getText()+"'");
 if(i==1){
  jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");
    jTextField4.setText("");
    jTextField5.setText("");
    jTextField6.setText("");
    jTextField7.setText("");
    jTextField8.setText("");
  jLabel10.setIcon(null);
  jTextField10.setText("");
  if (i > 0) {
            JOptionPane.showMessageDialog(null, "Se elimino el registro de forma exitosa :D");
            tablaDatos("");
        } else {

            JOptionPane.showMessageDialog(null, "Ocurrio un error, no se pudo eliminar el registro :(");

        }
 }
 }  catch (SQLException ex) {
        Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}                                        

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");
    jTextField4.setText("");
    jTextField5.setText("");
    jTextField6.setText("");
    jTextField7.setText("");
  jTextField8.setText("");
  jLabel10.setIcon(null);
  jTextField10.setText("");

}                                        

    

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  int row=jTable1.getSelectedRow();
  String id= 
   Error ->String.valueOf(model.getValueAt(jTable1.getSelectedRow(), 0)); <- ERROR
  if(row >= 0){
  jTextField1.setText(jTable1.getValueAt(row, 0).toString());
  jTextField2.setText(jTable1.getValueAt(row, 1).toString());
   jTextField3.setText(jTable1.getValueAt(row, 2).toString());
   jTextField6.setText(jTable1.getValueAt(row, 3).toString()); 
   
   Connection conectar=null;
   try{
   Class.forName("com.mysql.jdbc.Driver");   
   DriverManager.getConnection("jdbc:mysql://127.0.0.1/proyecto", "root", "");   
       
   }  catch (ClassNotFoundException ex) {
          Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
      } catch (SQLException ex) {
          Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
      }
   try{
    Statement s=conectar.createStatement();
    ResultSet rs=s.executeQuery("SELECT Nombre,Apellido,Email,Telefono,Compañia,Posicion,Foto,Nota,Ruta  FROM datos WHERE id='"+id+"'");
    Image i=null;
    Blob blob=rs.getBlob("Foto");
    i=javax.imageio.ImageIO.read(blob.getBinaryStream());
    ImageIcon image = new ImageIcon(i); 
    jLabel10.setIcon(image);
    jTextField10.setText(rs.getString(3));
    
       
   }  catch (SQLException ex) {
          Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IOException ex) {
          Logger.getLogger(datos.class.getName()).log(Level.SEVERE, null, ex);
      }

  
  
  }else{
  JOptionPane.showMessageDialog(null, "No se selecciono fila");    
  }
}                                          


public static void main(String args[]) {
   
    //<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(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(datos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
   

[![introducir la descripción de la imagen aquí][1]][1]LesLes explico, estoy haciendo un programa de guardar la información de un contacto con MysqlMysql y estoy poniendo un filtrofiltro de búsqueda en la interfaz de netbeansNetbeans, hace la busqueda pero a la hora de seleccionar a una persona en la tabla no me muestra nada y me da este error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

[![introducir la descripción de la imagen aquí][1]][1]Les explico, estoy haciendo un programa de guardar la información de un contacto con Mysql y estoy poniendo un filtro de búsqueda en la interfaz de netbeans, hace la busqueda pero a la hora de seleccionar a una persona en la tabla no me muestra nada y me da este error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

Les explico, estoy haciendo un programa de guardar la información de un contacto con Mysql y estoy poniendo un filtro de búsqueda en la interfaz de Netbeans, hace la busqueda pero a la hora de seleccionar a una persona en la tabla no me muestra nada y me da este error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
se añadieron 12007 caracteres en el cuerpo; etiquetas editadas
Origen Enlace
Loading
cambio tags
Enlace
Orici
  • 7.8k
  • 13
  • 50
  • 89
Loading
se añadieron 100 caracteres en el cuerpo
Origen Enlace
Loading
Origen Enlace
Loading