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
求各位大神帮忙看看 

解决方案 »

  1.   

    根据异常信息,你的sql语法有错误,你再检查一下。
      

  2.   

    year,month,date 这三个参数不为null可能为空,检查一下
      

  3.   

    String str="select * from affair where left(time,4)="+year+"and mid(time,6,2)="+month;and前没有空格,上面的语句也是。
      

  4.   

    加空格是一个问题,第一个if能执行那应该不是这个问题了,System.out.println(str)放在rs=stmt.executeQuery(str)的前面,这样打印出sql直接放到数据库软件运行就容易找问题了。
    ;
      

  5.   

    year和month两个参数中任意一个为空白字符,就会造成这个SQL语法错误,
      

  6.   

    回复于: 2013-07-10 21:51:45 根据异常信息,你的sql语法有错误,你再检查一下。 
      

  7.   

    把sql语句打印出来吧,看看sql语句里面有什么错误!
      

  8.   

    select * from affair where left(time,4)="+year+"空格and mid(time,6,2)="+空格month
      

  9.   

    我觉得楼主的sql中的 time,year都是变量把,变成?号会不会好点,然后再穿进去吧,不然time放到sql中代表会是变量吗?我觉得应该是个字符串吧,反正我没这么用过,呵呵,说的不对不要见笑