servlet+jsp update修改页面的实现,整整搞了两个小时才搞定

2021-07-02 06:08

阅读:699

YPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

标签:utf-8   next   pack   public   pos   自动   post   path   更新   

package DAO;

public class books {
private int bid;
private String bname;
private int booksl;
private double price;

public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public int getBooksl() {
return booksl;
}
public void setBooksl(int booksl) {
this.booksl = booksl;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}


}

 

package Util;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.DBManager;
public class Updatebk extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();

String bid = request.getParameter("bid");
String bname = request.getParameter("bname");
String booksl = request.getParameter("booksl");
String price = request.getParameter("price");

Connection conn = null;
try {
conn=DBManager.getDBManager().getConnection();
PreparedStatement pst = conn.prepareStatement("update books set bname=?,price=?,booksl=? where bid=?");

pst.setString(1, bname);
pst.setString(2, price);
pst.setString(3, booksl);
pst.setString(4, bid);//根据ID获取值,并且ID只能在最后面赋值
int x =pst.executeUpdate();
pst.close();
conn.close();
if(x>0){
out.print("更新成功");
}else if(x out.print("更新失败");
throw new Exception();
}

} catch (Exception e) {
e.printStackTrace();
}

}

}

以下这个是转发页面同时是jsp页面字段自动获取数据库默认值页面,没有这个页面jsp页面input value拿不到值

package vive;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.DBManager;
import DAO.books;

public class Updatebkpage extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String bid=request.getParameter("bid");

Connection conn = null;
try {
conn=DBManager.getDBManager().getConnection();
PreparedStatement pst = conn.prepareStatement("select * from books where bid=?");
pst.setString(1, bid);
ResultSet rs =pst.executeQuery();
books s = new books();
if(rs.next()){
s.setBid(rs.getInt("bid"));
s.setBname(rs.getString("bname"));
s.setPrice(rs.getDouble("price"));
s.setBooksl(rs.getInt("booksl"));
}
pst.close();
conn.close();
request.setAttribute("books", s);
request.getRequestDispatcher("WEB-INF/page/Updatebk.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}

}

}

jsp呈现展示页面




String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>




修改页面


























修改图书信息




书名:

价格:

数量:






servlet+jsp update修改页面的实现,整整搞了两个小时才搞定

标签:utf-8   next   pack   public   pos   自动   post   path   更新   

原文地址:http://www.cnblogs.com/gzwxx505094038/p/7128818.html


评论


亲,登录后才可以留言!