版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
首先附上效果图
增加
删除
更新
效果图展示完毕,下面我们来看代码
第一步建立MVC
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=DB";
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();
}
}
}
Library.java
package com.hnpi.bean;
public class Library {
/**
* ID
*/
private Integer id;
/**
* 书名
*/
private String bookname;
/**
* 作者
*/
private String writer;
/**
* 编号
*/
private String number;
/**
* 出版社
*/
private String press;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
public Library() {
super();
// TODO Auto-generated constructor stub
}
}
LibraryService.java
package com.hnpi.service;
import java.util.List;
import com.hnpi.bean.Library;
public interface LibraryService {
List<Library> list();
boolean delLibrary(Integer id);
boolean delLibrary(Library library);
boolean addLibrary(Library library);
boolean updateLibrary(Library library);
}
LibraryServiceImpl.java
package com.hnpi.service.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.hnpi.bean.Library;
import com.hnpi.service.LibraryService;
import com.hnpi.util.DBUtil;
public class LibraryServiceImpl implements LibraryService {
PreparedStatement ps = null;
ResultSet rs = null;
Connection conn = null;
public List<Library> list(){
List<Library> librarys=new ArrayList<Library>();
//从数据库获取
Connection conn = DBUtil.getConn();
String sql ="select * from librarys";
PreparedStatement ps = null;
ResultSet rs=null;
try{
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
Library library= new Library();
library.setId(rs.getInt(1));
library.setBookname(rs.getString(2));
library.setWriter(rs.getString(3));
library.setNumber(rs.getString(4));
library.setPress(rs.getString(5));
librarys.add(library);
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
DBUtil.closeConn(conn, ps, null);
}
return librarys;
}
public boolean delLibrary(Integer id) {
Connection conn=DBUtil.getConn();
String sql="delete from librarys where id =?";
PreparedStatement ps=null;
int count=0;
try{
ps=conn.prepareStatement(sql);
ps.setInt(1, id);
count =ps.executeUpdate();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
DBUtil.closeConn(conn, ps, null);
}
if(count >0)
return true;
else
return false;
}
public boolean delLibrary(Library library) {
// TODO Auto-generated method stub
Connection conn=DBUtil.getConn();
String sql="delete from librarys where id=?";
PreparedStatement ps=null;
int count=0;
try{
ps=conn.prepareStatement(sql);
ps.setInt(1, library.getId());
count=ps.executeUpdate();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
DBUtil.closeConn(conn, ps, null);
}
if(count>0)
return true;
else
return false;
}
public boolean updateLibrary(Library library){
try{
Connection conn=DBUtil.getConn();
String sql = "update librarys set id=?,bookname=?,writer=?,phone=?,press=? where id = ' " +library.getId()+ " ' ";
ps=conn.prepareStatement(sql);
ps.setInt(1, library.getId());
ps.setString(2, library.getBookname());
ps.setString(3, library.getWriter());
ps.setString(4, library.getNumber());
ps.setString(5, library.getPress());
int result=ps.executeUpdate();
if(result>0){
return true;
}else{
return false;
}
}catch (SQLException sqlException) {
// TODO: handle exception
sqlException.printStackTrace();
return false;
}finally{
DBUtil.closeConn(conn, ps, null);
}
}
public boolean addLibrary(Library library){
try{
Connection conn = DBUtil.getConn();
String sql="insert into librarys(id,bookname,writer,phone,press) values(?,?,?,?,?)";
ps=conn.prepareStatement(sql);
ps.setInt(1, library.getId());
ps.setString(2, library.getBookname());
ps.setString(3, library.getWriter());
ps.setString(4, library.getNumber());
ps.setString(5, library.getPress());
int result =ps.executeUpdate();
if(result>0){
return true;
}else{
return false;
}
}catch (SQLException sqlException) {
// TODO: handle exception
sqlException.printStackTrace();
return false;
}finally{
DBUtil.closeConn(conn, ps, null);
}
}
}
LibraryAddServlet.java
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.Library;
import com.hnpi.service.LibraryService;
import com.hnpi.service.impl.LibraryServiceImpl;
public class LibraryAddServlet 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");
response.setContentType("text/html");
LibraryService libraryService=new LibraryServiceImpl();
String id = request.getParameter("ID");
String bookname = request.getParameter("name");
String writer = request.getParameter("write");
String number = request.getParameter("number");
String press = request.getParameter("press");
Library library= new Library();
library.setId(Integer.parseInt(id));
library.setBookname(bookname);
library.setWriter(writer);
library.setNumber(number);
library.setPress(press);
if(libraryService.addLibrary(library)){
response.sendRedirect("libraryList");
}else{
response.sendRedirect("add.jsp");
}
}
}
LibraryDelServlet.java
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.Library;
import com.hnpi.service.LibraryService;
import com.hnpi.service.impl.LibraryServiceImpl;
public class LibraryDelServlet 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 {
//删除
//获取索要的ID
String idStr =request.getParameter("id");
//根据ID删除数据
LibraryService libraryService=new LibraryServiceImpl();
Library library = new Library();
library.setId(Integer.parseInt(idStr));
if(libraryService.delLibrary(library)){
response.sendRedirect("libraryList");
}else{
response.sendRedirect("libraryList");
}
}
}
LibraryListServlet.java
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.Library;
import com.hnpi.service.LibraryService;
import com.hnpi.service.impl.LibraryServiceImpl;
public class LibraryListServlet 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 {
LibraryService libraryService= new LibraryServiceImpl();
List<Library> librarys=libraryService.list();
HttpSession session =request.getSession();
session.setAttribute("libraryList", librarys);
request.getRequestDispatcher("libraryList.jsp").forward(request, response);
}
}
LibraryUpdateServlet.java
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.Library;
import com.hnpi.service.LibraryService;
import com.hnpi.service.impl.LibraryServiceImpl;
public class LibraryUpdateServlet 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");
response.setContentType("text/html");
LibraryService libraryService=new LibraryServiceImpl();
String id = request.getParameter("ID");
String bookname = request.getParameter("name");
String writer = request.getParameter("write");
String number = request.getParameter("number");
String press = request.getParameter("press");
Library library= new Library();
library.setId(Integer.parseInt(id));
library.setBookname(bookname);
library.setWriter(writer);
library.setNumber(number);
library.setPress(press);
if(libraryService.updateLibrary(library)){
response.sendRedirect("libraryList");
}else{
response.sendRedirect("update.jsp");
}
}
}
classifybook.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 'classifybook.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>
<table border="1" cellspacing="0" style="width: 800px" >
<tr>
<th>ID</th>
<th>name</th>
<th>操作</th>
</tr>
<c:forEach items="${sessionScope.libraryList}" var="library"
varStatus="status">
<tr>
<th>${library.id}</th>
<th>${library.bookname}</th>
<th><a href="libraryDel?id=${library.id}">删除</a>
<a href="update.jsp?id=${library.id }">更新</a></th>
</c:forEach>
</tr>
</table>
</body>
</html>
add.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 'MyJsp.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">
书ID <input name="ID" type="text"/><br/>
书名 <input name="name" type="text"/><br/>
作者 <input name="write" type="text"/><br/>
编号 <input name="number" type="text"/><br/>
出版社 <input name="press" type="text"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>