jsp&Servlet-单表信息操作

1.建立数据库中的book表

2.将book表中的属性封装在model文件夹下

package com.oracle.book.model;

import java.io.Serializable;
import java.util.Date;

@SuppressWarnings("serial")
public class Book  implements Serializable{
	private int bookid;
	private String bookname;
	private double bookprice;
	private String publishdate;//数据库是date则java类中也是 java,util.date
	private String bookauthor;
	private String bookpublisher;
	private String bookinfo;
	private String booktype;
	private String salelocation;
	public int getBookid() {
		return bookid;
	}
	public void setBookid(int bookid) {
		this.bookid = bookid;
	}
	public String getBookname() {
		if(bookname==null){
			return "无";
		}
		return bookname;
	}
	public void setBookname(String bookname) {
		this.bookname = bookname;
	}
	public double getBookprice() {
		return bookprice;
	}
	public void setBookprice(double bookprice) {
		this.bookprice = bookprice;
	}
	public String getPublishdate() {
		return publishdate;
	}
	public String getPublishdatetoString() {
		if(publishdate==null){
			return "无";
		}
		return publishdate.toString();
	}
	public void setPublishdate(String publishdate) {
		this.publishdate = publishdate;
	}
	public String getBookauthor() {
		if(bookauthor==null){
			return "无";
		}
		return bookauthor;
	}
	public void setBookauthor(String bookauthor) {
		this.bookauthor = bookauthor;
	}
	public String getBookpublisher() {
		if(bookauthor==null){
			return "无";
		}
		return bookpublisher;
	}
	public void setBookpublisher(String bookpublisher) {
		this.bookpublisher = bookpublisher;
	}
	public String getBookinfo() {
		if(bookinfo==null){
			return "无";
		}
		return bookinfo;
	}
	public void setBookinfo(String bookinfo) {
		this.bookinfo = bookinfo;
	}
	public String getBooktype() {
		if(booktype==null){
			return "无";
		}
		return booktype;
	}
	public void setBooktype(String booktype) {
		this.booktype = booktype;
	}
	public String getSalelocation() {
		if(salelocation==null){
			return "无";
		}
		return salelocation;
	}
	public void setSalelocation(String salelocation) {
		this.salelocation = salelocation;
	}
}

3.编写JDBCTemplate放在util文件夹下

package com.oracle.book.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCTemplate {

	private Connection conn = null;

	private PreparedStatement ps = null;

	private ResultSet rs = null;

	// 静态块 ---类加载时执行
	static {
		// 加载驱动
		try {
			Class.forName("org.gjt.mm.mysql.Driver").newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

	}

	public JDBCTemplate() {
		super();
		// 加载驱动

	}

	// 获取连接
	private void getConnection() {
		try {
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/book", "root", "oracle");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	// 创建ps对象
	private void createPreparedStatement(String sql) {
		this.getConnection();

		try {
			ps = conn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 执行查询的方法
	 * 
	 * @param sql
	 *            可带?号的查询sql语句
	 * @param args
	 *            给?号赋值数组
	 * @return rs对象
	 */
	public ResultSet executeQuery(String sql, String[] args) {

		this.createPreparedStatement(sql);

		try {
			// 给?号赋值
			for (int i = 0; args != null && i < args.length; i++) {
				ps.setString(i + 1, args[i]);
			}
			// 执行查询方法
			rs = ps.executeQuery();

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

	}

	/**
	 * 执行增删改的方法
	 * 
	 * @param sql
	 *            可带?号的增删改的sql语句
	 * @param args
	 *            给?号赋值数组
	 * @return 影响的行数
	 */
	public int executeUpdate(String sql, String[] args) {

		int rowCount = -1;

		this.createPreparedStatement(sql);

		try {
			// 给?号赋值
			for (int i = 0; args != null && i < args.length; i++) {
				ps.setString(i + 1, args[i]);
			}
			// 执行查询方法
			rowCount = ps.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.closeResource();
		}

		return rowCount;

	}

	/**
	 * 获取首行首列的值
	 * 
	 * @param sql 
	 * @return
	 */
	public String executeScalar(String sql) {

		String res = "";
		this.createPreparedStatement(sql);

		try {
			ResultSet rs = ps.executeQuery();

			if (rs.next()) {
				res = rs.getString(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.closeResource();
		}
		return res;
	}

	/**
	 * 关闭资源
	 */
	public void closeResource() {

		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
			if (ps != null) {
				ps.close();
				ps = null;
			}
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

4.编写BookDAO放在dao文件夹下

package com.oracle.book.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.oracle.book.model.Book;
/*
 * 查找所有的书本信息
 */
public class BookDAO  extends BaseDAO{
	public List<Book> finAll(int currentpage){
		List<Book> list=new ArrayList<Book>();
		String sql="select * from book limit "+(currentpage-1)*2+",2";
		ResultSet rs=this.getJDBC().executeQuery(sql, null);
		try {
			while(rs.next()){
				Book book=new Book();
				book.setBookid(rs.getInt(1));
				book.setBookname(rs.getString(2));
				book.setBookprice(rs.getDouble(3));
				book.setPublishdate(rs.getString(4));
				book.setBookauthor(rs.getString(5));
				book.setBookpublisher(rs.getString(6));
				book.setBookinfo(rs.getString(7));
				book.setBooktype(rs.getString(8));
				book.setSalelocation(rs.getString(9));
				list.add(book);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return list;
	}
	/*
	 * 根据bookid查询书本
	 */
	public Book findById(String bookid){
		Book book=null;
		String sql="select * from book where bookid="+bookid;
		ResultSet rs=this.getJDBC().executeQuery(sql, null);
		try {
			if(rs.next()){
				book=new Book();
				book.setBookid(rs.getInt(1));
				book.setBookname(rs.getString(2));
				book.setBookprice(rs.getDouble(3));
				book.setPublishdate(rs.getString(4));
				book.setBookauthor(rs.getString(5));
				book.setBookpublisher(rs.getString(6));
				book.setBookinfo(rs.getString(7));
				book.setBooktype(rs.getString(8));
				book.setSalelocation(rs.getString(9));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeResource();
		}
		return book;
	}
	public int findcount(){
		String sql="select count(*) from book";
		return Integer.parseInt(this.getJDBC().executeScalar(sql));
	}
	/* 
	 * 增加书本
	 */
	public int doAdd(Book book){
		String sql="insert into book values(null,?,?,?,?,?,?,?,?);";
		return this.getJDBC().executeUpdate(sql, new String[]{book.getBookname(),book.getBookprice()+"",book.getPublishdatetoString(),book.getBookauthor(),book.getBookpublisher(),book.getBookinfo(),book.getBooktype(),book.getSalelocation()});
	}
	/*
	 * 修改书本信息
	 */
	public int doupdate(Book book,String bookid){
		String sql="update book set bookname=?,bookprice=?,publishdate=?,bookauthor=?,bookpublisher=?,bookinfo=?,booktype=?,salelocation=? where bookid="+bookid;
		System.out.println(sql);
		return this.getJDBC().executeUpdate(sql, new String[]{book.getBookname(),book.getBookprice()+"",book.getPublishdatetoString(),book.getBookauthor(),book.getBookpublisher(),book.getBookinfo(),book.getBooktype(),book.getSalelocation()});
	}
	/*
	 * 删除书本
	 */
	public int dodelete(String bookid){
		String sql="delete from book where bookid="+bookid;
		return this.getJDBC().executeUpdate(sql, null);				
	}
}
5.设计添加书本页面(index.jsp)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>Bootstrap 基础 Template</title>
    <!-- Bootstrap -->
    <link href="bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-2.1.0.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="bootstrap/3.3.4/js/bootstrap.min.js"></script>
    
	<body>
		
	<div class="container">	
		
		<div class="jumbotron">
			<h2>当当网图书管理</h2>   
			<div> <%=request.getParameter("msg")==null?"":request.getParameter("msg")%> </div>
		</div>
		
		<div class="row">
			<div class="col-sm-4">
				<a href="index.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="添加图书" /></a>
				<a href="deletebook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="删除图书" /></a>
				<a href="updatebook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="修改图书" /></a>
				<a href="allbook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="查询图书" /></a>
			</div>
			<div class="col-sm-8">
				<!-- get和post是有区别的。
					什么区别:
						两点:1/get是以 ?  & 符号的形式将参数附加到地址中,提交。【数据是在请求头】
							 	get最多可以提交 255字符。2kb.. get方式的乱码,程序是不好解决的,需要
							 	修改容器【服务器】
							 2/post是提交的数据量理论上没有限制。文件上传必须用post
						
				 -->
				<form method="post" action="AddBook">
					<table class="table table-bordered table-striped">
						<tr>
							<td>图书名称</td>
							<td><input name="bname" class="form-control" type="text" /></td>
						</tr>
						<tr>
							<td>售价</td>
							<td><input name="bprice"  class="form-control" type="text" /></td>
						</tr>
						<tr>
							<td>出版时间</td>
							<td><input name="bpubdate" class="form-control" type="text" /></td>
						</tr>
						<tr>
							<td>作者</td>
							<td><input name="bauthor" class="form-control" type="text" /></td>
						</tr>
						<tr>
							<td>出版社</td>
							<td><input name="bpublisher"  class="form-control" type="text" /></td>
						</tr>
						<tr>
							<td>备注</td>
							<td><input name="binfo" class="form-control" type="text" /></td>
						</tr>
						<tr>
							<td>类别</td>
							<td><input name="btype" class="form-control" type="text" /></td>
						</tr>
						<tr>
							<td>销售区域</td>
							<td>
								<input name="bsaleloc" type="checkbox" value="hd">华东
								<input name="bsaleloc" type="checkbox" value="hb">华北
								<input name="bsaleloc" type="checkbox" value="hn">华南
								<input name="bsaleloc" type="checkbox" value="hz">华中
								<input name="bsaleloc" type="checkbox" value="hz">全国	
							</td>
						</tr>
						<tr>
							<td align="center" colspan="2">
								<input  class=" btn btn-block btn-danger btn-lg" type="submit" value="添加"> 
							</td>
						</tr>
					</table>
				</form>
				
			</div>
		</div>
		
	</div>
		
	</body>
</html>

6.设计删除书本页面(deletebook.jsp)

<%@page import="com.oracle.book.dao.BookDAO"%>
<%@page import="com.oracle.book.model.Book"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>Bootstrap 基础 Template</title>
    <!-- Bootstrap -->
    <link href="bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-2.1.0.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="bootstrap/3.3.4/js/bootstrap.min.js"></script>
    
    <%
    	//分页
    	int currentpage=1;
    	BookDAO bookdao=new BookDAO();
    	if(request.getParameter("currentpage")!=null){
    		currentpage=Integer.parseInt(request.getParameter("currentpage"));
    	}
    	//分页标准
    	int pagenorm=2;
    	//总记录数
    	int allrow=bookdao.findcount();
    	//总页数
    	int allpage=allrow % pagenorm>0?allrow/pagenorm+1:allrow/pagenorm;
    	//处理分页
    	if(allpage!=0&¤tpage>allpage){
    		currentpage=allpage;
    	}
    	if(currentpage<=1){
    		currentpage=1;
    	}
    	List<Book> books=bookdao.finAll(currentpage);
     %>
	<body>
		
	<div class="container">	
		
		<div class="jumbotron">
			<h2>当当网图书管理</h2>   
			<div> <%=request.getParameter("msg")==null?"":request.getParameter("msg")%> </div>
		</div>
		
		<div class="row">
			<div class="col-sm-4">
				<a href="index.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="添加图书" /></a>
				<a href="deletebook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="删除图书" /></a>
				<a href="updatebook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="修改图书" /></a>
				<a href="allbook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="查询图书" /></a>
			</div>
			<div class="col-sm-8">
				 <form action="DeleteBook" method="post">
					<table class="table table-bordered table-striped">
						<tr>
							<th>删除</th>
							<th>图书名称</th>
							<th>售价</th>
							<th>出版时间</th>
							<th>作者</th>
							<th>出版社</th>
							<th>备注</th>
							<th>类别</th>
							<th>销售区域</th>
						</tr>
						<%
							for(Book book:books){
							
						%>
						<tr>
							<td><input type="checkbox" name="bookid" value="<%=book.getBookid()%>"/></td>
							<td><input name="bname" class="form-control" type="text" value="<%=book.getBookname()%>"/></td>
							<td><input name="bprice"  class="form-control" type="text" value="<%=book.getBookprice()%>" /></td>
							<td><input name="bpubdate" class="form-control" type="text" value="<%=book.getPublishdatetoString()%>"/></td>
							<td><input name="bauthor" class="form-control" type="text" value="<%=book.getBookauthor()%>"/></td>
							<td><input name="bpublisher"  class="form-control" type="text" value="<%=book.getBookpublisher()%>"/></td>
							<td><input name="binfo" class="form-control" type="text" value="<%=book.getBookinfo()%>"/></td>
							<td><input name="btype" class="form-control" type="text" value="<%=book.getBooktype()%>"/></td>
							<td><input name="bsaleloc" class="form-control" type="text" value="<%=book.getSalelocation()%>"/></td>
						</tr>
						<% }%>
						<tr>
							<td align="center" colspan="9">
								<a href="deletebook.jsp?currentpage=<%=currentpage-1%>">上一页</a>
								<a href="deletebook.jsp?currentpage=<%=currentpage+1%>">下一页</a>
							</td>
						</tr>
						<tr>
							<td align="center" colspan="9">
								<input  class=" btn btn-block btn-danger btn-lg" type="submit" value="删除"> 
							</td>
						</tr>							
					</table>
				</form>
			</div>
		</div>
		
	</div>
	</body>
</html>

7.设计修改书本页面(updatebook.jsp)

<%@page import="com.oracle.book.dao.BookDAO"%>
<%@page import="com.oracle.book.model.Book"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>Bootstrap 基础 Template</title>
    <!-- Bootstrap -->
    <link href="bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-2.1.0.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="bootstrap/3.3.4/js/bootstrap.min.js"></script>
    
    <%
    	//分页
    	int currentpage=1;
    	BookDAO bookdao=new BookDAO();
    	if(request.getParameter("currentpage")!=null){
    		currentpage=Integer.parseInt(request.getParameter("currentpage"));
    	}
    	//分页标准
    	int pagenorm=2;
    	//总记录数
    	int allrow=bookdao.findcount();
    	//总页数
    	int allpage=allrow % pagenorm>0?allrow/pagenorm+1:allrow/pagenorm;
    	//处理分页
    	if(allpage!=0&¤tpage>allpage){
    		currentpage=allpage;
    	}
    	if(currentpage<=1){
    		currentpage=1;
    	}
    	List<Book> books=bookdao.finAll(currentpage);
     %>
	<body>
		
	<div class="container">	
		
		<div class="jumbotron">
			<h2>当当网图书管理</h2>   
			<div> <%=request.getParameter("msg")==null?"":request.getParameter("msg")%> </div>
		</div>
		
		<div class="row">
			<div class="col-sm-4">
				<a href="index.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="添加图书" /></a>
				<a href="deletebook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="删除图书" /></a>
				<a href="updatebook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="修改图书" /></a>
				<a href="allbook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="查询图书" /></a>
			</div>
			<div class="col-sm-8">
				 <form action="doupdate.jsp" method="post">
					<table class="table table-bordered table-striped">
						<tr>
							<th>修改</th>
							<th>图书名称</th>
							<th>售价</th>
							<th>出版时间</th>
							<th>作者</th>
							<th>出版社</th>
							<th>备注</th>
							<th>类别</th>
							<th>销售区域</th>
						</tr>
						<%
							for(Book book:books){
							
						%>
						<tr>
							<td><input type="checkbox" name="bookid" value="<%=book.getBookid()%>"/></td>
							<td><input name="bname" class="form-control" type="text" value="<%=book.getBookname()%>"/></td>
							<td><input name="bprice"  class="form-control" type="text" value="<%=book.getBookprice()%>" /></td>
							<td><input name="bpubdate" class="form-control" type="text" value="<%=book.getPublishdatetoString()%>"/></td>
							<td><input name="bauthor" class="form-control" type="text" value="<%=book.getBookauthor()%>"/></td>
							<td><input name="bpublisher"  class="form-control" type="text" value="<%=book.getBookpublisher()%>"/></td>
							<td><input name="binfo" class="form-control" type="text" value="<%=book.getBookinfo()%>"/></td>
							<td><input name="btype" class="form-control" type="text" value="<%=book.getBooktype()%>"/></td>
							<td><input name="bsaleloc" class="form-control" type="text" value="<%=book.getSalelocation()%>"/></td>
						</tr>
						<% }%>
						<tr>
							<td align="center" colspan="9">
								<a href="updatebook.jsp?currentpage=<%=currentpage-1%>">上一页</a>
								<a href="updatebook.jsp?currentpage=<%=currentpage+1%>">下一页</a>
							</td>
						</tr>
						<tr>
							<td align="center" colspan="9">
								<input  class=" btn btn-block btn-danger btn-lg" type="submit" value="修改"> 
							</td>
						</tr>						
					</table>
				</form>
			</div>
		</div>
		
	</div>
	</body>
</html>

8.设计修改书本页面(doupdate.jsp)

<%@page import="com.oracle.book.model.Book"%>
<%@page import="com.oracle.book.dao.BookDAO"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>Bootstrap 基础 Template</title>
    <!-- Bootstrap -->
    <link href="bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-2.1.0.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="bootstrap/3.3.4/js/bootstrap.min.js"></script>
    
	<body>
	<%
		BookDAO bookdao=new BookDAO();
		//获取提交信息
		String bookid=request.getParameter("bookid");
		Book book=bookdao.findById(bookid);
	 %>	
	<div class="container">	
		
		<div class="jumbotron">
			<h2>当当网图书管理</h2>   
			<div> <%=request.getParameter("msg")==null?"":request.getParameter("msg")%> </div>
		</div>
		
		<div class="row">
			<div class="col-sm-4">
				<a href="index.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="添加图书" /></a>
				<a href="deletebook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="删除图书" /></a>
				<a href="updatebook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="修改图书" /></a>
				<a href="allbook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="查询图书" /></a>
			</div>
			<div class="col-sm-8">
				<form method="post" action="UpdateBook">
					<input type="hidden" name="bookid" value="<%=bookid%>"/> 
					<table class="table table-bordered table-striped">
						<tr>
							<td>图书名称</td>
							<td><input name="bname" class="form-control" type="text" value="<%=book.getBookname()%>"/></td>
						</tr>
						<tr>
							<td>售价</td>		
							<td><input name="bprice"  class="form-control" type="text" value="<%=book.getBookprice()%>" /></td>
						</tr>
						<tr>
							<td>出版时间</td>
							<td><input name="bpubdate" class="form-control" type="text" value="<%=book.getPublishdatetoString()%>"/></td>
						</tr>
						<tr>
							<td>作者</td>
							<td><input name="bauthor" class="form-control" type="text" value="<%=book.getBookauthor()%>"/></td>
						</tr>
						<tr>
							<td>出版社</td>
							<td><input name="bpublisher"  class="form-control" type="text" value="<%=book.getBookpublisher()%>"/></td>
						</tr>
						<tr>
							<td>备注</td>
							<td><input name="binfo" class="form-control" type="text" value="<%=book.getBookinfo()%>"/></td>
						</tr>
						<tr>
							<td>类别</td>
							<td><input name="btype" class="form-control" type="text" value="<%=book.getBooktype()%>"/></td>
						<tr>
							<td>销售区域</td>
							<td><input name="bsaleloc" class="form-control" type="text" value="<%=book.getSalelocation()%>"/></td>
						</tr>
						<tr>
							<td align="center" colspan="2">
								<input  class=" btn btn-block btn-danger btn-lg" type="submit" value="修改"> 
							</td>
						</tr>
					</table>
				</form>
				
			</div>
		</div>
		
	</div>
		
	</body>
</html>
</html>

9.设计查询书本页面(allbook.jsp)

<%@page import="com.oracle.book.model.Book"%>
<%@page import="com.oracle.book.dao.BookDAO"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
    <title>Bootstrap 基础 Template</title>
    <!-- Bootstrap -->
    <link href="bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-2.1.0.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="bootstrap/3.3.4/js/bootstrap.min.js"></script>
    
    <%
    	//分页
    	int currentpage=1;
    	BookDAO bookdao=new BookDAO();
    	if(request.getParameter("currentpage")!=null){
    		currentpage=Integer.parseInt(request.getParameter("currentpage"));
    	}
    	//分页标准
    	int pagenorm=2;
    	//总记录数
    	int allrow=bookdao.findcount();
    	//总页数
    	int allpage=allrow % pagenorm>0?allrow/pagenorm+1:allrow/pagenorm;
    	//处理分页
    	if(allpage!=0&¤tpage>allpage){
    		currentpage=allpage;
    	}
    	if(currentpage<=1){
    		currentpage=1;
    	}
    	List<Book> books=bookdao.finAll(currentpage);
     %>
	<body>
		
	<div class="container">	
		
		<div class="jumbotron">
			<h2>当当网图书管理</h2>   
			<div> <%=request.getParameter("msg")==null?"":request.getParameter("msg")%> </div>
		</div>
		
		<div class="row">
			<div class="col-sm-4">
				<a href="index.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="添加图书" /></a>
				<a href="deletebook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="删除图书" /></a>
				<a href="updatebook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="修改图书" /></a>
				<a href="allbook.jsp"><input type="button" class="btn btn-lg btn-block btn-success" value="查询图书" /></a>
			</div>
			<div class="col-sm-8">
				<!-- get和post是有区别的。
					什么区别:
						两点:1/get是以 ?  & 符号的形式将参数附加到地址中,提交。【数据是在请求头】
							 	get最多可以提交 255字符。2kb.. get方式的乱码,程序是不好解决的,需要
							 	修改容器【服务器】
							 2/post是提交的数据量理论上没有限制。文件上传必须用post
						
				 -->
					<table class="table table-bordered table-striped">
						<tr>
							<th>图书名称</th>
							<th>售价</th>
							<th>出版时间</th>
							<th>作者</th>
							<th>出版社</th>
							<th>备注</th>
							<th>类别</th>
							<th>销售区域</th>
						</tr>
						<%
							for(Book book:books){
							
						%>
						<tr>
							<td><input name="bname" class="form-control" type="text" value="<%=book.getBookname()%>"/></td>
							<td><input name="bprice"  class="form-control" type="text" value="<%=book.getBookprice()%>" /></td>
							<td><input name="bpubdate" class="form-control" type="text" value="<%=book.getPublishdatetoString()%>"/></td>
							<td><input name="bauthor" class="form-control" type="text" value="<%=book.getBookauthor()%>"/></td>
							<td><input name="bpublisher"  class="form-control" type="text" value="<%=book.getBookpublisher()%>"/></td>
							<td><input name="binfo" class="form-control" type="text" value="<%=book.getBookinfo()%>"/></td>
							<td><input name="btype" class="form-control" type="text" value="<%=book.getBooktype()%>"/></td>
							<td><input name="bsaleloc" class="form-control" type="text" value="<%=book.getSalelocation()%>"/></td>
						</tr>
						<% }%>
						<tr>
						<td align="center" colspan="8">
						<a href="allbook.jsp?currentpage=<%=currentpage-1%>">上一页</a>
						<a href="allbook.jsp?currentpage=<%=currentpage+1%>">下一页</a>
						</td>
						</tr>						
					</table>
			</div>
		</div>
		
	</div>
		
	</body>
</html>
10.设计处理添加、删除、修改的Servlet类AddServlet、DeleteServlet、UpdateServlet类放在servlet文件夹下

package com.oracle.book.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oracle.book.dao.BookDAO;
import com.oracle.book.model.Book;

public class AddServlet extends HttpServlet {
	public AddServlet() {
		super();
	}
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

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

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置服务器编码格式
		request.setCharacterEncoding("utf-8");
		// 设置浏览器编码格式
		response.setCharacterEncoding("utf-8");
		// 告诉浏览器所用的文档类型和编码格式
		response.setContentType("text/html;charset=utf-8");
		// 获取数据
		String bookname = request.getParameter("bname");
		double bookprice = Double.parseDouble(request.getParameter("bprice"));
		String bookauthor = request.getParameter("bauthor");
		String bookpublisher = request.getParameter("bpublisher");
		String[] str = request.getParameterValues("bsaleloc");
		String salelocation = "";
		for (int i = 0; i < str.length; i++) {
			if(str[i].equals(str[str.length-1])){
				if (str[i].equals("hz")) {
					salelocation += "华中";
				} else if (str[i].equals("hd")) {
					salelocation += "华东";
				} else if (str[i].equals("hb")) {
					salelocation += "华北";
				} else if (str[i].equals("hn")) {
					salelocation += "华南";
				} else if (str[i].equals("all")) {
					salelocation += "全国";
				}
			}else{
				if (str[i].equals("hz")) {
					salelocation += "华中,";
				} else if (str[i].equals("hd")) {
					salelocation += "华东,";
				} else if (str[i].equals("hb")) {
					salelocation += "华北,";
				} else if (str[i].equals("hn")) {
					salelocation += "华南,";
				} else if (str[i].equals("all")) {
					salelocation += "全国";
				}
			}
		}
		String publishdate = request.getParameter("bpubdate");
		System.out.println(publishdate);
		String bookinfo = request.getParameter("binfo");
		String booktype = request.getParameter("btype");
		Book book = new Book();
		book.setBookname(bookname);
		book.setBookprice(bookprice);

		// string转换为date java
		SimpleDateFormat smf = new SimpleDateFormat("yyyy-MM-dd");
		Date d=null;
		try {
			d = smf.parse(publishdate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		book.setPublishdate(publishdate); 
		book.setBookauthor(bookauthor);
		book.setBookpublisher(bookpublisher);
		book.setBookinfo(bookinfo);
		book.setBooktype(booktype);
		book.setSalelocation(salelocation);
		BookDAO bookdao = new BookDAO();
		int count = bookdao.doAdd(book);
		if (count > 0) {
			PrintWriter out = response.getWriter();
			out.write("添加成功");
			request.getRequestDispatcher("index.jsp?msg=添加成功").forward(request,
					response);
		} else {
			request.getRequestDispatcher("error.jsp?errorinfo=添加失败").forward(
					request, response);
		}
	}
	public void init() throws ServletException {
		// Put your code here
	}

}

package com.oracle.book.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oracle.book.dao.BookDAO;

public class DeleteServlet extends HttpServlet {
	public DeleteServlet() {
		super();
	}
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//设置服务器编码格式
		request.setCharacterEncoding("utf-8");
		//设置浏览器编码格式
		response.setCharacterEncoding("utf-8");
		//告诉浏览器所用的文档类型和编码格式
		response.setContentType("text/html;charset=utf-8");
		//获取数据
		String bookid=request.getParameter("bookid");
		BookDAO bookdao=new BookDAO();
		int count=bookdao.dodelete(bookid);
		if(count>0){
			PrintWriter out=response.getWriter();
			out.write("修改成功");
			request.getRequestDispatcher("deletebook.jsp?msg=删除成功").forward(request, response);
		}else{
			request.getRequestDispatcher("error.jsp?errorinfo=删除失败").forward(request, response);
		}
	}
	public void init() throws ServletException {
		// Put your code here
	}

}

package com.oracle.book.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oracle.book.dao.BookDAO;
import com.oracle.book.model.Book;


public class UpdateServlet extends HttpServlet {
	public UpdateServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}


	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//设置服务器编码格式
		request.setCharacterEncoding("utf-8");
		//设置浏览器编码格式
		response.setCharacterEncoding("utf-8");
		//告诉浏览器所用的文档类型和编码格式
		response.setContentType("text/html;charset=utf-8");
		//获取数据
		String bookid=request.getParameter("bookid");
		String bookname=request.getParameter("bname");
		double bookprice=Double.parseDouble(request.getParameter("bprice"));
		String bookauthor=request.getParameter("bauthor");
		String bookpublisher=request.getParameter("bpublisher");
		String salelocation=request.getParameter("bsaleloc");
		String publishdate=request.getParameter("bpubdate");
		String bookinfo=request.getParameter("binfo");
		String booktype=request.getParameter("btype");
		Book book=new Book();
		book.setBookname(bookname);
		book.setBookprice(bookprice);
		book.setPublishdate("2010-12-12");
		book.setBookauthor(bookauthor);
		book.setBookpublisher(bookpublisher);
		book.setBookinfo(bookinfo);
		book.setBooktype(booktype);
		book.setSalelocation(salelocation);
		BookDAO bookdao=new BookDAO();
		int count=bookdao.doupdate(book,bookid);
		if(count>0){
			PrintWriter out=response.getWriter();
			out.write("修改成功");
			request.getRequestDispatcher("updatebook.jsp?msg=修改成功").forward(request, response);
		}else{
			request.getRequestDispatcher("error.jsp?errorinfo=修改失败").forward(request, response);
		}
	}

	public void init() throws ServletException {
		// Put your code here
	}


}
页面效果预览




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值