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日

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”