one Field for 2 parameters in JAVA

时间:2015-04-23 05:36:41

标签: java mysql

public ArrayList searchCustomer(String cid) throws SQLException {


ArrayList searchCustList = new ArrayList();



PreparedStatement pStmt = connection.prepareStatement("select * from customer where (custID = ? OR firstName LIKE ?)");


 pStmt.setString(1, cid);


pStmt.setString(2, "%" + cid + "%");

please explain last command i used one text field for search customer by name or ID can any body explain last line

1 个答案:

答案 0 :(得分:1)

Your question is unclear, but if you want to understand :pStmt.setString(2, "%" + cid + "%");

Then it set the second parameter in sql query to the value of cid variable, and add % around

Adding % around, mean in an SQL Like 'any character', so having %cid% mean anything containing cid in it.

As the actual query use cid for either custId or firstName, it mean that it look for user having a specific id, or having in its firstname the id.

Which is strange, and looks like more a bug, than a logical query, but maybe it come from old legacy having some id in firstname, who knows