如何对数据库进行多条件的查询??? 谢谢大家了~~~~~~~
有3个文本框 输入查询的条件 文本框的名字就是(B,RX,BJ)这三个文本框的值也就是条件,我写了一个SQL语句只能三个条件同时选中才能查询,我想让它选一个能查询选二个也能查询,应该怎么做,谢谢大家了
if(B==null||B.trim().length()==0||RX==null||RX.trim().length()==0||BJ==null||BJ.trim().length()==0)
sqlString=new String( "select * from ZC_XJGL ");
else
if(RX==null||RX.trim().length()==0||BJ==null||BJ.trim().length()==0)
sqlString=new String( "select * from ZC_XJGL where B= ' "+B.trim()+ " ' ");
else
if(BJ==null||BJ.trim().length()==0)
sqlString=new String( "select * from ZC_XJGL where B= ' "+B.trim()+ " ' or (RXF= ' "+RX.trim()+ " ') ");
else
sqlString=new String( "select * from ZC_XJGL where B= ' "+B.trim()+ " ' or (RXF= ' "+RX.trim()+ " ') or ( BJ= ' "+BJ.trim()+ " ') ");
%>
------解决方案--------------------你先写sql=select * from ZC_XJGL where 1=1
然后,你在判断if(RX==null||RX.trim().length()==0){
sql=sql+ "and b= ' "+rx+ " ';
}
这样就不用像你那样判断了。
------解决方案--------------------sqlString=select * from ZC_XJGL ";
sqlStr= " ";
if(B!=null && B.trim().length()> 0)
sqlStr=sqlStr + or "B = " + B.trim();
if(RX!=null && RX.trim().length()> 0)
sqlStr=sqlStr + or "RX = " + RX.trim();
if(BJ!=null && BJ.trim().length()> 0)
sqlStr=sqlStr + or "BJ = " + BJ.trim();
if (!sqlStr.equals( " "))
sqlString = sqlString + " where 1=1 " + sqlStr ;