关于与需要的文件下载
黑色 表示正文
Java-Project文件夹中存放项目,需要的同学可以下载看一下,也欢迎进行修改!
Java-Jar文件夹中存放.java或其他类型需要引入的外部文件
jar文件里面有不需要的可以不下载 本文档只用到了mysql connector
sqlserver与oracle数据库请下载sqljdbc4/ojdbc14并修改数据库连接语句
(连接类:src – Jdbc – Tool – BookManageOper 修改时仅需修改常量值)
一.所有页面与项目介绍
-
首页面
-
添加页面
所有代码只是为了做出来作业而写的
二.所用软件与文件
软件
JAR文件
注意!jar文件需要在File-Project Structure-Artifacts中引入到WEB-INF/lib中
三.数据库的建立
项目/数据库/Jar文件下载中 Java – 数据库 – BookManage.sql 进入SQLyog执行即可
四.导入Jar文件
在idea2018中 导入jar文件 其他版本或软件请自行寻找教程
五.代码编写
(1)src–jdbc包含的文件与其详解
src – Jdbc包含Dao(数据访问接口) , Impl(Dao实现类) , Entity(实体类) , Tool(工具类)
Dao(数据访问接口):包含BookDao与TypeDao 主要用来进行方法的定义
Impl(Dao实现类):包含BookDaoImpl与TypeDaoImpl,他们都继承自Dao中的接口,主要进行抽象方法的具体实现
Entity(实体类):包含Book与Type,也就是数据库中的表,其中的字段就是数据库的字段
Tool(工具类):包含BookManageOper与Page,用于数据库的连接开启与关闭和分页的操作
(2)src – jdbc – Tool
(2.1)src – jdbc – Tool – BookManageOper[数据库连接][工具类]
package Jdbc.Tool;
import java.io.Console;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class BookManageOper {
//创建数据与数据库的连接的字符常量 修改数据库仅需修改url中的bookmanage即可
//connector8.x中已经进行修改classname与url,详细请前往其他教程寻找
private static final String classname="com.mysql.jdbc.Driver";
//mysql5.x不需要?后面的内容 但我是8.x并且用的时connector5 所以需要加?后的内容
private static final String url="jdbc:mysql://localhost:3306/bookmanage?useUnicode=true&characterEncoding=utf8";
private static final String user="root";
private static final String pwd="123456";
//创建与数据库的连接 返回Connection连接对象
public static Connection create(){
//首先创建一个Connection对象 并赋值为null
Connection connection=null;
try{
//为Connection对象赋值(获取连接)
Class.forName(classname);
connection=DriverManager.getConnection(url,user,pwd);
}catch (Exception ex){
//当发生错误时在控制台(Console)中输出错误信息
//并显示Tool - BookManageOper - create() Error
ex.printStackTrace();
System.out.println("Tool - BookManageOper - create() Error");
}
//返回connection对象
return connection;
}
//通过三个对象关闭数据库的连接
public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){
try{
//如果不为空则进行关闭 为空则跳过
if(connection!=null){
connection.close();
}else if(preparedStatement!=null){
preparedStatement.close();
}else if(resultSet!=null){
resultSet.close();
}
}catch (Exception ex){
ex.printStackTrace();
System.out.println("Tool - BookManageOper - close(C,P,R) Error");
}
}
}
(2.2)src – jdbc – Tool – Page[翻页][工具类]
package Jdbc.Tool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Page {
//当前页数 默认为1
private int thisPage=1;
//总页数
private int sumPage;
//每页显示行数{固定为5 修改的时候修改5为x即可}
private int showPage=5;
//所有记录数 可以使用这个来进行代替sumpage? 【未实现】
private int sumRecord;
//单例模式
private Page(){
}
private static Page page=null;
public static Page getPage(){
if(page==null)
page=new Page();
return page;
}
//私有的字段 用于数据库连接与操作
private Connection connection=null;
private PreparedStatement preparedStatement=null;
private ResultSet resultSet=null;
//在这里我没有给sumRecord创建get/set方法 因为没有外部类使用到他
//如果有使用的需求可以加上 没有需求可以把sumRecord直接删除 在下方sum定义局部变量
public int getThisPage() {
return thisPage;
}
//当设置当前页数时,进行判断
public void setThisPage(int thisPage) {
//如果大于0且小于等于总页数,正常运行
if(thisPage>0&&thisPage<=sumPage){
this.thisPage=thisPage;
}
//如果当前页数大于总页数,那么跳转到最后一页(设置为总页数)
else if(thisPage>sumPage){
this.thisPage=getSumPage();
}
//如果都不满足(小于0) 那么设为默认值1
else{
this.thisPage=1;
}
}
public int getSumPage() {
return sumPage;
}
//当设置总页数时,进行判断
public void setSumPage(int sumPage) {
//当总页数大于0时 进行赋值 否则将sumPage设为1(可以不设else 那么出现<0时SumPage不变)
if(sumPage>0){
this.sumPage = sumPage;
//当总页数设置完之后,需要将当前页数设为当前页数
//因为当删除数据之后 4页变成了3页 那么当前页数不能继续为4 而是为最大页数3
this.setThisPage(this.getThisPage());
}else{
this.sumPage = 1;
}
}
public int getShowPage() {
return showPage;
}
public void setShowPage(int showPage) {
//这里可以不进行判断,因为没在外面设置ShowPage
//但是有些页面需要进行每页显示的修改 如论坛
//需要进行每页显示的修改时需要进行判断
if(showPage>=1&&showPage<=99){
this.showPage = showPage;
}
}
//获取总记录数 初始化或每次添加、删除结束后执行
public void loadSumRecord(){
System.out.println("Tool - Page - getSumPage()");
try{
//创建Connection连接
connection=BookManageOper.create();
//查询Book表的所有记录
String sql="select count(1) from book";
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
//因为是返回count(1)[只返回一个数据] 所以使用if
if(resultSet.next()){
sumRecord=resultSet.getInt(1);
//三目运算符 if?true:false 当总记录除总页数余0时 返回结果,不为0返回结果+1
this.setSumPage(sumRecord%showPage==0?sumRecord/showPage:sumRecord/showPage+1);
}
}catch (Exception ex){
ex.printStackTrace();
}finally {
BookManageOper.close(connection,preparedStatement,resultSet);
}
}
}
(3)src – jdbc – Entity
(3.1)src – jdbc – Entity – Book[图书][实体类]
package Jdbc.Entity;
public class Book {
//图书 mysql - BookManage . Book
//图书编号
private int id;
//图书名
private String name;
//作者名
private String author;
//购入时间
private String time;
//图书类型 - Type类 包含id(类型编号)与name(类型名)
private Type type;
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 getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
}
(3.2)src – jdbc – Entity – Type[类型][实体类]
package Jdbc.Entity;
public class Type {
//图书类型 mysql - BookManage . Type
//图书类型编号
private int id;
//图书类型名
private String name;
//无参构造方法
public Type(){
}
//有参构造方法 方便赋值 不用再进行type.setId()...
public Type(int id, String name) {
this.id = id;
this.name = name;
}
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;
}
}
(4)src – jdbc – Dao
(4.1)src – jdbc – Dao – BookDao[图书][Dao接口]
package Jdbc.Dao;
import Jdbc.Entity.Book;
import java.util.List;
public interface BookDao {
//获取所有书籍数据 Book - index.jsp/selectServlet -
List<Book> getAllBook();
//增加一条数据 Book - insert.jsp/insertServlet -
boolean insertBook(String name,String author,String time,int type);
//删除一条数据 Book - index.jsp/deleteServlet -
boolean deleteBook(int id);
}
(4.2)src – jdbc – Dao – TypeDao[类型][Dao接口]
package Jdbc.Dao;
import Jdbc.Entity.Type;
import java.util.List;
public interface TypeDao {
//获取所有类型信息 Type - insert.jsp/Servlet -
List<Type> getAllType();
}
(5)src – jdbc – Impl
Impl通过implements Dao 来实现Dao中的抽象方法
impl一般来说都是默认类 不会是接口或抽象类 因为没有意义
(5.1)src – jdbc – Impl – BookDaoImpl(BookDao)[图书][Dao接口实现类]
package Jdbc.Impl;
import Jdbc.Dao.BookDao;
import Jdbc.Entity.Book;
import Jdbc.Entity.Type;
import Jdbc.Tool.BookManageOper;
import Jdbc.Tool.Page;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class BookDaoImpl implements BookDao {
//连接数据库与处理sql
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
@Override
public List<Book> getAllBook() {//获取所有书籍数据
//当获取书籍时 首先重新加载一下记录
Page page=Page.getPage();
page.loadSumRecord();
//先创建一个List 之后直接Return List
List<Book> bookList=new ArrayList<>() ;
try{
connection=BookManageOper.create();
//查询图书表中的x-x行数据 并查询它对应的类型名
String sql="select b.*,t.* from book b,type t where b.type=t.id limit ?,?";
preparedStatement=connection.prepareStatement(sql);
// row * ( page - 1 ) 5 * (2-1) = 5 不在前五行
int Limit1=page.getShowPage()*(page.getThisPage()-1);
preparedStatement.setInt(1, Limit1);
preparedStatement.setInt(2, page.getShowPage());
resultSet=preparedStatement.executeQuery();
//返回不确定行或多行的查询sql语句 使用while
while (resultSet.next()){
//创建一个book对象 并将数据库中的值赋给这个对象
Book book=new Book();
book.setId(resultSet.getInt("b.id"));
book.setName(resultSet.getString("b.name"));
book.setAuthor(resultSet.getString("b.author"));
book.setTime(resultSet.getString("b.time"));
book.setType(new Type(resultSet.getInt("t.id"),resultSet.getString("t.name")));
//将当前对象加入到list中
bookList.add(book);
}
System.out.println("Impl - BookDaoImpl - getAllBook() ReturnListCount : "+bookList.size());
}catch (Exception ex){
ex.printStackTrace();
System.out.println("Impl - BookDaoImpl - getAllBook() Error");
}finally {
BookManageOper.close(connection,preparedStatement,resultSet);
}
return bookList;
}
@Override
public boolean insertBook(String name,String author,String time,int type) {//添加一条数据
boolean Return=false;
try{
connection=BookManageOper.create();
//增加一条数据 因为id为自增 所以会自动赋值
String sql="insert into book(name,author,time,type) values (?,?,?,?)";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,name);
preparedStatement.setString(2,author);
preparedStatement.setString(3,time);
preparedStatement.setInt(4,type);
//执行返回影响行数的sql使用Update
int ReturnInt=preparedStatement.executeUpdate();
//返回行数大于0则成功
if(ReturnInt>0)
Return=true;
System.out.println("Impl - BookDaoImpl - insertBook(s,s,s,i) Return : "+Return);
}catch (Exception ex){
ex.printStackTrace();
System.out.println("Impl - BookDaoImpl - insertBook(s,s,s,i)");
}finally {
BookManageOper.close(connection,preparedStatement,resultSet);
}
return Return;
}
@Override
public boolean deleteBook(int id) {//删除一条数据
boolean Return=false;
try{
connection=BookManageOper.create();
//根据id删除一条数据 因为id是唯一的
String sql="delete from book where id=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1,id);
int ReturnInt=preparedStatement.executeUpdate();
if(ReturnInt>0)
Return=true;
System.out.println("Impl - BookDaoImpl - insertBook(s,s,s,i) Return : "+Return);
}catch (Exception ex){
ex.printStackTrace();
System.out.println("Impl - BookDaoImpl - deleteBook(s) Error");
}finally {
BookManageOper.close(connection,preparedStatement,resultSet);
}
return Return;
}
}
(5.2)src – jdbc – Impl – TypeDaoImpl(TypeDao)[类型][Dao接口实现类]
package Jdbc.Impl;
import Jdbc.Dao.TypeDao;
import Jdbc.Entity.Type;
import Jdbc.Tool.BookManageOper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class TypeDaoImpl implements TypeDao {
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
@Override
public List<Type> getAllType() {//获取所有的图书类型
List<Type> typeList = new ArrayList<>();
try{
connection=BookManageOper.create();
String sql="select * from type";
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
while (resultSet.next()){
Type type=new Type();
type.setId(resultSet.getInt("id"));
type.setName(resultSet.getString("name"));
typeList.add(type);
}
System.out.println("Impl - TypeDaoImpl - getAllType() - ReturnListCount : "+typeList.size());
}catch (Exception ex){
ex.printStackTrace();
System.out.println("Impl - TypeDaoImpl - getAllType() Error");
}finally {
BookManageOper.close(connection,preparedStatement,resultSet);
}
System.out.println("Impl - TypeDaoImpl - getAllType() 完成");
System.out.println();
return typeList;
}
}
-
- web && src – Servlet
(1)web – web_inf
(1.1)web – web_inf – web.xml[Web配置文件]
web.xml是web程序的配置文件,它很重要!!!没有配置web.xml的话会报大错
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--启动时的欢迎页面 修改只需要修改welcome-file-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--Servlet的映射 Servlet如果不进行映射则不会被外部所访问-->
<servlet>
<servlet-name>SelectServlet</servlet-name>
<servlet-class>Servlet.SelectServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SelectServlet</servlet-name>
<url-pattern>/SelectServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>InsertServlet</servlet-name>
<servlet-class>Servlet.InsertServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InsertServlet</servlet-name>
<url-pattern>/InsertServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>deleteServlet</servlet-name>
<servlet-class>Servlet.deleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>deleteServlet</servlet-name>
<url-pattern>/deleteServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>PageServlet</servlet-name>
<servlet-class>Servlet.PageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PageServlet</servlet-name>
<url-pattern>/PageServlet</url-pattern>
</servlet-mapping>
</web-app>
(2)web – *.jsp 与其相对应的Servlet
(2.1)web – index页面
(2.1.1)web – index.jsp[首页][Jsp页面]
<%@ page import="java.util.List" %>
<%@ page import="Jdbc.Entity.Book" %>
<%@ page import="Jdbc.Tool.Page" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--
Created by IntelliJ IDEA.
User: 30974
Date: 2023/1/9
Time: 20:33
To change this template use File | Settings | File Templates.
--%>
<link rel="shortcut icon" href="#">
<html>
<head>
<title>个人图书管理</title>
<%--<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0"/>--%>
<%--<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>--%>
</head>
<body>
<%
Page page1=Page.getPage();
List<Book> bookList=(List<Book>) session.getAttribute("bookList");
if(bookList==null){
request.getRequestDispatcher("SelectServlet").forward(request,response);
}
//else如果删掉 session一直有值 就不会再次进入获取bookList 会造成页面数据不刷新
else{
session.setAttribute("bookList",null);
}
%>
<h1>图书信息</h1>
<table border="1">
<tr>
<td>图书名称</td>
<td>图书作者</td>
<td>购买时间</td>
<td>图书分类</td>
<td>操作</td>
</tr>
<%
for (Book b : bookList) {
out.print(
"<tr id=trid"+b.getId()+">" +
"<td>"+b.getName()+"</td>" +
"<td>"+b.getAuthor()+"</td>" +
"<td>"+b.getTime()+"</td>" +
"<td>"+b.getType().getName()+"</td>" +
"<td><span class=delete bookid="+b.getId()+">删除</span></td>" +
"</tr>"
);
}
%>
</table>
<p>
<%--设置toPageType 使pageServlet能够识别进行哪种操作--%>
<a href="PageServlet?toPageType=First">首页</a>
<a href="PageServlet?toPageType=Prev">上一页</a>
<span>
<%=page1.getThisPage()%>/<%=page1.getSumPage()%>
</span>
<a href="PageServlet?toPageType=Next">下一页</a>
<a href="PageServlet?toPageType=Last">尾页</a>
</p>
<a href="Insert.jsp">新增图书信息</a>
<script src="JS/jquery-1.12.4.js"></script>
<script src="JS/bootstrap.js"></script>
<script>
$(function(){
setOddBack()
})
function setOddBack(){
$("tr:odd").css("background","gray");
$("tr:even").css("background","none");
}
$(".delete").click(
function () {
var bookid=$(this).attr("bookid");
//confirm 根据点击的按钮返回true false
var deleteIs=confirm("确定要删除编号"+bookid+"的书籍吗?");
//如果点击确定 那么进入deleteServlet
if(deleteIs==true){
location.href="deleteServlet?bookid="+bookid;
}
}
)
<%
//删除操作进行后根据return值进行弹框
String delIs=request.getParameter("delIs");
if (delIs!=null){
if(delIs.equals("delTrue")){
%>
alert("删除成功");
<%
}else{
%>
alert("删除失败");
<%
}
}
%>
</script>
</body>
</html>
(2.1.2)web – SelectServlet (index.jsp)[查询][Servlet]
package Servlet;
import Jdbc.Dao.BookDao;
import Jdbc.Impl.BookDaoImpl;
import Jdbc.Entity.Book;
import javafx.application.Application;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet(name = "SelectServlet")
public class SelectServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//当前doPost()暂无作用
System.out.println();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//当前doGet()用于页面中Session - bookList的初始化
//创建Jdbc - Impl - BookDaoImpl实例
BookDao bookDao=new BookDaoImpl();
//获取book中所有数据
List<Book> bookList=bookDao.getAllBook();
//将book表中的数据存入Session - bookList中
HttpSession session=request.getSession();
session.setAttribute("bookList",bookList);
//跳转到jsp页面
request.getRequestDispatcher("index.jsp").forward(request,response);
System.out.println();
}
}
(2.1.3)web – deleteServlet(index.jsp)[删除][Servlet]
package Servlet;
import Jdbc.Dao.BookDao;
import Jdbc.Impl.BookDaoImpl;
import Jdbc.Tool.Page;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "deleteServlet")
public class deleteServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String idString=request.getParameter("bookid");
int id=Integer.parseInt(idString);
BookDao bookDao=new BookDaoImpl();
//根据id删除一条数据 返回true成功/false失败
boolean ReturnIs=bookDao.deleteBook(id);
//如果成功将index.jsp中的delIs设为delTrue 否则设为delFalse
if(ReturnIs==true){
request.getRequestDispatcher("index.jsp?delIs=delTrue").forward(request,response);
}else{
request.getRequestDispatcher("index.jsp?delIs=delFalse").forward(request,response);
}
}
}
(2.1.4)web – PageServlet(index.jsp)[分页][Servlet]
package Servlet;
import Jdbc.Tool.Page;
import com.mysql.cj.Session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name = "PageServlet")
public class PageServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String toPageType=request.getParameter("toPageType");
Page page=Page.getPage();
//switch判断是什么操作 执行对应的代码
switch (toPageType){
case "First":
page.setThisPage(1);
break;
case "Prev":
page.setThisPage(page.getThisPage()-1);
break;
case "Next":
page.setThisPage(page.getThisPage()+1);
break;
case "Last":
page.setThisPage(page.getSumPage());
break;
}
HttpSession session=request.getSession();
session.setAttribute("bookList",null);
//这里不能使用转发 使用转发会出现thisPage多次++的问题
response.sendRedirect("index.jsp");
}
}
(2.1)web – insert页面
(2.2)web – insert.jsp[添加][Jsp页面]
<%@ page import="java.util.List" %>
<%@ page import="Jdbc.Entity.Type" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer" %>
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.text.SimpleDateFormat" %><%--
Created by IntelliJ IDEA.
User: 30974
Date: 2023/1/10
Time: 11:30
To change this template use File | Settings | File Templates.
--%>
<html>
<head>
<title>添加图书</title>
</head>
<body>
<%
if(session.getAttribute("typeList")==null) {
request.getRequestDispatcher("InsertServlet").forward(request, response);
}
//获取一个本日时间,年-月-日 格式
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
String thisDay=simpleDateFormat.format(new Date());
request.setAttribute("thisDay",thisDay);
%>
<form method="post" action="InsertServlet">
<p>
图书名称
<input type="text" name="insertName" required>
</p>
<p>
图书作者
<input type="text" name="insertAuthor" required>
</p>
<p>
//购入时间默认为本日 并且最大为本日
购入时间
<input type="date" name="insertTime" value="${thisDay}" max="${thisDay}" min="2000-01-01" required>
</p>
<p>
图书类别
<select name="insertType" required>
<c:forEach items="${sessionScope.typeList}" var="t" varStatus="status">
<option value="${t.getId()}">${t.getName()}</option>
</c:forEach>
</select>
</p>
<p>
<input type="submit" id="insertSub" value="增加图书">
</p>
</form>
<script src="JS/jquery-1.12.4.js"></script>
<script>
function goIndex() {
location.href="index.jsp"
}
<%
String insIs=request.getParameter("insIs");
if (insIs!=null){
if(insIs.equals("insTrue")){
%>
var conIs=confirm("添加成功(确定继续添加,取消返回首页)");
if(conIs==false){
goIndex();
}else{
;
}
<%
}else{
%>
alert("添加失败");
<%
}
}
%>
</script>
</body>
</html>
(2.2.1)web – InsertServlet(Insert.jsp)[添加][Servlet]
package Servlet;
import Jdbc.Dao.BookDao;
import Jdbc.Dao.TypeDao;
import Jdbc.Entity.Type;
import Jdbc.Impl.BookDaoImpl;
import Jdbc.Impl.TypeDaoImpl;
import com.mysql.cj.Session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet(name = "InsertServlet")
public class InsertServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//当前doPost()方法用于向BookManage - Book表中添加一条数据
//设置获取的字符编码格式为utf-8
request.setCharacterEncoding("utf-8");
//获取所有需要添加的字段
String name=request.getParameter("insertName");
String author=request.getParameter("insertAuthor");
String time=request.getParameter("insertTime");
String typeString=request.getParameter("insertType");
int type=Integer.parseInt(typeString);
//创建Jdbc - Impl - BookDaoImpl实例
BookDao bookDao=new BookDaoImpl();
//进行添加 并返回是否添加成功(t/f)
boolean insertIs=bookDao.insertBook(name,author,time,type);
//跳转到jsp页面
if(insertIs==true){
request.getRequestDispatcher("Insert.jsp?insIs=insTrue").forward(request,response);
}else{
request.getRequestDispatcher("Insert.jsp?insIs=insFalse").forward(request,response);
}
System.out.println();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//当前doGet()用于页面中Session - typeList的初始化
//创建Jdbc - Impl - TypeDaoImpl实例
TypeDao typeDao=new TypeDaoImpl();
//获取Type表中的所有数据
List<Type> typeList=typeDao.getAllType();
//将Type表中的数据添加到Session - typeList中,以便jsp页面进行使用
HttpSession session=request.getSession();
if(typeList!=null)
session.setAttribute("typeList",typeList);
//跳转到jsp页面
request.getRequestDispatcher("Insert.jsp").forward(request,response);
System.out.println();
}
}
六.结尾
原文地址:https://blog.csdn.net/minecrafteawrt/article/details/128665669
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_21388.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!