图书管理系统增删改查操作

用的是MVC模式写的代码
在这里插入图片描述
bean下的Book.java

package com.hnpi.bean;

public class Book {

	private int id;
	private String name;
	private String auto;
	private String num;
	private String press;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuto() {
		return auto;
	}
	public void setAuto(String auto) {
		this.auto = auto;
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public String getPress() {
		return press;
	}
	public void setPress(String press) {
		this.press = press;
	}
	
	
	public Book() {
		super();
		// TODO Auto-generated constructor stub
	}
}

service包下的BookService.java接口

package com.hnpi.service;

import java.util.List;

import com.hnpi.bean.Book;

public interface BookService {
	List<Book> list();
	
	boolean	delBook(Integer id);
	
	
	boolean addBook(Book add);
	
	boolean updateBook(Book update);
	
}

serviceImpl包下的接口实现类BookServiceImpl.java

package com.hnpi.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.hnpi.bean.Book;
import com.hnpi.service.BookService;
import com.hnpi.util.DBUtil;

public class BookServiceImpl implements BookService {

	public List<Book> list() {
		List<Book> books = new ArrayList<Book>();
		
		//链接数据库
		Connection conn = DBUtil.getConn();
		String sql = "select * from book";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Book book = new Book();
				book.setId(rs.getInt(1));
				book.setName(rs.getString(2));
				book.setAuto(rs.getString(3));
				book.setNum(rs.getString(4));
				book.setPress(rs.getString(5));
				
				books.add(book);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeConn(conn, ps, rs);
		}

		//返回值
		return books;
		

	}

	//删除
	public boolean delBook(Integer id) {
		//链接数据库
		Connection conn = DBUtil.getConn();
		String sql = "delete from book where id = ?";
		PreparedStatement ps = null;
		int count = 0;
		try {
			ps = conn.prepareStatement(sql);

			ps.setInt(1, id);
			count = ps.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeConn(conn, ps, null);
		}

		//判断是否删除成功
		if (count > 0)
			return true;
		else
			return false;
	}

	

	//添加
	public boolean addBook(Book add) {
		
		//链接数据库
		Connection conn = DBUtil.getConn();
    	String sql="insert into book(name,auto,num,press)values(?,?,?,?)";
    	PreparedStatement ps = null;
 		int count = 0;
   
    	try {
			ps = conn.prepareStatement(sql);

			//获取数据(从addservlet穿过来的)
			ps.setString(1, add.getName());
			ps.setString(2, add.getAuto());
			ps.setString(3, add.getNum());
			ps.setString(4, add.getPress());
			count = ps.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeConn(conn, ps, null);
		}

		//判断是否增加成功
		if (count > 0)
			return true;
		else
			return false;
	}

	//更新
	public boolean updateBook(Book update) {
		//链接数据库
		Connection conn = DBUtil.getConn();
		String sql="update book set name=?,auto=?,num=?,press=? where id=?";
		PreparedStatement ps = null;
		int count = 0;
		
		try {
			ps = conn.prepareStatement(sql);

			//获取数据()
			ps.setInt(5, update.getId());
			
			ps.setString(1, update.getName());
			ps.setString(2, update.getAuto());
			ps.setString(3, update.getNum());
			ps.setString(4, update.getPress());
			count = ps.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeConn(conn, ps, null);
		}
		//判断是否更新成功
		if (count > 0)
			return true;
		else
			return false;
		
	}
}

servlet包
1.BookAddServlet

package com.hnpi.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.hnpi.bean.Book;
import com.hnpi.service.BookService;
import com.hnpi.service.impl.BookServiceImpl;

public class BookAddServlet extends HttpServlet {


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

		doPost(request, response);
	}


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

		//获取增加页面传送过来的数据
		String name = request.getParameter("name");
		String auto = request.getParameter("auto");
		String num = request.getParameter("num");
		String press = request.getParameter("press");
		
		BookService bookService = new BookServiceImpl();
		
		//把获取到的数据存入add中
		Book add = new Book();
		add.setName(name);
		add.setAuto(auto);
		add.setNum(num);
		add.setPress(press);
		
		//判断增加(使用方法)
		if(bookService.addBook(add)){
			response.sendRedirect("list");
		}else{
			response.sendRedirect("list");
		}
		
	}
}

2.BookDelServlet

package com.hnpi.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.hnpi.bean.Book;
import com.hnpi.service.BookService;
import com.hnpi.service.impl.BookServiceImpl;

public class BookDelServlet extends HttpServlet {

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

		doPost(request, response);
	}


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

		String id = request.getParameter("id");
		
		BookService bookService = new BookServiceImpl();
		
		
		if (bookService.delBook(Integer.parseInt(id))) {
			// 成功!
			response.sendRedirect("list");
		} else {
			// 失败!
			response.sendRedirect("list");
		}
		
	}


}

3.BookListServlet

package com.hnpi.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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

import com.hnpi.bean.Book;
import com.hnpi.service.BookService;
import com.hnpi.service.impl.BookServiceImpl;

public class BookListServlet extends HttpServlet {

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

		doPost(request, response);
	}


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

		BookService bookService = new BookServiceImpl();
		
		List<Book> books = bookService.list();
		
		
		HttpSession session = request.getSession();
		session.setAttribute("bookList", books);
		request.getRequestDispatcher("index.jsp").forward(request, response);
		
	}



}

4.BookUpdateServlet

package com.hnpi.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.hnpi.bean.Book;
import com.hnpi.service.BookService;
import com.hnpi.service.impl.BookServiceImpl;

public class BookUpdateServlet extends HttpServlet {


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

		doPost(request, response);
	}


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

		//转化为中文编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		//获取更新页面传过来的内容
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String auto = request.getParameter("auto");
		String num = request.getParameter("num");
		String press = request.getParameter("press");
		
		BookService bookServlce = new BookServiceImpl();
		
		//把穿过来的数据放在update中
		Book update = new Book();
		
		update.setId(Integer.parseInt(id));
		update.setName(name);
		update.setAuto(auto);
		update.setNum(num);
		update.setPress(press);
		
		//更新的方法判断
		if(bookServlce.updateBook(update)){
			response.sendRedirect("list");
		}else{
			response.sendRedirect("list");
		}
		
		
	}


}

5.TypeListServlet

package com.hnpi.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

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

import com.hnpi.bean.Book;
import com.hnpi.service.BookService;
import com.hnpi.service.impl.BookServiceImpl;

public class TypeListServlet extends HttpServlet {


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

		doPost(request, response);
	}


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

		BookService bookService = new BookServiceImpl();
		
		List<Book> books = bookService.list();
		
		
		HttpSession session = request.getSession();
		session.setAttribute("bookList", books);
		request.getRequestDispatcher("typeBook.jsp").forward(request, response);
		
		
	}
}

util包下的工具类DBUtil.java

package com.hnpi.util;

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

public class DBUtil {

	public static Connection getConn() {

		String url = "jdbc:sqlserver://localhost:1433;databaseName=Library";
		String user = "sa";
		String pwd = "1";
		Connection conn = null;

		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			conn = DriverManager.getConnection(url, user, pwd);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}

	public static void closeConn(Connection conn, PreparedStatement ps,
			ResultSet rs) {

		try {
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}

		try {
			if (ps != null) {
				ps.close();
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}

		try {
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}

	}

}

jsp
1.addBook.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>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'addBook.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form action="add" method="post">
    	<tr>名称:<input type="text" name="name"/></tr><br><br>
    	<tr>作者:<input type="text" name="auto"/></tr><br><br>
    	<tr>编号:<input type="text" name="num"/></tr><br><br>
    	<tr>出版社:<input type="text" name="press"/></tr><br><br>
    	<tr><input type="submit" value="添加"/></tr>
    </form>
  </body>
</html>

2.index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  
    <c:forEach items="${sessionScope.bookList}" var="book" varStatus="status">
    	${book.id} ${book.name} ${book.auto} ${book.num} ${book.press}
    	
    	<a href="delete?id=${book.id}" style="width: 80" onclick="javascript:delcfm()">删除</a>
    	<a href="updateBook.jsp?id=${book.id}">更新</a><br />
		</c:forEach>
		
		<hr>
		<a href="addBook.jsp">添加用户</a><br>
		<a href="typeBook.jsp">点击查看图书分类</a>
		
		
		<!-- 删除提示框 -->
		<script type="text/javascript" language="javascript">
    	function delcfm(){
    		if(!confirm("确认要删除吗")){
    			window.event.returnValue = false;
    		}
    	}
    </script>
		
  </body>
</html>

3.updateBook.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.hnpi.util.DBUtil"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%
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>
    <base href="<%=basePath%>">
    
    <title>My JSP 'updateBook.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   
    <%
    	String id = request.getParameter("id");
    	
    	
    	Connection conn = DBUtil.getConn();
    	String sql = "select * from book where id = ?";
    	session.setAttribute("id", id);
		PreparedStatement ps = null;
		ResultSet rs = null;
    	try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			rs = ps.executeQuery();
			while (rs.next()) {
			String name=rs.getString("name");
    		String auto = rs.getString("auto");
    		String num = rs.getString("num");
    		String press = rs.getString("press");
    		
    		%>
    		
    		 此用户信息为:<br><br>
     		<form action="update" method="post">
     			<table>
     				<tr><td>ID:</td><td><input type="text" name="id" value=<%=id%>></td></tr>
     				<tr><td>名称:</td><td><input type="text" name="name" value=<%=name%>></td></tr>
     				<tr><td>作者:</td><td><input type="text" name="auto" value=<%=auto%>></td></tr>
     				<tr><td>编号:</td><td><input type="text" name="num" value=<%=num%>></td></tr>
     				<tr><td>出版社:</td><td><input type="text" name="press" value=<%=press%>></td></tr>
					<tr><td><input type="submit" value="确认"></td>
    				<td><input type="reset" value="取消"></td></tr>
     			</table>
     		</form>
    		
    		
			<%	
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.closeConn(conn, ps, null);
		}
    	
    	
     %>
  </body>
</html>

4.typeBook.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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>
    <base href="<%=basePath%>">
    
    <title>My JSP 'typeBook.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    
     <c:forEach items="${sessionScope.bookList}" var="book" varStatus="status">
    	${book.id} ${book.name}
    	<a href="delete?id=${book.id}" style="width: 80" onclick="javascript:delcfm()">删除</a>
    	<a href="updateBook.jsp?id=${book.id}">更新</a><br />
		</c:forEach>
    
    <!-- 删除提示框 -->
    <script type="text/javascript" language="javascript">
    	function delcfm(){
    		if(!confirm("确认要删除吗")){
    			window.event.returnValue = false;
    		}
    	}
    </script>
    
    
  </body>
</html>

喜欢的可以自己复制代码运行一下,这里就不展示效果图了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值