我创建了项目servlet
和jsp
。我正在使用数据库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();
}
}
答案 0 :(得分:1)
从异常中我可以看到你的表名是order
,它是MySql中的保留字。您需要修改查询才能使用它。
最简单的MySql特定方法是使用“`”
来逃避它create table `order`
#... rest of the definition
insert into `order` values
#... rest of insert statement
也有数据库无关的方法。 有关更多信息,请参阅this answer。