validate data entry to jTable in java netbeans

1

How to know when nothing is entered from the database into the table? I do my query and the result is entered into the table, but when nothing is entered, it means that the searched name is not in the database. At that time I want to send a message that it is not in the database, just that I do not know how to validate that there is nothing in the Jtable.

private void BuscarNomActionPerformed(java.awt.event.ActionEvent evt) {        
     String[] titulos = {"NOMBRE","#                REPORTE","GRUPO","SISTEMA","FECHA","MOTIVO","SUSPENSION"};
        String[] registro = new String[7];

  DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  Connection unaConexion = (Connection)      DriverManager.getConnection("jdbc:mysql://localhost/umaya", "root", "");
        java.sql.Statement instruccionSQL = unaConexion.createStatement();

        String Nomb=Nombre.getText();

        modelo = new DefaultTableModel(null, titulos);

        Conexion mysql = new Conexion();
        Connection cn = mysql.Conectar();
        Statement st;

 sSQL = "SELECT nombre,numero_reporte,gradoygrupo,sistema,fecha,motivo,suspension FROM generaldb WHERE nombre LIKE '%"+Nomb+"%'";



          String cadena;
              Integer co=0;

                try {

                    st = cn.createStatement();
                    ResultSet rs = st.executeQuery(sSQL);

                    while (rs.next()) {
                            String cow = rs.getString("nombre");
                     //INGRESO EL RESULTADO A MI TABLA      
                            registro[0] = rs.getString("nombre");
                            registro[1] = rs.getString("numero_reporte");
                            registro[2] = rs.getString("gradoygrupo");
                            registro[3] = rs.getString("sistema");
                            registro[4] = rs.getString("fecha");
                            registro[5] = rs.getString("motivo");
                            registro[6] = rs.getString("suspension");

                            modelo.addRow(registro);
           }
                            Tabla.setModel(modelo);
                     }
                    catch (SQLException ex) 
                    {
                            JOptionPane.showMessageDialog(null, "no se     ingreso a la base de datos ");
                    }

       }
    
asked by Daniel Rodriguez 27.12.2016 в 17:37
source

3 answers

1

The DefaultTableModel has a method to know how many rows (rows) it has. After the cycle you can ask for the number of rows.

try {

     st = cn.createStatement();
     ResultSet rs = st.executeQuery(sSQL);

     while (rs.next()) {
        String cow = rs.getString("nombre");
        //INGRESO EL RESULTADO A MI TABLA      
        registro[0] = rs.getString("nombre");
        registro[1] = rs.getString("numero_reporte");
        registro[2] = rs.getString("gradoygrupo");
        registro[3] = rs.getString("sistema");
        registro[4] = rs.getString("fecha");
        registro[5] = rs.getString("motivo");
        registro[6] = rs.getString("suspension");

        modelo.addRow(registro);
     }
     Tabla.setModel(modelo);

     if( modelo.getRowCount() == 0 )
     {
        JOptionPane.showMessageDialog(null, "El nombre buscado no existe en la Base de datos.");
     }


} catch (SQLException ex) {
     JOptionPane.showMessageDialog(null, "no se     ingreso a la base de datos ");
 }
    
answered by 04.10.2018 в 07:39
0

After closing the while , add the following:

if(model.getRowCount() == 0){
    //Lanzas el mensaje
}

After adding all the rows to the model, the check is made. It is not correct that this check is added within the loop while

    
answered by 17.03.2017 в 10:56
0

You can make a slight modification to your code:

          String row = null;
    while (rs.next()) {
                      row = rs.getString("nombre"); // si el valor del campo no existe retorna un null, sino el valor del registro
                     //INGRESO EL RESULTADO A MI TABLA      
                            registro[0] = rs.getString("nombre");
                            registro[1] = rs.getString("numero_reporte");
                            registro[2] = rs.getString("gradoygrupo");
                            registro[3] = rs.getString("sistema");
                            registro[4] = rs.getString("fecha");
                            registro[5] = rs.getString("motivo");
                            registro[6] = rs.getString("suspension");

                            modelo.addRow(registro);
           }
               if(row != null) Tabla.setModel(modelo);
    
answered by 09.12.2017 в 01:24