javaweb项目,在servlet中调用dal层的insert语句,把信息插入到数据库中,有时候可以插入有时不可以的原因
基于mysql+eclipse+jsp
servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String number;
HttpSession session=request.getSession();
String accountname=(String) session.getAttribute("account");
number=request.getParameter("ISBN");
ProductDal p=new ProductDal();
Product ps=p.getByISBN(number);
Shopcar shopcar=new Shopcar();//model对象
//封装数据
shopcar.setNumber(number);
System.out.println(number);
shopcar.setName(ps.getPname());
System.out.println(ps.getPname());
shopcar.setPrice(ps.getPrice());
System.out.println(ps.getPrice());
shopcar.setAmount( ps.getCount1());
System.out.println(ps.getCount1());
shopcar.setAccountname(accountname);
System.out.println(accountname);
//调用DAL,将数据添加到数据库
ShopcarDal dal=new ShopcarDal();
int count=dal.add(shopcar);
//response.getWriter().print(count);
System.out.println(count);
response.sendRedirect(request.getContextPath()+ "/shopcar/shopcar");
}
dal
public int add(Shopcar shopcar){
try {
conn=util.getConnection();
String sql="insert into shopcar(number,name,price,amount,accountname) value(?,?,?,?,? )";
//4.执行sql语句
//4.1定义传令兵
//Statement st=(Statement) conn.createStatement();
PreparedStatement pst= conn.prepareStatement(sql);
pst.setString(1, shopcar.getNumber());
pst.setString(2, shopcar.getName());
pst.setInt(3, shopcar.getPrice());//4.2执行指令
pst.setInt(4,shopcar.getAmount());
pst.setString(5,shopcar.getAccountname());
//st.execute(sql);
int count= pst.executeUpdate();//增删改、数据表基本结构(create、drop、alter等)
// pst.executeUpdate();//增删改
//4.3释放传令兵
//st.close();
pst.close();
conn=null;
return count;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
是因为设置了会话时间的原因吗?