简体   繁体   中英

How to filter data from mysql and show it in jtable using java

I have a JTable(t1) & JTextField(txtSearch) & JButton(btnSearch) in my Java Program. What I would like to do is, if I type letter A (case insensitive) in the JTextField And Press the Button, all the doctor who's name Contain letter A will display on my JTable. until I get a specific name on my JTable.

that's my code,

    btnSearch = new JButton();
    btnSearch.setBackground(new java.awt.Color(51, 51, 255));
    btnSearch.setText("Search");
    btnSearch.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
                String search =txtSearch.getText(); 
                try{
                    conn = DriverManager.getConnection( "jdbc:mysql://localhost/bibodent", "root", "" );
                String query = "SELECT name_doctor, mobile_doctor, phone_doctor, mail_doctor, city_doctor, spec_doctor, adres_doctor, note_doctor FROM doctor WHERE name_doctor LIKE '%" + txtName.getText() + "%' ";

                ps = conn.prepareStatement(query);
                ps.setString(1, search);
                ResultSet rs = ps.executeQuery(query);
                t1.setModel(DbUtils.resultSetToTableModel(rs));

            }catch (Exception e){
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, e);
            }

        }
    });

In your code there is query string as:

SELECT ... FROM doctor WHERE name_doctor LIKE '%" + txtName.getText() + "%' "

which does not contain any ? mark in it. So there is no need to have ps.setString(1, search); because you've set the parameter of your query just inline by appending txtName.getText() in your query string as:

SELECT ... FROM doctor WHERE name_doctor LIKE '%" + txtName.getText() + "%' "

This way of appending parameters in the query string is not advised because it will be buggy and also there is no type checking in it. The more elegant way which you used somehow is to use PreparedStatement and set the parameters of the query as ? mark in your query.

Then as MadProgrammer indicated for each ? in your query you should set the parameter use ps.setString(n, search); while n is staring at 1 for the first appearing ? . So you may want to change your query string as:

SELECT ... FROM doctor WHERE name_doctor LIKE ? and then set the parameter using ps.setString(1, "%" + search + "%"); and it should work.

The more important point in your code is where you are executing your query:

ResultSet rs = ps.executeQuery(query);

After you define a query string with ? in it and then setting the parameters using ps.setString(n, search); it's a common mistake to executing the ps with the argument of query ! In this way the ps ignores the set parameters and then runs the raw query with ? in it. The reason you didn't get this error is because you didn't parameterized your query string with ? . So after you've set ps.setString(n, search); you should execute your ps without any argument:

ResultSet rs = ps.executeQuery();

Hope this helps!

This will search for all name_doctor with 'A' in it.

String query="Select * From doctor where name_doctor like %A%";

This doc has more details: https://www.tutorialspoint.com/mysql/mysql-like-clause.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM