日期:2014-05-18  浏览次数:20630 次

请帮我看看语法上有什么问题吗?奇怪?
进行对数据库的删除操作,代码如下:
String   sql= "DELETE   *   FROM   usercustomer   WHERE   customerid= "+request.getParameter( "customerid ")+ ",ascription= ' "+(String)session.getAttribute( "userid ")+ " ' ";
stmt.executeUpdate(sql);

但提示我错误:
javax.servlet.ServletException:   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   '*   FROM   usercustomer   WHERE   customerid=2,ascription= 'abcd ' '   at   line   1


------解决方案--------------------
DELETE FROM .........

不要*
------解决方案--------------------
1)去掉*;
2)加上“and”在Where条件里;
3)Sql中的变量取出来,看的清晰。
String customerid = request.getParameter( "customerid ");
String ascription = (String)session.getAttribute( "userid ");
String sql=
"DELETE FROM usercustomer "+
"WHERE customerid= "+customerid+
"and ascription= "+ascription;
stmt.executeUpdate(sql);
------解决方案--------------------
这样才是正确的:

String sql= "DELETE FROM usercustomer WHERE customerid= "+request.getParameter( "customerid ")+ "and ascription= ' "+(String)session.getAttribute( "userid ")+ " ' ";
stmt.executeUpdate(sql);

不能要*
也不能要,应该把,改为 AND

------解决方案--------------------
(String)session.getAttribute( "userid ")应该改为:

((String)session.getAttribute( "userid ")).replaceAll( " ' ", " ' ' ")