Tell me, how can I display entries from the table according to the Id entered in the text box ( searchBox - fx:id text box for entering id )?

Tried to do the following way:

 @FXML private void searchUser(ActionEvent event) { String sqlfiltr = ("SELECT * FROM teachers WHERE id = '"+searchBox+"'"); try{ Connection conn = dbConnection.getConnection(); this.data = FXCollections.observableArrayList(); ResultSet rs = conn.createStatement().executeQuery(sqlfiltr); while (rs.next()){ this.data.add(new TeacherData(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5))); } } catch (SQLException e){ System.err.println("Oshibka " + e); } this.idTableColumn.setCellValueFactory(new PropertyValueFactory<TeacherData, String>("ID")); this.fnamecolum.setCellValueFactory(new PropertyValueFactory<TeacherData, String>("firstname")); this.lnamecolum.setCellValueFactory(new PropertyValueFactory<TeacherData, String>("lastname")); this.emailcolum.setCellValueFactory(new PropertyValueFactory<TeacherData, String>("email")); this.coursecolum.setCellValueFactory(new PropertyValueFactory<TeacherData, String>("course")); this.teachertable.setItems(null); this.teachertable.setItems(this.data); } 

    1 answer 1

    And what kind of error occurs?

    In general, I would write this:

     String sqlfiltr = ("SELECT * FROM teachers WHERE id = " + searchBox); 

    If id is a number, then "" are not needed, sort of.

    • The error did not occur, just passed and the table did not fill. I tried as you suggested: Oshibka org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (near "[id = searchBox, styleClass = text-input text-field]": syntax error) studer
    • one
      And so: String sqlfiltr = ("SELECT * FROM teachers WHERE id =?"); PreparedStatement stmt = conn.prepareStatement (sqlfiltr); stmt.setString (1, searchBox); ResultSet rs = stmt.executeQuery (); - EtcOS
    • Thanks for the answer. I tried that. In principle, he selects the desired row and displays it in a table, but to the position below the existing records, i.e. he appends the row selected by id to the table. Can you tell me how I can clear the table and then add only 1 this line there? - studer
    • one
      @studer displays the entire contents of this.data. If you need to display only a new line, just clear the collection of old elements - Victor
    • @ Victor Thank you, I did not think. It helped. - studer