test第七次全国人口普查登记

一、题目:

第七次全国人口普查登记(20分)

1、项目需求:

开展第七次全国人口普查,将为编制“十四五”规划提供重要信息支持;推动实现人口与经济社会、资源环境协调发展,为深化供给侧结构性改革,推动经济高质量发展,建设现代化经济体系提供强有力的支持;人口普查还将为完善人口发展战略和政策体系,促进人口长期均衡发展提供重要信息支持。

2.系统要求与功能设计

2.1 页面功能要求

1)能够在Tomcat服务器中正确部署,并通过浏览器查看;(1分)

2)网站页面整体风格统一;

 

1 人口普查系统功能结构图

3)主页面:要求显示人口登记、修改人口信息、删除人口信息、查询人口信息,人口信息浏览五个子菜单。(1分)

4)人口登记页面:(6分)

①完成添加人口信息登记,基本信息和填报限制如下表所示

家庭户 集体户(单选框)

住房类型

家庭住宅、集体住所、工作地住所、其他住宅、无住宅(单选框)

本户现住房面积

XXXXX平方米(文本框,只能输入这个整数)

本户现住房间数

XXXXX间(文本框,只能输入这个整数)

户主姓名

XXXXX(文本框)

身份证号码

XXXXXXXXXXXXXXXXXX(判断身份证位数13位并且只能录入数字、最后一位可以录入X)

性别

/女(单选框)

民族

(文本框)

受教育程度

研究生、大学本科、大学专科、高中、初中、小学、未上过学(下拉框实现)

 

点击“提交”按钮,保存成功则跳转到人口基本信息浏览界面,新录入的信息置顶显示。失败则提示错误信息,返回当前页面

评分标准:

①完成人口登记页面(未完成0分,完成1分)

②保存人口信息入库(未完成0分,完成1分)

③户别、住房类别、性别实现一个单选框录入0.5分,全部实现1分。(未完成0分,完成一个0.5,全部完成1分)

④本户现住房面积或本户现住房间数实现整数判断(未完成0分,完成0.5分)

⑤判断身份证按照要求录入,实现位数和数字录入判断0.5分,实现最后一位可以输入数字或X判断0.5分。(1分)

⑥受教育程度下拉框录入(未完成0分,完成0.5分)

⑦提交后页面跳转功能;(未完成0分,完成1分)

5)修改人口信息页面:3分)

输入户主姓名,显示其余信息,可对身份证号码、性别、民族、受教育程度进行修改。(身份证号码、性别、受教育程度必须符合录入要求);如果该名户主信息数据库不存在,则提示“该户主信息不存在”。(3分)

评分标准:

①完成修改户主个人信息页面(未完成0分,完成0.5分)

② 实现数据库中信息更新(未完成0分,完成1分)

③修改信息判断是否符合要求。(未完成0分,完成0.5分)

④输入姓名,显示其余信息,若该信息不存在,提示错误信息;(未完成0分,完成1分)

6)删除人口信息页面:录入户主姓名,显示详细信息后,点击“删除”按钮,弹出提示框,提示“是否确认删除该户主信息”,确认后删除该信息。(1分)

评分标准:

①输入户主姓名可显示其余信息。(未完成0分,完成0.5分)

②对应删除数据库中信息(未完成0分,完成0.5分)

7)浏览人口信息页面:(2分)

以列表形式显示人口基本信息,结果列表中显示姓名、性别,民族、受教育程度基本信息,点击姓名,可以跳转到个人详细信息。

①实现以列表形式显示人口基本信息,结果列表中显示姓名、性别,民族、受教育程度基本信息(未完成0分,完成1分)

②实现跳转个人详细信息页面。(未完成0分,完成1分)

8)查询人口信息页面:(3分)

要求可以根据人口姓名、性别、民族、受教育程度四种条件实现模糊查询,输出结果以列表形式显示,显示人口姓名、性别、民族、受教育程度基本信息,点击列表中的姓名,跳转到人口个人详细信息页面。

评分标准:

①缺少一种查询条件扣除1分。(未完成0分,完成3分)

2.2 功能要求

1)设计出合理的数据库和数据表,要求使用mysql、sqlserver、oracle三种数据库中一种(1分)

2)使用Serverlet实现页面交互(1分)。

3)使用Java Bean封装数据库连接操作(1分。)

二、代码

1.yemian.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>第七次全国人口普查登记</title>
</head>
<body>
<h2 align="center">第七次全国人口普查登记</h2>
<a href="login.jsp">信息登记</a><br>
<a href="xiugai01.jsp">信息修改</a><br>
<a href="delete.jsp">信息删除</a><br>
<a href="chaxun.jsp">查询信息</a><br>
<a href="zongliulan.jsp">浏览信息</a><br>
</body>
</html>

2.login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录登记</title>
</head>
<body>
<form method="post"  action="loginServlet">
    <table align="center" border="1" width="600">
        <tr>
            <td>户别</td>
<%--            单选框--%>
            <td>
                <select name="hubie">
                    <option  value="家庭户">家庭户</option>
                    <option  value="集体户">集体户</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>住房类型</td>
            <td>
                <select name="type">
                    <option value="家庭住宅">家庭住宅</option>
                    <option value="集体住所">集体住所</option>
                    <option value="工作地住所">工作地住所</option>
                    <option value="其他住宅">其他住宅</option>
                    <option value="无住宅">无住宅</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>本户现住房面积</td>
            <td>
                <input type="text" name="mianji" ><br>
            </td>
        </tr>
        <tr>
            <td>本户现住房间数</td>
            <td>
                <input type="text" name="num"><br>
            </td>
        </tr>
        <tr>
            <td>户主姓名</td>
            <td>
                <input type="text" name="name"><br>
            </td>
        </tr>
        <tr>
            <td>身份证号码</td>
            <td>
                <input type="text" name="id"><br>
            </td>
        </tr>
        <tr>
            <td>性别</td>
            <td>
                <select name="sex">
                    <option value="男"></option>
                    <option value="女"></option>
                </select>
            </td>
        </tr>
        <tr>
            <td>民族</td>
            <td>
                <input type="text" name="nation">
            </td>
        </tr>
        <tr>
            <td>受教育程度</td>
            <td>
                <select name="educate">
                    <option value="研究生">研究生</option>
                    <option value="大学本科">大学本科</option>
                    <option value="大学专科">大学专科</option>
                    <option value="高中">高中</option>
                    <option value="初中">初中</option>
                    <option value="小学">小学</option>
                    <option value="未上过学">未上过学</option>
                </select>
            </td>
        </tr>
        <tr align="center">
            <td colspan="2">
                <button>提交</button>
            </td>
        </tr>
    </table>
</form>
</body>
</html>

3.loginServlet.java

package com.xxxx.servelt;


import com.xxxx.po.renkou;
import util.DBUtil;

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.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编译格式
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //接收数据
        String hubie = request.getParameter("hubie");
        String type = request.getParameter("type");
        String mianji = request.getParameter("mianji");
        String num = request.getParameter("num");
        String name = request.getParameter("name");
        String id = request.getParameter("id");
        String sex = request.getParameter("sex");
        String nation = request.getParameter("nation");
        String educate = request.getParameter("educate");
        PrintWriter write = response.getWriter();
        if(!isNumeric(mianji)){
            write.write("面积不是整数");
            write.close();
        } else if (!isNumeric(num)) {
            write.write("房间数量不是整数");
            write.close();
        }
        //建立联系
        else {
            PreparedStatement preparedStatement = null;
            Connection connection = null;
            try {
                connection = DBUtil.getConnection();
                String sql = "insert into renkou01(hubie,type,mianji,num,name,id,sex,nation,educate) values(?,?,?,?,?,?,?,?,?)";
                preparedStatement = null;
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, hubie);
                preparedStatement.setString(2, type);
                preparedStatement.setString(3, mianji);
                preparedStatement.setString(4, num);
                preparedStatement.setString(5, name);
                preparedStatement.setString(6, id);
                preparedStatement.setString(7, sex);
                preparedStatement.setString(8, nation);
                preparedStatement.setString(9, educate);
                preparedStatement.executeUpdate();
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            request.getRequestDispatcher("liulan.jsp").forward(request,response);
        }
    }

    private boolean isNumeric(String str) {
        for (int i = str.length();--i>=0;){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }


}

4.liulan.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>--%>
<html>
<head>
    <title>浏览</title>
</head>
<body>

<h2 align="center" >登记成功</h2><br>
<table align="center"  border="1">
    <tr>
        <td>户别</td>
        <td>住房类型</td>
        <td>本户现住房面积</td>
        <td>本户现住房间数</td>
        <td>户主姓名</td>
        <td>身份证号</td>
        <td>性别</td>
        <td>民族</td>
        <td>受教育程度</td>
    </tr>


<%

    Connection connection= DBUtil.getConnection();
    PreparedStatement preparedStatement=null;
    ResultSet in=null;
    try{
//        按照添加时间排序::
        preparedStatement=connection.prepareStatement("select * from renkou01");
        in = preparedStatement.executeQuery();
        while(in.next()){
%>
<tr>
    <td><%=in.getString(1)%></td>
    <td><%=in.getString(2)%></td>
    <td><%=in.getString(3)%></td>
    <td><%=in.getString(4)%></td>
    <td><%=in.getString(5)%></td>
    <td><%=in.getString(6)%></td>
    <td><%=in.getString(7)%></td>
    <td><%=in.getString(8)%></td>
    <td><%=in.getString(9)%></td>
</tr>
<%
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }finally {
        connection.close();
        preparedStatement.close();
        in.close();
        }
%>
</table>
</body>
</html>

5.xiugai01.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>信息修改</title>
</head>
<body>
<form method="post" action="xiugaiServlet01">
    户主姓名:<input type="text" name="name"><br>
    <button>查询户主信息</button>
</form>
</body>
</html>

6.xiugaiServlet01.java

package com.xxxx.servelt;

import util.DBUtil;

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.*;

@WebServlet("/xiugaiServlet01")
public class xiugaiServlet01 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //获取jsp 中的 name
        String name = request.getParameter("name");
        //链接
        try {
            Connection connection = DBUtil.getConnection();
            String sql = "select * from renkou01 where name=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, name);
            ResultSet rs = preparedStatement.executeQuery();
            int i = 0;
            if (rs.next()) {
                i = 1;
                request.setAttribute("hubie", rs.getString("hubie"));
                request.setAttribute("type", rs.getString("type"));
                request.setAttribute("mianji", rs.getString("mianji"));
                request.setAttribute("num", rs.getString("num"));
                request.setAttribute("name", rs.getString("name"));
                request.setAttribute("id", rs.getString("id"));
                request.setAttribute("sex", rs.getString("sex"));
                request.setAttribute("nation", rs.getString("nation"));
                request.setAttribute("educate", rs.getString("educate"));
                rs.close();
                preparedStatement.close();
                connection.close();
            }
            if(i==1){
                request.getRequestDispatcher("xiugai02.jsp").forward(request,response);
            }
            else{
                PrintWriter write= response.getWriter();
                write.write("该户主信息不存在");
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}

7.xiugai02.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改页面户主信息显示</title>
</head>
<body>
<%
    String name = (String) request.getAttribute("name");
    session.setAttribute("name",name);
%>
<form method="post" action="xiugaiServlet02">
<table align="center" border="1">
    <tr>
    <td>户别:</td>
    <td>${hubie}</td>
    <td>
        修改:
        <select name="hubie">
            <option value="家庭户">家庭户</option>
            <option value="集体户">集体户</option>
        </select>
    </td>
</tr>
    <tr>
        <td>住房类型</td>
        <td>${type}</td>
        <td>修改:
            <select name="type">
                <option value="家庭住宅">家庭住宅</option>
                <option value="集体住所">集体住所</option>
                <option value="工作地住所">工作地住所</option>
                <option value="其他住宅">其他住宅</option>
                <option value="无住宅">无住宅</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>本户现住房面积</td>
        <td>${mianji}</td>
        <td>修改:
            <input type="text" name="mianji" ><br>
        </td>
    </tr>
    <tr>
        <td>本户现住房间数</td>
        <td>${num}</td>
        <td>修改:
            <input type="text" name="num"><br>
        </td>
    </tr>
    <tr>
        <td>户主姓名</td>
        <td>${name}</td>
        <td>
            ${name}
        </td>
    </tr>
<%--    --%>
    <tr>
        <td>身份证号码</td>
        <td>${id}</td>
        <td>
            ${id}
        </td>
    </tr>
    <tr>
        <td>性别</td>
        <td>${sex}</td>
        <td>
            ${sex}
        </td>
    </tr>
    <tr>
        <td>民族</td>
        <td>${nation}</td>
        <td>修改:
            <input type="text" name="nation">
        </td>
    </tr>
    <tr>
        <td>受教育程度</td>
        <td>${educate}</td>
        <td>修改:
            <select name="educate">
                <option value="研究生">研究生</option>
                <option value="大学本科">大学本科</option>
                <option value="大学专科">大学专科</option>
                <option value="高中">高中</option>
                <option value="初中">初中</option>
                <option value="小学">小学</option>
                <option value="未上过学">未上过学</option>
            </select>
        </td>
    </tr>
    <tr align="center">
        <td colspan="3"><button>保存信息</button></td>
    </tr>
</table>
</form>
</body>
</html>

8.xiugaiServlet02.java

package com.xxxx.servelt;

import util.DBUtil;

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.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/xiugaiServlet02")
public class xiugaiServlet02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //从jsp接收数据
//        String name = request.getParameter("name");
//        System.out.println(name);
        String name = (String) request.getSession().getAttribute("name");
        System.out.println(name);
        String hubie = request.getParameter("hubie");
        String type = request.getParameter("type");
        String mianji = request.getParameter("mianji");
        String num = request.getParameter("num");
//        String id = request.getParameter("id");
//        String sex = request.getParameter("sex");
        String nation = request.getParameter("nation");
        String educate = request.getParameter("educate");
        PrintWriter writer=response.getWriter();
        //连接
        try {
            Connection connection = DBUtil.getConnection();
            String sql="update renkou01 set hubie=?,type=?,mianji=?,num=?,nation=?,educate=? where name=?";
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,hubie);
            preparedStatement.setString(2,type);
            preparedStatement.setString(3,mianji);
            preparedStatement.setString(4,num);
            preparedStatement.setString(7,name);
//            preparedStatement.setString(6,id);
//            preparedStatement.setString(5,sex);
            preparedStatement.setString(5,nation);
            preparedStatement.setString(6,educate);
            int rs=preparedStatement.executeUpdate();
            if(rs>0){
                writer.write("修改成功");
            }
            else{
                writer.write("修改失败");
            }
            preparedStatement.close();
            connection.close();
            writer.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

9.delete.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>信息删除页面</title>
</head>
<body>
<form method="post" action="deleteServlet">
    <table align="center">
        <tr>
        姓名:<input name="name" type="text">
        </tr>
        <tr align="center">
            <button>查询户主信息</button>
        </tr>
    </table>
</form>
</table>
</body>
</html>

10.deleteServlet.java

package com.xxxx.servelt;

import util.DBUtil;

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.*;


@WebServlet("/deleteServlet")
public class deleteServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String name = request.getParameter("name");
        PrintWriter writer = response.getWriter();
        try {
            Connection connection=DBUtil.getConnection();
            String sql = "delete from renkou01 where name=?";
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            //对应问号
            preparedStatement.setString(1,name);
            int rs = preparedStatement.executeUpdate();
            if(rs>0){
//                System.out.println("删除成功");
                writer.write("删除成功");

            }else{
//                System.out.println("删除失败");
                writer.write("删除失败");
            }
            preparedStatement.close();
            connection.close();
            writer.close();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}

11.chaxun.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>查询信息</title>
</head>
<body>
<form method="post" action="chaxunServlet">
    <table>
        <tr>
            <td>户主姓名:</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
            <td>性别:</td>
            <td><input type="text" name="sex"> </td>
        </tr>
        <tr>
            <td>民族:</td>
            <td><input type="text" name="nation"></td>
        </tr>
        <tr>
            <td>受教育程度:</td>
            <td><input name="educate" type="text"> </td>
        </tr>
        <tr align="center">
            <td colspan="2"><button>查询</button></td>
        </tr>
    </table>

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

12.chaxunServlet.java

package com.xxxx.servelt;


import util.DBUtil;

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.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/chaxunServlet")
public class chaxunServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //获取参数
        String name=request.getParameter("name");
        String sex = request.getParameter("sex");
        String nation=request.getParameter("nation");
        String educate=request.getParameter("educate");
        //连接
        try {
            Connection connection = DBUtil.getConnection();
            String sql = "select * from renkou01 where name like ?";
//            String sql = "select * from renkou01 where name like '%"+name+"%'";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"%"+name+"%");
            ResultSet rs= preparedStatement.executeQuery();
            while(rs.next()){
                request.setAttribute("hubie",rs.getString("hubie"));
                request.setAttribute("type",rs.getString("type"));
                request.setAttribute("mianji",rs.getString("mianji"));
                request.setAttribute("num",rs.getString("num"));
                request.setAttribute("name",rs.getString("name"));
                request.setAttribute("id",rs.getString("id"));
                request.setAttribute("sex",rs.getString("sex"));
                request.setAttribute("nation",rs.getString("nation"));
                request.setAttribute("educate",rs.getString("educate"));
                request.getRequestDispatcher("chaxun02.jsp").forward(request,response);
                rs.close();
                preparedStatement.close();
                connection.close();
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}

13.chaxun02.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>查询结果</title>
</head>
<body>
<table align="center"  border="1">
    <tr>
        <td>户别</td>
        <td>住房类型</td>
        <td>本户现住房面积</td>
        <td>本户现住房间数</td>
        <td>户主姓名</td>
        <td>身份证号</td>
        <td>性别</td>
        <td>民族</td>
        <td>受教育程度</td>
    </tr>
    <tr>
        <td>${hubie}</td>
        <td>${type}</td>
        <td>${mianji}</td>
        <td>${num}</td>
        <td>${name}</td>
        <td>${id}</td>
        <td>${sex}</td>
        <td>${nation}</td>
        <td>${educate}</td>
    </tr>
</table>
</body>
</html>

14.zongliulan.jsp

<%@ page import="java.sql.Connection" %>
<%@ page import="util.DBUtil" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %><%--
  Created by IntelliJ IDEA.
  User: 22466
  Date: 2022/10/18
  Time: 20:23
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>浏览页面</title>
</head>
<body>
<table align="center"  border="1">
    <tr>
        <td>户别</td>
        <td>住房类型</td>
        <td>本户现住房面积</td>
        <td>本户现住房间数</td>
        <td>户主姓名</td>
        <td>身份证号</td>
        <td>性别</td>
        <td>民族</td>
        <td>受教育程度</td>
    </tr>


        <%
    //浏览
    //链接
//    Connection connection = null;
//    //数据库操作对象
//    PreparedStatement preparedStatement=null;
//    //查询结果
//    ResultSet resultSet=null;
//
    Connection connection= DBUtil.getConnection();
    PreparedStatement preparedStatement=null;
    ResultSet in=null;
    try{
//        按照添加时间排序::
        preparedStatement=connection.prepareStatement("select * from renkou01");
        in = preparedStatement.executeQuery();
        while(in.next()){
%>
    <tr>
        <td><%=in.getString(1)%></td>
        <td><%=in.getString(2)%></td>
        <td><%=in.getString(3)%></td>
        <td><%=in.getString(4)%></td>
        <td><%=in.getString(5)%></td>
        <td><%=in.getString(6)%></td>
        <td><%=in.getString(7)%></td>
        <td><%=in.getString(8)%></td>
        <td><%=in.getString(9)%></td>
    </tr>
        <%
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }finally {
        connection.close();
        preparedStatement.close();
        in.close();
        }
%>
</body>
</html>

15.DBUtil.java

package util;

        import java.sql.*;

public class DBUtil{
    public static Connection getConnection() throws ClassNotFoundException, SQLException {

        Connection connection = null;//连接数据库
        Statement stmt = null;//Statement 对象用于将 SQL 语句发送到数据库中。
        ResultSet rs = null;
        //1. 导入驱动jar包
        //2.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
//        好像要设置时区??
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/course ?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT", "root", "123456");

        return connection;
    }

    public static void close(Connection connection) {
        try {
            if (connection != null) {
                connection.close();
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void close(PreparedStatement preparedStatement) {
        try {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void close(ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

三、wenti

页面之间比较混乱,代码重复比较多,还有很多小问题。

笔记1 - yuanse - 博客园 (cnblogs.com)

idea实现对数据库的增删改查功能_青春要不限量的博客-CSDN博客_idea增删改查

posted on 2022-10-19 09:51  201812  阅读(160)  评论(0编辑  收藏  举报