• 利用JDBC及Servlet实现对信息录入注册功能的实现


    利用JDBC及Servlet实现对登录注册功能的实现;

    1.题目要求:

        1、新建一个数据库名为(个人姓名拼音),表(学生所在城市),字段(sid:学号,sname:姓名,sage:年龄,scontent:简介)

        2、实现一个注册页面register.jsp,需包含以上信息的输入

        3、利用Servlet实现对数据的接受及JDBC的保存

        4、实现一个显示页面show.jsp。从数据库中返回以上所有数据(或EL表达式)

    2.实施效果

    3.实施代码

    register.jsp

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <html>
    3. <head>
    4. <link rel="stylesheet" type="text/css" href="styles.css">
    5. <title>信息录入</title>
    6. </head>
    7. <body>
    8. <%--<h1>学生信息注册</h1>--%>
    9. <form action="${pageContext.request.contextPath}/register" method="post">
    10. <label>学号:</label>
    11. <input type="text" name="sid"><br>
    12. <label>姓名:</label>
    13. <input type="text" name="sname"><br>
    14. <label>年龄:</label>
    15. <input type="text" name="sage"><br>
    16. <label>简介:</label>
    17. <input type="text" name="scontent"><br>
    18. <input type="submit" value="注册">
    19. </form>
    20. </body>
    21. </html>

    style.css

    1. body {
    2. display: flex;
    3. justify-content: center;
    4. align-items: center;
    5. height: 100vh;
    6. }
    7. h1 {
    8. float: top;
    9. text-align: center;
    10. }
    11. form {
    12. display: flex;
    13. flex-direction: column;
    14. align-items: center;
    15. width: 300px;
    16. padding: 20px;
    17. border: 1px solid #ddd;
    18. border-radius: 5px;
    19. }
    20. input[type=text]{
    21. width: 100%;
    22. padding: 12px 20px;
    23. margin: 8px 0;
    24. border: 1px solid #ccc;
    25. box-sizing: border-box;
    26. }
    27. input[type=submit] {
    28. background-color: #4CAF50;
    29. color: white;
    30. padding: 14px 20px;
    31. margin: 8px 0;
    32. border: none;
    33. cursor: pointer;
    34. width: 100%;
    35. }
    36. input[type=submit]:hover {
    37. background-color: #45a049;
    38. }
    39. .error {
    40. color: red;
    41. }
    42. .success {
    43. color: green;
    44. }

    show.jsp

    1. <%@ page import="java.sql.*" %><%--
    2. Created by IntelliJ IDEA.
    3. User: ALASIJIA
    4. Date: 2023/11/16
    5. Time: 11:29
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%--<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>--%>
    9. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    10. <html>
    11. <head>
    12. <title>信息展示</title>
    13. <style>
    14. body {
    15. font-family: Arial, sans-serif;
    16. }
    17. h1 {
    18. color: #333;
    19. }
    20. table {
    21. width: 100%;
    22. border-collapse: collapse;
    23. }
    24. th, td {
    25. border: 1px solid #333;
    26. padding: 8px;
    27. }
    28. th {
    29. background-color: #f2f2f2;
    30. }
    31. </style>
    32. </head>
    33. <body>
    34. <h1>学生信息</h1>
    35. <table>
    36. <tr>
    37. <th>学号</th>
    38. <th>姓名</th>
    39. <th>年龄</th>
    40. <th>简介</th>
    41. </tr>
    42. <%
    43. // 数据库连接信息
    44. String url = "jdbc:mysql://localhost:3307/person_info?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
    45. String username = "root";
    46. String password = "123456";
    47. // 建立数据库连接
    48. Connection con = null;
    49. Statement stmt = null;
    50. ResultSet rs = null;
    51. try {
    52. Class.forName("com.mysql.cj.jdbc.Driver");
    53. con = DriverManager.getConnection(url, username, password);
    54. // 查询学生信息
    55. stmt = con.createStatement();
    56. rs = stmt.executeQuery("SELECT * FROM stu_city");
    57. // 遍历结果集并显示学生信息
    58. while (rs.next()) {
    59. String sid = rs.getString("sid");
    60. String sname = rs.getString("sname");
    61. String sage = rs.getString("sage");
    62. String scontent = rs.getString("scontent");
    63. out.println("");
    64. out.println("" + sid + "");
    65. out.println("" + sname + "");
    66. out.println("" + sage + "");
    67. out.println("" + scontent + "");
    68. out.println("");
    69. }
    70. } catch (Exception e) {
    71. e.printStackTrace();
    72. } finally {
    73. // 关闭数据库连接和资源
    74. if (rs != null) {
    75. try {
    76. rs.close();
    77. } catch (SQLException e) {
    78. e.printStackTrace();
    79. }
    80. }
    81. if (stmt != null) {
    82. try {
    83. stmt.close();
    84. } catch (SQLException e) {
    85. e.printStackTrace();
    86. }
    87. }
    88. if (con != null) {
    89. try {
    90. con.close();
    91. } catch (SQLException e) {
    92. e.printStackTrace();
    93. }
    94. }
    95. }
    96. %>
    97. </table>
    98. </body>
    99. </html>

    JDBCServlet

    1. package com.hjj.servlet.hw10;
    2. import javax.servlet.ServletException;
    3. import javax.servlet.annotation.WebServlet;
    4. import javax.servlet.http.HttpServlet;
    5. import javax.servlet.http.HttpServletRequest;
    6. import javax.servlet.http.HttpServletResponse;
    7. import java.io.IOException;
    8. import java.sql.Connection;
    9. import java.sql.DriverManager;
    10. import java.sql.PreparedStatement;
    11. import java.sql.SQLException;
    12. import java.util.TimeZone;
    13. /**
    14. * @author:嘉佳 Date:2023/11/16 10:46
    15. **/
    16. @WebServlet("/register")
    17. public class JDBCServlet extends HttpServlet {
    18. @Override
    19. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    20. req.setCharacterEncoding("UTF-8");
    21. String sid = req.getParameter("sid");
    22. String sname = req.getParameter("sname");
    23. String sage = req.getParameter("sage");
    24. String scontent = req.getParameter("scontent");
    25. Connection connection=null;
    26. PreparedStatement stmt=null;
    27. String url="jdbc:mysql://localhost:3307/person_info?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC";
    28. String user="root";
    29. String password="123456";
    30. try {
    31. Class.forName("com.mysql.cj.jdbc.Driver");
    32. connection = DriverManager.getConnection(url, user, password);
    33. String sql = "INSERT INTO stu_city (sid, sname, sage, scontent) VALUES (?, ?, ?, ?)";
    34. stmt=connection.prepareStatement(sql);
    35. stmt.setInt(1, Integer.parseInt(sid));
    36. stmt.setString(2,sname);
    37. stmt.setInt(3, Integer.parseInt(sage));
    38. stmt.setString(4,scontent);
    39. // System.out.println(stmt);
    40. stmt.executeUpdate();
    41. // if (rs>0)
    42. // resp.sendRedirect("show.jsp");
    43. resp.sendRedirect("hw10/show.jsp");
    44. } catch (ClassNotFoundException | SQLException e) {
    45. e.printStackTrace();
    46. }finally {
    47. if(stmt!=null){
    48. try {
    49. stmt.close();
    50. } catch (SQLException e) {
    51. e.printStackTrace();
    52. }
    53. }
    54. if (connection!=null) {
    55. try {
    56. connection.close();
    57. } catch (SQLException e) {
    58. e.printStackTrace();
    59. }
    60. }
    61. }
    62. }
    63. }

  • 相关阅读:
    2023年:哪些Trello的替代品值得关注?
    postman做接口测试
    Mybatis--动态SQL
    一文看懂推荐系统:排序06:粗排三塔模型,性能介于双塔模型和精排模型之间
    排序算法-堆排序和TopK算法
    手把手教你搭建zookeeper和kafka集群(超级详细)
    以太坊源码笔记-blockchain
    JTS: 12 Descriptions 图形覆盖
    【踩坑及思考】浏览器存储 cookie 最大值超过 4kb,或 http 头 cookie 超过限制值
    LeetCode //C - 77. Combinations
  • 原文地址:https://blog.csdn.net/GANTENJ/article/details/134510091