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

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<0){
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呈现展示页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="DAO.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>修改页面</title>
</head>

<body>


<form action="Updatebk" method="post">
<table align="center">
<tr align="center">
<td align="center">
<h2 align="center">修改图书信息</h2>
</td>
<tr align="center">
<td align="right"></td>
<td>
<input type="hidden" name="bid" value="${books.bid}"/>
</td>
</tr>
<tr align="center">
<td align="right">书名:</td>
<td>
<input type="text" name="bname" value="${books.bname}"/>
</td>
</tr>
<tr>
<td align="right">价格:</td>
<td>
<input type="text" name="price" value="${books.price}" />
</td>
</tr>
<tr>
<td align="right">数量:</td>
<td>
<input type="text" name="booksl" value="${books.booksl} "/>
</td>
</tr>
<tr align="center">
<td align="right">
<input type="submit" value="提交修改" />
<input type="reset" value="重 置" />
</td>
</tr>
</table>
</form>

</body>
</html>

上一篇:ASP.NET之Ajax系列(一)


下一篇:【php增删改查实例】第十一节 - 部门管理模块(编辑功能)