sql搜索where子句

时间:2016-06-09 07:31:18

标签: java sql-server

我想根据siteid(integer)中的refrencenooldrefrencenoaccount(table)进行搜索,但我的查询仅搜索refrencno(varchar(15))oldrefrenceno(varchar(50)) 。当用户输入siteid搜索数据时,它不显示任何内容。

Statement pst=null;
pst=ccn.createStatement();
String a=txt_search.getText();
String sql = "";
try {
    int s = Integer.parseInt(siteid.getText());
    sql ="select * from account WHERE siteid ="+s+" refrenceno='"+a+"' or oldrefrenceno='"+a+"' ";
}catch (Exception ex){
    sql ="select * from account WHERE refrenceno='"+a+"' or oldrefrenceno='"+a+"' ";   
}
ResultSet rs=pst.executeQuery(sql);

1 个答案:

答案 0 :(得分:0)

如果您想基于三个字段进行搜索,那么您的查询应该是

sql ="select * from account WHERE siteid ="+s+" AND refrenceno='"+a+"' AND oldrefrenceno='"+a+"' ";