2

I am developing a JavaFX Application which has access to a Database through JDBC/ODBC. When I click on a button, I want some TextFields to update their texts according to a ResultSet, showing the First Name and Last Name of a certain user.

When I try to invoke the TEXTFIELD.setText(RESULTSET.getString(1)) of the TextFields, I get an SQLException: No data found.

Here's the code:

@FXML
private TextField tf_matricula;

@FXML
TextField tf_apellido;

@FXML
TextField tf_apellidos;

@FXML
TextField tf_nombre;

@FXML
public void btn_ir_Action(ActionEvent event){


        try(Connection con  = JDBCHandler.getConnection()){
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT Apellido, Apellidos, Nombre FROM Alumnos WHERE Matricula='" + tf_matricula.getText() + "'");
            if(rs.next()){
                tf_apellido.setText(rs.getString(1));
                tf_apellidos.setText(rs.getString(2));
                tf_nombre.setText(rs.getString(3));
            }
        }catch(SQLException | ClassNotFoundException ex){
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }

}

And the Error Message I get is the following:

No data found
java.sql.SQLException: No data found
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7137)
    at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3906)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:5697)
    at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:353)
    at pruebaingresos.SistemasAlumnosFXMLController.btn_ir_Action(SistemasAlumnosFXMLController.java:64)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:75)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)

The weird thing is that when I save the ResultSet information into Strings and then pass them to the setText method of the TextFields, then it works perfectly. As shown below

@FXML
public void btn_ir_Action(ActionEvent event){

        try(Connection con  = JDBCHandler.getConnection()){
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("SELECT Apellido, Apellidos, Nombre FROM Alumnos WHERE Matricula='" + tf_matricula.getText() + "'");
            if(rs.next()){
                String apellido = rs.getString(1);
                String apellidos = rs.getString(2);
                String nombre = rs.getString(3);
                tf_apellido.setText(apellido);
                tf_apellidos.setText(apellidos);
                tf_nombre.setText(nombre);
            }
        }catch(SQLException | ClassNotFoundException ex){
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }

}

I know this problem is practically solved, but I still wonder why this happens, I don't want to have any bugs for this reason later on, and I haven't found the reason yet.

chanklor
  • 487
  • 5
  • 11

0 Answers0