总得做点什么
2020-01-08 14:00
采纳率: 20%
浏览 278
已采纳

这个为什么没办法插入到数据库?

servlet里调用impl中的方法
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
//将订单写入数据库
int count = 1;
HttpSession ses = request.getSession();
List member = (List) ses.getAttribute("member");
HashMap shopCart = (HashMap) ses.getAttribute("SelectedGoods");
Order order = new Order();
for (HashMap.Entry entry : shopCart.entrySet()) {
for (int i = 0; i < member.size(); i++) {
order.setAddress(member.get(i).getAddress());
order.setMname(member.get(i).getName());
order.setTelephone(member.get(i).getTelephone());
}
order.setPname(entry.getValue().getName());
order.setPrice(entry.getValue().getPrice());
order.setNum(entry.getValue().getAmount());
order.setNote(entry.getValue().getNote());
order.setOrderid("o"+count);
count++;
try {
DAOFactory.getOrderDAOInstance().doCreate(order);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//跳转
request.getRequestDispatcher("psuccess.jsp").forward(request, response);
HttpSession session = request.getSession();
session.removeAttribute("SelectedGoods");

}

impl中的方法
public boolean doCreate(Order order)throws Exception{
    boolean flag = false;
    String sql = "INSERT INTO order(orderid,mname,address,telephone,pname,note,price,num)VALUES(?,?,?,?,?,?,?,?)";
    this.pstmt = this.conn.prepareStatement(sql);
    /*System.out.println(order.getOrderid());
    System.out.println(order.getMname());
    System.out.println(order.getAddress());
    System.out.println(order.getTelephone());
    System.out.println(order.getPname());
    System.out.println(order.getNote());
    System.out.println(order.getPrice());
    System.out.println(order.getNum());*/
    this.pstmt.setString(1,order.getOrderid());
    this.pstmt.setString(2,order.getMname());
    this.pstmt.setString(3,order.getAddress());
    this.pstmt.setString(4,order.getTelephone());
    this.pstmt.setString(5,order.getPname());
    this.pstmt.setString(6,order.getNote());
    this.pstmt.setDouble(7,order.getPrice());
    this.pstmt.setInt(8,order.getNum());
    if(this.pstmt.executeUpdate()>0){
        flag = true;
    }
    this.pstmt.close();
    return flag;
}

报错信息:
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(orderid,mname,address,telephone,pname,note,price,num)VALUES('o1','qwe','qw' at line 1
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dabocaiqq 2020-01-08 14:48
    已采纳
    order可以作为表名,但是要这么写
    INSERT INTO `order`(orderid,mname,address,telephone,pname,note,price,num)VALUES(?,?,?,?,?,?,?,?)
    
    打赏 评论
  • 老铁咖啡 2020-01-08 14:18

    order是mysql的关键字,不能使用order作为表名

    打赏 评论
  • 呓语的风 2020-01-08 14:47

    INSERT INTO `order`(orderid,mname,address,telephone,pname,note,price,num)VALUES(?,?,?,?,?,?,?,?)

    打赏 评论

相关推荐 更多相似问题