2021-04-21 22:06  阅读(126)
文章分类:JSP 简单教程 文章标签:JSP
©  原文作者:一点教程 原文地址:http://www.yiidian.com/jsp/

本文讲解在JSP中轻松创建CRUD示例。我们将使用Dao访问数据库,将使用JSTL遍历数据库记录。

1 下载jstl包和mysql驱动包

下载jstl包

下载mysql驱动包

2 创建表和插入测试数据

1)在MySQL的test数据库,使用以下SQL创建customer表:

    
    CREATE TABLE `customer` (
       `id` int(11) NOT NULL AUTO_INCREMENT,
       `name` varchar(50) DEFAULT NULL,
       `gender` varchar(20) DEFAULT NULL,
       `telephone` varchar(100) DEFAULT NULL,
       `address` varchar(100) DEFAULT NULL,
       PRIMARY KEY (`id`)
     ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8

注意:id采用MySQL的自增长策略,所以我们程序不用维护id值。

2)插入一些测试数据,如下:

202104212201111_1.png

3 编写Customer实体

    
    package com.yiidian.domain;
    
    /**
     * 一点教程网 - http://www.yiidian.com
     */
    public class Customer {
        private Integer id;
        private String name;
        private String gender;
        private String telephone;
        private String address;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getGender() {
            return gender;
        }
    
        public void setGender(String gender) {
            this.gender = gender;
        }
    
        public String getTelephone() {
            return telephone;
        }
    
        public void setTelephone(String telephone) {
            this.telephone = telephone;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    }
    

4 编写CustomerDao

    
    package com.yiidian.dao;
    
    import com.yiidian.domain.Customer;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 一点教程网 - http://www.yiidian.com
     */
    public class CustomerDao {
    
        public static Connection getConnection(){
            Connection con=null;
            try{
                Class.forName("com.mysql.jdbc.Driver");
                con= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
            }catch(Exception e){System.out.println(e);}
            return con;
        }
        public static int save(Customer customer){
            int status=0;
            try{
                Connection con=getConnection();
                PreparedStatement ps=con.prepareStatement(
                        "insert into customer(name,gender,telephone,address) values(?,?,?,?)");
                ps.setString(1,customer.getName());
                ps.setString(2,customer.getGender());
                ps.setString(3,customer.getTelephone());
                ps.setString(4,customer.getAddress());
                status=ps.executeUpdate();
            }catch(Exception e){System.out.println(e);}
            return status;
        }
    
        public static int update(Customer customer){
            int status=0;
            try{
                Connection con=getConnection();
                PreparedStatement ps=con.prepareStatement(
                        "update customer set name=?,gender=?,telephone=?,address=? where id=?");
                ps.setString(1,customer.getName());
                ps.setString(2,customer.getGender());
                ps.setString(3,customer.getTelephone());
                ps.setString(4,customer.getAddress());
                ps.setInt(5,customer.getId());
                status=ps.executeUpdate();
            }catch(Exception e){System.out.println(e);}
            return status;
        }
    
        public static int delete(Customer customer){
            int status=0;
            try{
                Connection con=getConnection();
                PreparedStatement ps=con.prepareStatement("delete from customer where id=?");
                ps.setInt(1,customer.getId());
                status=ps.executeUpdate();
            }catch(Exception e){System.out.println(e);}
    
            return status;
        }
        public static List<Customer> getAllRecords(){
            List<Customer> list=new ArrayList<Customer>();
    
            try{
                Connection con=getConnection();
                PreparedStatement ps=con.prepareStatement("select * from customer");
                ResultSet rs=ps.executeQuery();
                while(rs.next()){
                    Customer customer=new Customer();
                    customer.setId(rs.getInt("id"));
                    customer.setName(rs.getString("name"));
                    customer.setGender(rs.getString("gender"));
                    customer.setTelephone(rs.getString("telephone"));
                    customer.setAddress(rs.getString("address"));
                    list.add(customer);
                }
            }catch(Exception e){
                System.out.println(e);
            }
            return list;
        }
        public static Customer getRecordById(int id){
            Customer customer=null;
            try{
                Connection con=getConnection();
                PreparedStatement ps=con.prepareStatement("select * from customer where id=?");
                ps.setInt(1,id);
                ResultSet rs=ps.executeQuery();
                while(rs.next()){
                    customer=new Customer();
                    customer.setId(rs.getInt("id"));
                    customer.setName(rs.getString("name"));
                    customer.setGender(rs.getString("gender"));
                    customer.setTelephone(rs.getString("telephone"));
                    customer.setAddress(rs.getString("address"));
                }
            }catch(Exception e){
                System.out.println(e);
            }
            return customer;
        }
    }
    

5 编写index.jsp

    
    <%@ page language="java" contentType="text/html;charset=utf-8" %>
    <html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>一点教程网-使用JSP完成CRUD</title>
    </head>
    <body>
    
    <h1>使用JSP完成CRUD</h1>
    <a href="addcustform.jsp">添加客户</a>
    <a href="viewcusts.jsp">查看客户</a>
    
    </body>
    </html>

6 编写addcustform.jsp

    
    <%@ page language="java" contentType="text/html;charset=utf-8" %>
    <html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>一点教程网-使用JSP完成CRUD</title>
    </head>
    <body>
    
    <h1>使用JSP完成CRUD</h1>
    <a href="addcustform.jsp">添加客户</a>
    <a href="viewcusts.jsp">查看客户</a>
    
    </body>
    </html>

7 编写addcust.jsp

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@page import="com.yiidian.dao.CustomerDao"%>
    <html>
    <head>
        <title>一点教程网-使用JSP完成CRUD</title>
    </head>
    <body>
    <%
        //设置参数的中文编码
        request.setCharacterEncoding("utf-8");
    %>
    <jsp:useBean id="customer" class="com.yiidian.domain.Customer"></jsp:useBean>
    <jsp:setProperty property="*" name="customer"/>
    
    <%
        int i= CustomerDao.save(customer);
        if(i>0){
            response.sendRedirect("addcust-success.jsp");
        }else{
            response.sendRedirect("addcust-error.jsp");
        }
    %>
    
    </body>
    </html>
    

8 编写addcust-success.jsp

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>一点教程网-使用JSP完成CRUD</title>
    </head>
    <body>
    <p>客户保存成功!</p>
    <a href="viewcusts.jsp">客户列表</a>
    </body>
    </html>
    

9 编写addcust-error.jsp

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>一点教程网-使用JSP完成CRUD</title>
    </head>
    <body>
    <p>抱歉, 保存失败!</p>
    </body>
    </html>
    

10 编写viewcusts.jsp

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@page import="com.yiidian.dao.CustomerDao,com.yiidian.domain.*,java.util.*"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <html>
    <head>
        <title>一点教程网-使用JSP完成CRUD</title>
    </head>
    <body>
    <h1>客户列表</h1>
    
    <%
        List<Customer> list=CustomerDao.getAllRecords();
        request.setAttribute("list",list);
    %>
    
    | 编号|名称|性别|联系电话|住址|编辑|删除|
| :-----: | :-----: | :-----: | :-----: | :-----: | :-----: | :-----: | 
| ${cust.id} | ${cust.name} | ${cust.gender} | ${cust.telephone} | ${cust.address} | 编辑 | 删除 | 

    <br/><a href="addcustform.jsp">添加客户</a>
    </body>
    </html>
    

11 编写editform.jsp

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ page import="com.yiidian.dao.CustomerDao,com.yiidian.domain.Customer"%>
    <html>
    <head>
        <title>一点教程网-使用JSP完成CRUD</title>
    </head>
    <body>
    
    <%
        String id=request.getParameter("id");
        Customer customer=CustomerDao.getRecordById(Integer.parseInt(id));
    %>
    
    <h1>编辑客户</h1>
    <form action="editcust.jsp" method="post">
        <input type="hidden" name="id" value="<%=customer.getId() %>"/>
        | 名称:|">|
| :-----: | :-----: | 
| 名称: | "> | 
| 性别: | &lt;%if(customer.getGender().equals("男")){%&gt;checked&lt;%}%&gt;/&gt;男&lt;%if(customer.getGender().equals("女")){%&gt;checked&lt;%}%&gt;/&gt;女 | 
| 联系电话: | "> | 
| 住址: | "> | 
|  | 

    </form>
    
    </body>
    </html>
    

12 编写editcust.jsp

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@page import="com.yiidian.dao.CustomerDao"%>
    <html>
    <head>
        <title>一点教程网-使用JSP完成CRUD</title>
    </head>
    <body>
    <%
        //设置参数的中文编码
        request.setCharacterEncoding("utf-8");
    %>
    <jsp:useBean id="customer" class="com.yiidian.domain.Customer"></jsp:useBean>
    <jsp:setProperty property="*" name="customer"/>
    
    <%
        int i= CustomerDao.update(customer);
        response.sendRedirect("viewcusts.jsp");
    %>
    
    </body>
    </html>
    

13 编写deletecust.jsp

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@page import="com.yiidian.dao.CustomerDao"%>
    <html>
    <head>
        <title>一点教程网-使用JSP完成CRUD</title>
    </head>
    <body>
    <jsp:useBean id="customer" class="com.yiidian.domain.Customer"></jsp:useBean>
    <jsp:setProperty property="*" name="customer"/>
    <%
        CustomerDao.delete(customer);
        response.sendRedirect("viewcusts.jsp");
    %>
    </body>
    </html>
    

14 运行测试

2021042122017_2.png

2021042122010_3.png202104212208_4.png

202104212206_5.png

202104212206_6.png

202104212206_7.png

202104212206_8.png

点赞(0)
版权归原创作者所有,任何形式转载请联系作者; Java 技术驿站 >> 使用JSP完成CRUD
上一篇
JSP页面异常处理
下一篇
使用JSP完成分页