sql="select * from dzxx_b";
if((this.r1.getText().trim())!=null){
if((this.r2.getText().trim())!=null){
sql+="where dzbh='"+this.r1.getText()+"' and dzxm='"+this.r2.getText()+"'";
}
else
sql="where dzbh='"+this.r1.getText()+"'";
}
else
{
if((this.r2.getText().trim())!=null){
sql+="where dzxm='"+this.r2.getText()+"'";
}
}
我是想在2个文本中任意输入条件,实现查询;
请大家帮帮忙,为什么老师输入第一个 条件点查询出现select * from dzxx_b where dzbh='输入内容' and dzxm='',为什么后面那条件也出来了;
if((this.r1.getText().trim())!=null){
if((this.r2.getText().trim())!=null){
sql+="where dzbh='"+this.r1.getText()+"' and dzxm='"+this.r2.getText()+"'";
}
else
sql="where dzbh='"+this.r1.getText()+"'";
}
else
{
if((this.r2.getText().trim())!=null){
sql+="where dzxm='"+this.r2.getText()+"'";
}
}
我是想在2个文本中任意输入条件,实现查询;
请大家帮帮忙,为什么老师输入第一个 条件点查询出现select * from dzxx_b where dzbh='输入内容' and dzxm='',为什么后面那条件也出来了;
--------------------------------------------
看看这个问题,或许对你有帮助:)
为什么在JSP中将传进来的参数要判断是不是为空null还要判断是不是为""呢,为什么啊双重判断呢
http://community.csdn.net/Expert/topic/5285/5285513.xml?temp=.3930475
祝:进步。
把SQL输出,然后放到数据库里去跑一下,为社么不对~不就一目了然了.
------------------------------------------------------------------
String sql="select * from dzxx_b where 1=1";
String fieldFirstText = this.r1.getText().trim();
String fieldSecondText = this.r2.getText().trim();
if(fieldFirstText != null && !fieldFirstText.equals("")){
sql = sql + " and dzbh='"+fieldFirstText+"'";
}
if(fieldSecondText != null && !fieldSecondText.equals("")){
sql = sql + " and dzxm='"+fieldSecondText+"'";
}
------------------------------------------------------------------
(this.r1.getText().trim())!=null始终都是true
所以,下面代码始终都不会被执行:
else{
if((this.r2.getText().trim())!=null){
sql+="where dzxm='"+this.r2.getText()+"'";
}
} 无论你的r2是否输入
(this.r2.getText().trim())!=null始终都是true
所以,下面这段代码始终都会进入:
sql+="where dzbh='"+this.r1.getText()+"' and dzxm='"+this.r2.getText()+"'";