Question

I have a table in my SQL in the following structure called student:

+-----------+-----------+-----------+------------+
| studentno | lastname  | firstname | middlename |
+-----------+-----------+-----------+------------+
| 2001-0001 | Matrix    | John      |    G       | 
| 2001-0002 | Mata      | Evan      |    A       | 
| 2001-0003 | Melly     | Carmelo   |    P       |
| 2001-0004 | Hamburger | Hardy     |    P       |
+-----------+-----------+-----------+------------+

I have a JTable and a JTextField in my Java Program. What I would like to do is, if I type letter M (case insensitive) in the JTextField, all the student who's last name starts with letter M will display on my JTable. If I type a second letter, say for example, A, all the students who's last name starts with MA will display, until I got a specific last name on my JTable..

My problem is that, even though I type letter M, all of the data in my MAappears on my JTable instead of just all the students who's last name starts with letter M.. And my JTable doesn't clears up even if I empty my JTextField..

I am using Netbeans and so far I have this code in my JTextFieldKeyPressed:

private void lastNameKeyPressed(java.awt.event.KeyEvent evt) {                                    


  Connection conn = null;

    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "students_dbs";
    Statement stmt = null;
    ResultSet result = null;
    String driver = "com.mysql.jdbc.Driver";
    String databaseUserName = "user1";
    String databasePassword = "test";
    PreparedStatement pst = null;

    try{
    conn = DriverManager.getConnection(url + dbName, databaseUserName, databasePassword);
    stmt = conn.createStatement();
    System.out.println("Connected to the database.");
    }catch(Exception e){
        System.out.println("Failed to connect ot the database.");
    }


try{
String sql = "SELECT studentno, lastname, firstname, middlename FROM student WHERE lastname LIKE '%" + lastname.getText() + "%'";
pst = conn.prepareStatement(sql);
result = pst.executeQuery();
studentsTable.setModel(DbUtils.resultSetToTableModel(result));
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}  

}   
Was it helpful?

Solution

First you need to add a listener to Listen for changes in the text, then send that text to your query to retrieve new data based on the passed text.

// Listen for changes in the text
textField.getDocument().addDocumentListener(new DocumentListener() {
  public void changedUpdate(DocumentEvent e) {
    warn();
  }
  public void removeUpdate(DocumentEvent e) {
    warn();
  }
  public void insertUpdate(DocumentEvent e) {
    warm();
  }

  public void warn() {
     // fire the execute statement.
  }
});

then you need to change your query to something like this:

String sql = "SELECT studentno, lastname, firstname, middlename FROM student WHERE firstname= '" + jtextfield.getText() + "%'";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top