数据库信息的web网页化显示——增删改查

一、bootStrap

1、bootstrap介绍

  • bootstrap是简洁、直观、强悍的前端开发框架,让web开发更迅速、简单。
  • bootstrap是当下最流行的前端框架(界面工具集)。
  • 目的是为了让web开发更敏捷。

2、为什么使用bootstrap

  • 提供了一套美观大方的界面组件。
  • 提供了一套优雅的HTML+CSS编码规范。
  • 让web开发更简单、更快捷。
  • 使用bootstrap并不代表不用写CSS样式,二是不用写绝大多数大家都会用到的样式。
  • bootstrap提供了大量的全局样式,在基本的HTML标签上通过添加class设置样式得到增强的效果。
    具体bootstrap框架代码

二、以书籍管理为例

1、service方法代码

@WebServlet("/book")
public class BookServlet extends HttpServlet {
   
    // 访问Servlet默认访问service方法
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
        System.out.println("BookServlet.service");
        // 解决post请求乱码问题
        req.setCharacterEncoding("UTF-8");

        // 把所有和书籍相关的增删改查放到一个Servlet
        //http://localhost:8080/JavaWeb/book?menthod=select
        //http://localhost:8080/JavaWeb/book?menthod=deleteById&id=2
        //http://localhost:8080/JavaWeb/book?menthod=insert
        String method = req.getParameter("method");
        if (method == null || method == "") {
   
            method = "select";
        }

        switch (method) {
   
            case "select":
                select(req,resp);
                break;
            case "delete":
                delete(req,resp);
                break;
            case "insert":
                insert(req,resp);
                break;
            case "selectById":
                selectById(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
   
        System.out.println("BookServlet.update");
        // 这些是传递的参数,用户在浏览器界面输入的信息
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String publish = req.getParameter("publish");
        String price = req.getParameter("price");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
   
            connection = JDBCUtil.getConnection();
            String sql = "update book set name=?,publish=?,price=? where id=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,name);
            statement.setString(2,publish);
            statement.setDouble(3,Double.parseDouble(price));
            statement.setInt(4,Integer.parseInt(id));
            int count = statement.executeUpdate();
            System.out.println("count:" + count);
        } catch (SQLException throwables) {
   
            throwables.printStackTrace();
        } finally {
   
            JDBCUtil.close(connection,statement,null);
        }

        resp.sendRedirect(req.getContextPath() + "/book?method=select");
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   
        System.out.println("BookServlet.selectById");
        int id = Integer.parseInt(req.getParameter("id"));
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null; // 结果集,存放从数据库中读取的符合条件的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值