日期:2014-05-17  浏览次数:20778 次

急!!用if 进行条件判断,里面有sql语句 报错
public List<Affair> queryByTime(String year,String month,String date){
List <Affair> list=new ArrayList<Affair>();
Affair affair;
try{
con=TestDataBase.getConnection();
stmt=con.createStatement();
if(year!=null&&month!=null&&date!=null){
String str="select * from affair where left(time,4)="+year+"and mid(time,6,2)="+month+"and mid(time,9,2)="+date;
rs=stmt.executeQuery(str);
System.out.println(str);
}
else if(year!=null&&month!=null&&date==null){
String str="select * from affair where left(time,4)="+year+"and mid(time,6,2)="+month;
rs=stmt.executeQuery(str);
System.out.println(str);
}
上面是代码  if else if 中  当满足第一个if的值时第一个if里的内容会执行  但是如果满足的是else if时  else if里面的内容却不能执行 服务器还报错  You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
求各位大神帮忙看看 

------解决方案--------------------
year,month,date 这三个参数不为null可能为空,检查一下
------解决方案--------------------
加空格是一个问题,第一个if能执行那应该不是这个问题了,System.out.println(str)放在rs=stmt.executeQuery(str)的前面,这样打印出sql直接放到数据库软件运行就容易找问题了。
;
------解决方案--------------------
year和month两个参数中任意一个为空白字符,就会造成这个SQL语法错误,
------解决方案--------------------
==null和.equeals("")都要加判断