简体   繁体   中英

using LIKE and OR in the where clause of select statement result to this ' Too few parameters. Expected 1.'

When I query in my database microsoft access, with this code..

dc.rs = dc.st.executeQuery("select count(*) from Accounts where username like '%"+searchTF.getText()+"%' OR firstname like '%"+searchTF.getText()+"%'");

I got this result, not sure if this is error because it is written in black text.

[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

I want to search by username or by firstname that's why i put an OR there.

Do anyone know where I got some problem?

Microsoft Access uses * for the wildcard

MS Access also uses single quotes according to this page

http://refactoringself.com/2011/06/22/ms-access-error-too-few-parameters-expected-x/

using a prepared Statement

String queryString = "select count(*) from Accounts where username like ? OR firstname like ?";
PreparedStatement  stmt= con.prepareStatement(queryString );
stmt.setString(1, "*" + searchTF.getText() + "*");
stmt.setString(2, "*" + searchTF.getText() + "*");
stmt.executeQuery();

or the less secure way

dc.rs = dc.st.executeQuery("select count(*) from Accounts where username like " + 
    "\'*" + searchTF.getText() + "\'* OR firstname like \'*" + searchTF.getText() + "*\'");

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