jdbc 修改数据



public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
    int id = Integer.valueOf(request.getParameter("id"));
  int bookCount = Integer.valueOf(request.getParameter("bookCount"));
  try {
   Class.forName("com.mysql.jdbc.Driver");
   String url = "jdbc:mysql://localhost:3306/sellbook";
      String username = "root";
   String password = "";
   Connection conn = DriverManager.getConnection(url,username,password);
      String sql = "update tb_book set bookcount=? where id=?";
   // 获取PreparedStatement
   PreparedStatement ps = conn.prepareStatement(sql);
   // 对SQL语句中的第一个参数赋值
   ps.setInt(1, bookCount);
   // 对SQL语句中的第二个参数赋值
   ps.setInt(2, id);
   ps.executeUpdate();
      ps.close();
      conn.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  // 重定向到FindServlet
  response.sendRedirect("FindServlet");
 } 
}

猜你喜欢

转载自joezheng123.iteye.com/blog/1871704