简体   繁体   中英

Retrieving Data from MySQL With Partial Data

Currently, I use a search field in JAVA to obtain information about an employee using a MySQL search as outlined below:

String sql = "select * from CISData where NAME = ?";

        pst=conn.prepareStatement(sql);
        pst.setString(1, searchPeopleField.getText());
        rs=pst.executeQuery();

        if(rs.next()) {
            String add1 = rs.getString("MONTHYEAR");
            ymField.setText(add1);
            String add2 = rs.getString("NAME");
            nameField.setText(add2);
            String add3 = rs.getString("REFERENCENO");
            ernField.setText(add3);
            String add4 = rs.getString("UTR");
            utrField.setText(add4);
            String add5 = rs.getString("VERIFICATIONNO");
            vrField.setText(add5);
            String add6 = rs.getString("MONTHENDING");
            meField.setText(add6);
            String add7 = rs.getString("GAP");
            gapField.setText(add7);
            String add8 = rs.getString("LCM");
            lcmField.setText(add8);
            String add9 = rs.getString("ALD");
            aldField.setText(add9);
            String add10 = rs.getString("AD");
            adField.setText(add10);
            String add11 = rs.getString("AP");
            apField.setText(add11);    
        }

As you can see from the above SQL statement, I select the data appropriate to the correct employee using their name which is saved under the 'NAME' column in a MySQL database.

The field 'NAME' in the MySQL database table contains their full name, for example, 'John Doe'. Now the problem with this approach, is that it only retrieves the information when I type the full name in, for example, 'John Doe'.

Now, my question is, is it possible to somehow suggest the full name to me when I type in 'Joh' instead of actually typing in their full name - 'John Doe'?

Thanks in advance!

使用LIKE查询( WHERE NAME LIKE 'Joh%' ),计算结果,如果超过一个,则表示“建议”

I think you just need to use LIKE in your sql query.

String name = searchPeopleField.getText();//Joh
String sql = 'select * from CISData where NAME LIKE' +name+'%';

您可以使用AJAX调用来满足此要求。

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