急!!用if 进行条件判断,里面有sql语句 报错

lanfangyi 2013-07-10 08:08:24
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
求各位大神帮忙看看
...全文
482 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyx100 2013-07-11
  • 打赏
  • 举报
回复
回复于: 2013-07-10 21:51:45 根据异常信息,你的sql语法有错误,你再检查一下。
zmjfanfan 2013-07-11
  • 打赏
  • 举报
回复
==null和.equeals("")都要加判断
sunaer 2013-07-11
  • 打赏
  • 举报
回复
year和month两个参数中任意一个为空白字符,就会造成这个SQL语法错误,
WSM6591289 2013-07-11
  • 打赏
  • 举报
回复
加空格是一个问题,第一个if能执行那应该不是这个问题了,System.out.println(str)放在rs=stmt.executeQuery(str)的前面,这样打印出sql直接放到数据库软件运行就容易找问题了。 ;
  • 打赏
  • 举报
回复
String str="select * from affair where left(time,4)="+year+"and mid(time,6,2)="+month; and前没有空格,上面的语句也是。
栖凤之梧 2013-07-11
  • 打赏
  • 举报
回复
year,month,date 这三个参数不为null可能为空,检查一下
坚持2012 2013-07-11
  • 打赏
  • 举报
回复
我觉得楼主的sql中的 time,year都是变量把,变成?号会不会好点,然后再穿进去吧,不然time放到sql中代表会是变量吗?我觉得应该是个字符串吧,反正我没这么用过,呵呵,说的不对不要见笑
tony4geek 2013-07-11
  • 打赏
  • 举报
回复
select * from affair where left(time,4)="+year+"空格and mid(time,6,2)="+空格month
S117 2013-07-11
  • 打赏
  • 举报
回复
把sql语句打印出来吧,看看sql语句里面有什么错误!
最美的词 2013-07-10
  • 打赏
  • 举报
回复
根据异常信息,你的sql语法有错误,你再检查一下。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧