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