JAVAWEB---简单的图书管理系统
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JAVAWEB---简单的图书管理系统,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8172字,纯文字阅读大概需要12分钟。
内容图文
效果图
实现的功能:图书的增加,删除,修改。此处为了方便,数据库使用虚拟数据库,直接导入。
主要掌握的知识点:JDBC技术,前端与后端的逻辑,三层架构MVC的了解。
下面是代码详解:
前端:index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
//UTF-8编码格式
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书管理系统</title>
</head>
<body>
<!--页面跳转的作用,链接显示的是/V1/book.do-->
<jsp:forward page="/v1/book.do">
<jsp:param name="action" value="list"/>
</jsp:forward>
</body>
</html>
modify.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
pageContext.setAttribute("ctx", request.getContextPath());//获取项目路径
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改书本</title>
</head>
<body>
<h1 align="center">修改书本</h1>
<hr>
<br>
//表单提交方法:post,
<form method="post" action="${ctx}/v1/book.do?action=modify-submit">
<table width="400" border="1" align="center">
//隐藏书的id
<input type="hidden" name="id" value="${book.id }">
<tr>
<td>名称</td>
//正则表达式显示书名
<td><input type="text" name="name" value="${book.name }"></td>
</tr>
<tr>
<td>价格</td>
<td><input type="text" name="price" value="${book.price }"></td>
</tr>
</table>
<!-- 表单按钮 -->
<p align="center">
<input type="submit" value="确定" name="btOk">
</p>
</body>
</html>
listbook.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.util.*,com.model.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
pageContext.setAttribute("ctx", request.getContextPath());
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书管理系统</title>
</head>
<body>
<h1 align="center">图书管理系统</h1>
<table width="800" border="1" align="center">
<tr>
<td>id</td>
<td>书名</td>
<td>价格</td>
<td colspan="2">操作</td>
</tr>
<c:forEach items="${books}" var="book" varStatus="idx">
<tr>
<td>${book.id }</td>
<td>${book.name}</td>
<td>${book.price}</td>
//超链接,跳转到delete界面
<td><a href="${ctx}/v1/book.do?action=delete&id=${book.id }">删除</a></td>
<td><a href="${ctx}/v1/book.do?action=modify&id=${book.id }">修改</a></td>
</tr>
</c:forEach>
</table>
<br>
<p align="center">
<a href="./addbook.jsp">增加书本</a><br>
</p>
</body>
</html>
addbook.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
pageContext.setAttribute("ctx", request.getContextPath()); //获取项目路径
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加书本</title>
</head>
<body>
<h1 align="center">添加书本</h1>
<hr>
<br>
<form method="post" action="${ctx}/v1/book.do?action=add-submit">
<table width="400" border="1" align="center">
<tr>
<td>名称</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>价格</td>
<td><input type="text" name="price" /></td>
</tr>
</table>
<p align="center">
<input type="submit" value="确定" name="btOk">
</p>
</body>
</html>
后端的代码:
(模拟三层架构)
com.model
package com.model;
public class Book {
public int id;
public String name;
public double price;
public Book(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}}
package com.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.BookDao;
import com.model.Book;
public class BookController extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf8"); // 解决乱码问题
response.setCharacterEncoding("utf8");
String action = request.getParameter("action"); // 根据action转向不同的程序处理
if (action == null || "list".equals(action)) { // 列表显示
BookDao dao = new BookDao();
List books = dao.getBooks();//list列表存储
request.setAttribute("books", books);
RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/jsp/listbook.jsp");
view.forward(request, response);
} else if ("delete".equals(action)) { // 删除
int id = Integer.parseInt(request.getParameter("id"));
BookDao dao = new BookDao();
boolean success = dao.removeBook(id);
response.sendRedirect("/libmgr/v1/book.do");
} else if ("add-submit".equals(action)) { // 添加提交
String name = request.getParameter("name");
double price = Double.parseDouble(request.getParameter("price"));
BookDao dao = new BookDao();
boolean success = dao.addBook(name, price);
response.sendRedirect("/libmgr/v1/book.do");
} else if ("modify".equals(action)) { // 修改
int id = Integer.parseInt(request.getParameter("id"));
BookDao dao = new BookDao();
Book book = dao.getBookById(id);
request.setAttribute("book", book);
RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/jsp/modifybook.jsp");
view.forward(request, response);
} else if ("modify-submit".equals(action)) { // 修改提交
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
double price = Double.parseDouble(request.getParameter("price"));
Book book = new Book(id, name, price);
BookDao dao = new BookDao();
dao.modifyBook(book);
response.sendRedirect("/libmgr/v1/book.do");
}
}
}
package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.model.Book;
public class BookDao {
// String url = "jdbc:mysql://localhost:3306/booksdb?user=root&password=root&characterEncoding=utf8";
// String driver = "com.mysql.jdbc.Driver";
String driver = "org.sqlite.JDBC";
String path=this.getClass().getClassLoader().getResource("/").getPath();
String dbfile=path.substring(0,path.length()-9)+"/books.db";
String url = "jdbc:sqlite:"+dbfile;
public List getBooks() {
List list = new ArrayList();
// System.out.println(path);
// System.out.println(dbfile);
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM books");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
double price = rs.getDouble("price");
Book book = new Book(id, name, price);
list.add(book);
}
st.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public boolean removeBook(int id) {
boolean success = false;
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url);
Statement st = conn.createStatement();
success = st.execute("delete FROM books where id=" + id);
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return success;
}
public boolean addBook(String name, double price) {
boolean success = false;
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url);
Statement st = conn.createStatement();
success = st.execute("insert into books(name,price) values('" + name + "'," + price + ")");
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return success;
}
public Book getBookById(int id) {
Book book =null;
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM books where id="+id);
while (rs.next()) {
int bid = rs.getInt("id");
String name = rs.getString("name");
double price = rs.getDouble("price");
book = new Book(bid, name, price);
}
st.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return book;
}
public boolean modifyBook(Book book) {
boolean success = false;
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url);
PreparedStatement st = conn.prepareStatement("update books set name=? , price=? where id=?");
st.setString(1, book.name);
st.setDouble(2, book.price);
st.setInt(3, book.id);
success = st.execute();
st.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return success;
}
}
内容总结
以上是互联网集市为您收集整理的JAVAWEB---简单的图书管理系统全部内容,希望文章能够帮你解决JAVAWEB---简单的图书管理系统所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。