zxcl1
总得做点什么
采纳率20%
2020-01-08 14:00

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

已采纳

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 dabocaiqq 1年前
    order可以作为表名,但是要这么写
    INSERT INTO `order`(orderid,mname,address,telephone,pname,note,price,num)VALUES(?,?,?,?,?,?,?,?)
    
    点赞 3 评论 复制链接分享
  • zml_moxueli 呓语的风 1年前

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

    点赞 1 评论 复制链接分享
  • u013254183 老铁咖啡 1年前

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

    点赞 1 评论 复制链接分享

为你推荐