欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Bean/DAO/Service/Servlet简单综合应用

程序员文章站 2022-05-06 20:37:18
...

分层思想

dao层:在没有使用mybatis等框架的时候,完成注册、获取连接、执行sql并返回结果
service:对应一个servlet,完成调用dao的功能,返回dao层输入的结果
servlet:完成获取jsp页面信息,并反馈结果到页面中
bean:是对结果的封装(实质将已赋值的bean存在list中,然后返回list到jsp页面中)

实例

项目结构

Bean/DAO/Service/Servlet简单综合应用

代码块

  • bean层
    Book.java
public class Book {
    //图书基本信息:书号、书名、作者、类型、价格、数量、出版日期
    private String BID;
    private String BNAME;
    private String BAUTHER;
    private String BTYPE;
    private int BPRICE;
    private int BAMOUNT;
    private Date BPUBDATE;
    //getter和setter方法省略
}
  • dao
    BookDao.java
public interface BookDao {
    //增加图书
    public void addBook(Book book) throws Exception;
}

BookDaoImpl.java

public class BookDaoImpl implements BookDao {
    @Override
    public void addBook(Book book) throws Exception {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            //连接数据库
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement("INSERT INTO T_BOOK VALUES (?,?,?,?,?,?,?)");
            ps.setString(1,book.getBID());
            ps.setString(2, book.getBNAME());
            ps.setString(3, book.getBAUTHER());
            ps.setString(4, book.getBTYPE());
            ps.setInt(5, book.getBPRICE());
            ps.setInt(6, book.getBAMOUNT());
            //格式化日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String publicDate = sdf.format(book.getBPUBDATE());
            ps.setString(7, publicDate);
            ps.executeUpdate();
            System.out.println("成功");
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("添加失败!");
        }
    }
}
  • service层
    BookService.java
public interface BookService {
    //增加图书
    public void addBook(Book book) throws Exception;
}

BookServiceImpl.java

public class BookServiceImpl implements BookService {
    
    BookDao bookDao = new BookDaoImpl();

    @Override
    public void addBook(Book book) throws Exception {
        bookDao.addBook(book);
    }
}
  • servlet层
    addBookServlet.java
package servlet;

import bean.Book;
import service.BookService;
import service.BookServiceImpl.BookServiceImpl;

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

@WebServlet(name = "AddBookServlet",urlPatterns = {"/AddBookServlet"})
public class addBookServlet extends HttpServlet {
    //确保版本的兼容性
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //防止中文乱码
        req.setCharacterEncoding("gb2312");
        req.setCharacterEncoding("gb2312");
        resp.setContentType("text/html;charset=gb2312");
        //把表单提交到User类中
        Book b = new Book();
        b.setBID(req.getParameter("BID"));
        b.setBNAME(req.getParameter("BNAME"));
        b.setBAUTHER(req.getParameter("BAUTHER"));
        b.setBTYPE(req.getParameter("BTYPE"));
        b.setBPRICE(Integer.parseInt(req.getParameter("BPRICE")));
        b.setBAMOUNT(Integer.parseInt(req.getParameter("BAMOUNT")));
        String publicDate = req.getParameter("BPUBDATE");
        
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = sdf.parse(publicDate);
                b.setBPUBDATE(date);
                resp.getWriter().write("<p>图书添加成功!</p><br>");
        }catch (ParseException e){
                e.printStackTrace();
                System.out.println("添加失败!");
            }
            
            //调用业务逻辑层
        BookService bookService = new BookServiceImpl();
            try {
                bookService.addBook(b);
            }catch (Exception e){
                e.printStackTrace();
            }           
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

  • JSP查询页面
<%@ page contentType="text/html;charset=gb2312" language="java" import="java.sql.*" %>
<html>
<head><title>图书增加</title></head>
<body>
<h1>图书增加</h1>
<form method="post" action="AddBookServlet">

    书号:<input name="BID" type="text" id="BID" /><br>
    书名:<input name="BNAME" type="text" id="BNAME" /><br>
    作者:<input name="BAUTHER" type="text" id="BAUTHER" /><br>
    类型:<input name="BTYPE" type="text" id="BTYPE" /><br>
    价格:<input name="BPRICE" type="text" id="BPRICE" /><br>
    数量:<input name="BAMOUNT" type="text" id="BAMOUNT" /><br>
    出版日期:<input name="BPUBDATE" type="text" id="BPUBDATE" /><br>

    <input name="submit" type="submit" value="确定" />
    <input name="reset" type="reset" value="重置" />
</form>
</body>
</html>