继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

图书系统接口说明文档....

摄氏du江
关注TA
已关注
手记 41
粉丝 58
获赞 1061

图片描述

图片描述

图片描述


用到的是MySQL数据库,调用数据Service层代码如下:

package com.blf.mypro.service;

import com.blf.mypro.po.Book;
import com.blf.mypro.util.DBHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Arvin on 2016/12/27.
 */
public class BookService {

    private DBHelper dbHelper = new DBHelper();

    /**
     * 查询所有图书
     */
    public List<Book> getAllBook(){
        // 准备SQL命令
        String sql = "select * from book";
        // 执行SQL命令获取数据结果
        ResultSet resultSet = dbHelper.execQuery(sql);
        // 把数据结果转为逻辑结果
        List<Book> books = new ArrayList<Book>();
        try {
            while(resultSet.next()){
                Book book = new Book();
                book.setBid(resultSet.getInt(1));
                book.setBname(resultSet.getString(2));
                book.setAuthor(resultSet.getString(3));
                book.setPrice(resultSet.getFloat(4));
                book.setPublisher(resultSet.getString(5));
                book.setPubdate(resultSet.getDate(6));
                book.setPicture(resultSet.getString(7));
                books.add(book);
            }
            return books;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                dbHelper.closeAll();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 通过出版社检索图书
     * @return
     */
    public List<Book> getBookByPublisher(String publisher,boolean islike){
        // 准备SQL命令
        String sql = "";
        // 执行SQL命令获取数据结果
        ResultSet resultSet = null;
        if (islike){
            sql = "select * from book where publisher like ?";
            resultSet = dbHelper.execQuery(sql, '%'+publisher+'%');
        }else {
            sql = "select * from book where publisher=?";
            resultSet = dbHelper.execQuery(sql,publisher);
        }
        // 把数据结果转为逻辑结果
        List<Book> books = new ArrayList<Book>();
        try {
            while(resultSet.next()){
                Book book = new Book();
                book.setBid(resultSet.getInt(1));
                book.setBname(resultSet.getString(2));
                book.setAuthor(resultSet.getString(3));
                book.setPrice(resultSet.getFloat(4));
                book.setPublisher(resultSet.getString(5));
                book.setPubdate(resultSet.getDate(6));
                book.setPicture(resultSet.getString(7));
                books.add(book);
            }
            return books;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                dbHelper.closeAll();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    /**
     * 查询所有出版社
     */
    public List<String> getAllPublisher(){
        String sql = "select distinct publisher from book";
        ResultSet resultSet = dbHelper.execQuery(sql);
        List<String> publishers = new ArrayList<String>();
        try {
            while (resultSet.next()){
                publishers.add(resultSet.getString(1));
            }
            return publishers;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                resultSet.close();
                dbHelper.closeAll();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

有多少文档,就该有多少Servlet.
这里给出:GetPublisherServlet

package com.blf.mypro.controller;

import com.chinasoft.mypro.service.BookService;
import com.google.gson.Gson;

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;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Dujiang on 2016/12/29.
 */
@WebServlet(name = "GetPublisherServlet",value = "/getpublishers")
public class GetPublisherServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

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

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        BookService bookService = new BookService();
        List<String> publishers = bookService.getAllPublisher();
        Gson gson = new Gson();
        String json = gson.toJson(publishers);
        out.print(json);
    }
}
打开App,阅读手记
3人推荐
发表评论
随时随地看视频慕课网APP