【问题标题】:Servlet,Jsp project MySQL Syntax ErrorServlet,Jsp 项目 MySQL 语法错误
【发布时间】:2016-06-12 14:34:20
【问题描述】:

我创建了项目servletjsp。我正在使用数据库phpmyadmin。我遇到了显示数据库产品的语法错误。在 Eclipse EE 控制台中返回以下错误。我没有解决问题。任何帮助将不胜感激。

 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'order
 (id,idUser,order,total) values (default,'1','flower','83.8883')' at
 line 1

Servlet 类:

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub


        String idUser =  req.getParameter("idUser");
        System.out.println(idUser);
        String order =  req.getParameter("order");
        System.out.println(order);
        String total =  req.getParameter("total");
        System.out.println(total);

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection cnx = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/shop","root","");
            PreparedStatement pr =  (PreparedStatement) cnx.prepareStatement(
                    "insert into order (id,idUser,order,total) values (default,?,?,?)"
                    );

               pr.setString(1,idUser);
               pr.setString(2,order );
               pr.setString(3, total);



               pr.executeUpdate();
               pr.close();

               resp.sendRedirect("Home");   


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

【问题讨论】:

标签: java mysql jsp


【解决方案1】:

从异常中我可以看到你的表名是order,这是MySql中的保留字。您需要修改查询才能使用它。

MySql 最简单的具体方法是用“`”转义它

create table `order` 
#... rest of the definition
insert into `order` values 
#... rest of insert statement

也有与数据库无关的方法。 更多信息请参考this answer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-01
    • 2014-06-09
    • 1970-01-01
    • 2017-05-29
    • 2014-05-11
    • 2011-03-21
    • 2010-12-04
    相关资源
    最近更新 更多