m0_60867154 2022-06-13 10:03 采纳率: 41.2%
浏览 52
已结题

javaweb项目,在servlet中调用dal层的insert语句,把信息插入到数据库中,有时候可以插入有时不可以的原因

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;
    }

是因为设置了会话时间的原因吗?

  • 写回答

3条回答 默认 最新

  • sum墨 2022-06-13 10:41
    关注

    你这个问题比较难看出来,需要调试,如果可以的话,sixin帮你看看

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月21日
  • 已采纳回答 6月13日
  • 创建了问题 6月13日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效