web实现增删改查的方式有很多啊,对于初学者来说当然是要先了解各部分的传值的方式.本篇博客从jsp技术的最基础方面进行说明.

一、什么是jsp技术

  首先,我们要了解什么是jsp技术.

  jsp技术是基于JAVA语言的动态网页技术,通常使用HTML语言来设计和格式化静态页面内容,然后通过jsp标签来实现动态部分,也就是数据间的互交.在这里推荐一篇博客来供初学者进行学习.

  山河永慕~"的博客:Jsp技术总结

二、代码实现

  1.创建数据库表

  创建一下数据库表,数据库名Student,表名student.

  

  2.创建web项目,项目名为Student并建立以下jsp页面

  1)主页面 index.jsp

  1. 1 <%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%>
  2. 2 <%@ page errorPage="error.jsp"%>
  3. 3 <html>
  4. 4 <head>
  5. 5 <title>学生管理系统</title>
  6. 6 <link rel="stylesheet" type="text/css" href="css/style.css">
  7. 7 </head>
  8. 8 <body>
  9. 9 <div align="center">
  10. 10 <p>学生管理系统</p>
  11. 11 <a href="add.jsp">添加学生信息</a> <br /> <br />
  12. 12 <table style="width: 50%;">
  13. 13 <tr>
  14. 14 <th>学号</th>
  15. 15 <th>姓名</th>
  16. 16 <th>性别</th>
  17. 17 <th>出生日期</th>
  18. 18 <th>家庭住址</th>
  19. 19 <th>操&nbsp;&nbsp;作</th>
  20. 20 </tr>
  21. 21 <%
  22. 22 try {
  23. 23 Class.forName("com.mysql.jdbc.Driver");
  24. 24 Connection con = DriverManager.getConnection(
  25. 25 "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root",
  26. 26 "root");
  27. 27 //使用Statement对象
  28. 28 Statement stmt = con.createStatement();
  29. 29 String s = "select * from student";
  30. 30 ResultSet rs = stmt.executeQuery(s);
  31. 31
  32. 32 while (rs.next()) {
  33. 33 int id = rs.getInt(1);
  34. 34 out.println("<tr><td>" + rs.getString(1) + "</td><td>" + rs.getString(2) + "</td><td>"
  35. 35 + rs.getString(3) + "</td><td>" + rs.getString(4) + "</td><td>" + rs.getString(5)
  36. 36 + "</td><td><a href='edit.jsp?id=" + id
  37. 37 + "'>修改</a>&nbsp;<a onclick='check()' href='del.jsp?id=" + id + "'>删除</a></td></tr>");
  38. 38 }
  39. 39 rs.close();
  40. 40 stmt.close();
  41. 41 con.close();
  42. 42 } catch (Exception e) {
  43. 43 out.println("Exception:" + e.getMessage());
  44. 44 }
  45. 45 %>
  46. 46
  47. 47 </table>
  48. 48 <br />
  49. 49 </div>
  50. 50 <script>
  51. 51 function check() {
  52. 52 if (confirm("确认执行此操作吗?")) {
  53. 53 return true;
  54. 54 } else {
  55. 55 return false;
  56. 56 }
  57. 57 }
  58. 58 </script>
  59. 59 </body>
  60. 60 </html>

  这里采用JDBC来连接数据库,并指定字符编码与解码方式为utf-8.并通过遍历表student的数据将每一行的每一列数据依次out.printf输出到table中.设计修改、操作的操作按钮传值id进行相应操作.

  其中,script中为javascript脚本语言,用来限制删除的操作,防止用户误删.

  2)添加操作

  add.jsp

  1. 1 <%@ page contentType="text/html; charset=utf-8" import="java.sql.*"
  2. 2 errorPage="error.jsp"%>
  3. 3 <html>
  4. 4 <head>
  5. 5 <title>添学生信息</title>
  6. 6 <link rel="stylesheet" type="text/css" href="css/style.css">
  7. 7 </head>
  8. 8 <body>
  9. 9 <div align="center">
  10. 10 <p>添加成员信息</p>
  11. 11 <form action="addsave.jsp" method="post">
  12. 12 <table style="width: 50%">
  13. 13 <tr>
  14. 14 <th width="50%">学号:</th>
  15. 15 <td width="50%"><input name="id" type="text" required
  16. 16 oninvalid="setCustomValidity('学号是必填项')"
  17. 17 oninput="setCustomValidity('')"></td>
  18. 18 </tr>
  19. 19 <tr>
  20. 20 <th>姓名:</th>
  21. 21 <td><input name="name" type="text" required
  22. 22 oninvalid="setCustomValidity('姓名是必填项')"
  23. 23 oninput="setCustomValidity('')"></td>
  24. 24 </tr>
  25. 25 <tr>
  26. 26 <th>性别:</th>
  27. 27 <td><select name="sex">
  28. 28 <option value="男">男</option>
  29. 29 <option value="女">女</option>
  30. 30 </select></td>
  31. 31 </tr>
  32. 32 <tr>
  33. 33 <th>出生日期:</th>
  34. 34 <td><input name="birthday" type="text" required
  35. 35 oninvalid="setCustomValidity('出生日期是必填项')"
  36. 36 oninput="setCustomValidity('')"></td>
  37. 37 </tr>
  38. 38 <tr>
  39. 39 <th>家庭住址:</th>
  40. 40 <td><input name="address" type="text" required
  41. 41 oninvalid="setCustomValidity('家庭居住地址是必填项')"
  42. 42 oninput="setCustomValidity('')"></td>
  43. 43 </tr>
  44. 44 </table>
  45. 45 <button type="submit" name="submit" value="添加">添&nbsp;加</button>
  46. 46 <button type="reset" value="重置">重&nbsp;置</button>
  47. 47 </form>
  48. 48 </div>
  49. 49 </body>
  50. 50 </html>

  设计添加页面,通过form表单传值到addsave.jsp

  addsave.jsp

  addsave.jsp进行数据的获取并将数据添加到数据库.因为传值的时候是以String的形式传输的,所以对于id要进行类型转换 1 int id = Integer.parseInt(request.getParameter("id")); .

  1. 1 <%@ page contentType="text/html; charset=utf-8" import="java.sql.*"
  2. 2 errorPage="error.jsp"%>
  3. 3 <html>
  4. 4 <head>
  5. 5 <title>添加学生信息</title>
  6. 6 <link rel="stylesheet" type="text/css" href="css/style.css">
  7. 7 </head>
  8. 8 <body>
  9. 9 <%
  10. 10 request.setCharacterEncoding("utf-8");
  11. 11 String submit = request.getParameter("submit");
  12. 12
  13. 13 int id = Integer.parseInt(request.getParameter("id"));
  14. 14 String name = request.getParameter("name");
  15. 15 String sex = request.getParameter("sex");
  16. 16 String birthday = request.getParameter("birthday");
  17. 17 String address = request.getParameter("address");
  18. 18 Class.forName("com.mysql.jdbc.Driver");
  19. 19 Connection con = DriverManager.getConnection(
  20. 20 "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root", "root");
  21. 21 //使用Statement对象
  22. 22 Statement stmt = con.createStatement();
  23. 23 String sql = "insert into student(id,name,sex,birthday,address) values('" + id + "','" + name + "','" + sex
  24. 24 + "','" + birthday + "'," + address + ")";
  25. 25 int i = stmt.executeUpdate(sql);
  26. 26
  27. 27 if (i == 1) {
  28. 28 %>
  29. 29
  30. 30 <script type="text/javascript">
  31. 31 alert("学生信息添加成功");
  32. 32 window.location.href = "index.jsp";
  33. 33 </script>
  34. 34 <%
  35. 35 } else {
  36. 36 %>
  37. 37 <script type="text/javascript">
  38. 38 alert("学生信息添加失败");
  39. 39 window.location.href = "index.jsp";
  40. 40 </script>
  41. 41 <%
  42. 42 }
  43. 43 stmt.close();
  44. 44 con.close();
  45. 45 %>
  46. 46 </body>
  47. 47 </html>

  3)修改操作

  edit.jsp

  1. 1 <%@ page import="java.sql.*" pageEncoding="utf-8" errorPage="error.jsp"%>
  2. 2 <html>
  3. 3 <head>
  4. 4 <title>修改学生信息</title>
  5. 5 <link rel="stylesheet" type="text/css" href="css/style.css">
  6. 6 </head>
  7. 7 <body>
  8. 8 <%
  9. 9 request.setCharacterEncoding("utf-8");
  10. 10 Class.forName("com.mysql.jdbc.Driver");
  11. 11 Connection con = DriverManager.getConnection(
  12. 12 "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root", "20173673");
  13. 13 Statement stmt = con.createStatement();
  14. 14 String id = request.getParameter("id");
  15. 15 ResultSet rs = stmt.executeQuery("select * from student where id=" + id);
  16. 16 rs.next();
  17. 17 %>
  18. 18 <div align="center">
  19. 19 <form action="editsave.jsp" method="post">
  20. 20 <h2>修改学生信息</h2>
  21. 21 <table style="width: 50%">
  22. 22 <tr>
  23. 23 <th width="50%">学号:</th>
  24. 24 <td width="50%"><input name="id" type="text"
  25. 25 value="<%=rs.getString(1)%>" disabled="disabled"></td>
  26. 26 </tr>
  27. 27 <tr>
  28. 28 <th>姓名:</th>
  29. 29 <td><input name="name" type="text"
  30. 30 value="<%=rs.getString(2)%>"></td>
  31. 31 </tr>
  32. 32 <tr>
  33. 33 <th>性别:</th>
  34. 34 <td><select name="sex">
  35. 35 <option value="<%=rs.getString(3)%>"><%=rs.getString(3)%>
  36. 36 <option value="男">男</option>
  37. 37 <option value="女">女</option>
  38. 38 </select>
  39. 39 </tr>
  40. 40 <tr>
  41. 41 <th>出生日期:</th>
  42. 42 <td><input name="birthday" type="text"
  43. 43 value="<%=rs.getString(4)%>"></td>
  44. 44 </tr>
  45. 45 <tr>
  46. 46 <th>家庭住址:</th>
  47. 47 <td><input name="address" type="text"
  48. 48 value="<%=rs.getString(5)%>"></td>
  49. 49 </tr>
  50. 50 </table>
  51. 51 <input type="hidden" name="id" value="<%=id%>">
  52. 52 <button type="submit" name="submit" value="修改">修&nbsp;改</button>
  53. 53 <button type="reset" value="重置">重&nbsp;置</button>
  54. 54 </form>
  55. 55 </div>
  56. 56 <%
  57. 57 rs.close();
  58. 58 stmt.close();
  59. 59 con.close();
  60. 60 %>
  61. 61
  62. 62 </body>
  63. 63 </html>

  通过id获取要修改的内容,显示到table的input输入框中,进行修改.然后将值传输到editsave.jsp中,进行信息修改的操作.

  editsave.jsp

  1. 1 <%@ page import="java.sql.*" pageEncoding="utf-8" errorPage="error.jsp"%>
  2. 2 <html>
  3. 3 <head>
  4. 4 <title>修改完成</title>
  5. 5 <link rel="stylesheet" type="text/css" href="css/style.css">
  6. 6 </head>
  7. 7 <body>
  8. 8 <%
  9. 9 request.setCharacterEncoding("utf-8");
  10. 10 int id = Integer.parseInt(request.getParameter("id"));
  11. 11 String name = request.getParameter("name");
  12. 12 String sex = request.getParameter("sex");
  13. 13 String birthday = request.getParameter("birthday");
  14. 14 String address = request.getParameter("address");
  15. 15 Class.forName("com.mysql.jdbc.Driver");
  16. 16 Connection con = DriverManager.getConnection(
  17. 17 "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root", "root");
  18. 18 Statement stmt = con.createStatement();
  19. 19 String sql = "update student set id = '" + id + "',name='" + name + "',sex = '" + sex + "',birthday='"
  20. 20 + birthday + "', address = '" + address + "'where id=" + id;
  21. 21
  22. 22 int i = stmt.executeUpdate(sql);
  23. 23 if (i == 1) {
  24. 24 %>
  25. 25
  26. 26 <script type="text/javascript">
  27. 27 alert("信息修改成功");
  28. 28 window.location.href = "index.jsp";
  29. 29 </script>
  30. 30 <%
  31. 31 } else {
  32. 32 %>
  33. 33 <script type="text/javascript">
  34. 34 alert("信息修改失败");
  35. 35 window.location.href = 'edit.jsp?id='
  36. 36 " + id + ";
  37. 37 </script>
  38. 38
  39. 39 <%
  40. 40 }
  41. 41 stmt.close();
  42. 42 con.close();
  43. 43 %>
  44. 44 </body>
  45. 45 </html>

  4)删除操作

  delete.jsp

  1. 1 <%@ page contentType="text/html; charset=gbk" language="java"
  2. 2 import="java.sql.*" pageEncoding="utf-8"%>
  3. 3 <html>
  4. 4 <head>
  5. 5 <title>删除学生信息</title>
  6. 6 <link rel="stylesheet" type="text/css" href="css/style.css">
  7. 7 </head>
  8. 8 <body>
  9. 9 <%
  10. 10 request.setCharacterEncoding("gbk");
  11. 11 Class.forName("com.mysql.jdbc.Driver");
  12. 12 Connection con = DriverManager.getConnection(
  13. 13 "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root", "root");
  14. 14 Statement stmt = con.createStatement();
  15. 15 String id = request.getParameter("id");
  16. 16 int i = stmt.executeUpdate("delete from student where id=" + id);
  17. 17 if (i == 1) {
  18. 18 %>
  19. 19 <script type="text/javascript">
  20. 20 alert("学生信息删除成功!");
  21. 21 window.location.href = "index.jsp";
  22. 22 </script>
  23. 23 <%
  24. 24 } else {
  25. 25 %>
  26. 26 <script type="text/javascript">
  27. 27 alert("学生信息删除失败");
  28. 28 window.location.href = "index.jsp";
  29. 29 </script>
  30. 30 <%
  31. 31 }
  32. 32 con.close();
  33. 33 stmt.close();
  34. 34 %>
  35. 35 </body>
  36. 36 </html>

  

三、效果截图

  index

  add

  edit

  大体就是这样啦,有什么不对的希望大家多多指正.

学生信息管理系统--基于jsp技术和MySQL的简单增删改查的更多相关文章

  1. Node+Express+MySql实现简单增删改查和登录

    var express = require('express'); var mysql = require('mysql'); var app = express(); var bodyParser ...

  2. shell编程系列22--shell操作数据库实战之shell脚本与MySQL数据库交互(增删改查)

    shell编程系列22--shell操作数据库实战之shell脚本与MySQL数据库交互(增删改查) Shell脚本与MySQL数据库交互(增删改查) # 环境准备:安装mariadb 数据库 [ro ...

  3. java jdbc 连接mysql数据库 实现增删改查

    好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打 ...

  4. 手撸Mysql原生语句--增删改查

    mysql数据库的增删改查有以下的几种的情况, 1.DDL语句 数据库定义语言: 数据库.表.视图.索引.存储过程,例如CREATE DROP ALTER SHOW 2.DML语句 数据库操纵语言: ...

  5. 【转载】通过JDBC对MySQL数据库的增删改查

    通过JDBC进行简单的增删改查(以MySQL为例) 目录 前言:什么是JDBC 一.准备工作(一):MySQL安装配置和基础学习 二.准备工作(二):下载数据库对应的jar包并导入 三.JDBC基本操 ...

  6. java数据库(MySQL)之增删改查

    1.查询数据 先救从简单的来吧,之前我们实现了将数据库表格信息读取到一个List集合中,数据库的查询,实 际上就是对这个集合的查询: public class Show { public static ...

  7. PHP+Mysql 实现数据库增删改查

    PHP和Mysql可以对数据库进行简单的增删改查,本文介绍了新闻列表的后台管理. Mysql数据库创建 创建一个新闻列表的数据库: 1. 查询数据库 1.1. 创建文件dbconfig.php,保存常 ...

  8. SpringBoot+Mybatis+Maven+MySQL逆向工程实现增删改查

    SpringBoot+Mybatis+MySQL+MAVEN逆向工程实现增删改查 这两天简单学习了下SpringBoot,发现这玩意配置起来是真的方便,相比于SpringMVC+Spring的配置简直 ...

  9. JSP+Servlet+JavaBean实现数据库的增删改查

    基本思想:JSP文件显示页面,使用form或href超链接传值到Servlet中方法,在Servlet方法中调用Dao层的类对象,实现对数据库里的数据的增删改查,之后重新返回到JSP输出操作完的结果. ...

随机推荐

  1. 策略模式干掉if-else,switch

    1.传统if -else 写法 String nodeModelStr = ""; if (nodeType == NodeType.START){ StartModel star ...

  2. 使用Keil语言的嵌入式C编程教程(上)

    使用Keil语言的嵌入式C编程教程(上) Embedded C Programming Tutorial with Keil Language Embedded System 嵌入式系统是指以单片机为 ...

  3. https ssl(tls)为什么不直接用公钥加密数据?

    很多人都提到了非对称加密速度慢,但这只是一个原因,但不是主要原因,甚至是微不足道的原因. SSL协议到3.0后就已经到头了,取而代之的是TLS,相较于SSL的"安全套接字层"的命名 ...

  4. python----日志模块loggin的使用,按日志级别分类写入文件

    1.日志的级别 日志一共分为5个等级,从低到高分别是: 级别 说明 DEBUG 输出详细的运行情况,主要用于调试. INFO 确认一切按预期运行,一般用于输出重要运行情况. WARNING 系统运行时 ...

  5. 即时性能分析工具 Pyroscope

    当网站上线后,流量增加或短暂功能故障,都会造成使用者体验相当不好,而这时该怎么快速找到性能的瓶颈呢?通常 CPU 达到 100% 时,有时候也很难复制及找出关键问题点. 本篇文章,我们会介绍一套工具叫 ...

  6. 手写Spring Config,最终一战,来瞅瞅撒!

    上一篇说到了手写Spring AOP,来进行功能的增强,下面本篇内容主要是手写Spring Config.通过配置的方式来使用Spring 前面内容链接: 我自横刀向天笑,手写Spring IOC容器 ...

  7. typescript 中的 infer 关键字的理解

    infer 这个关键字,整理记录一下,避免后面忘记了.有点难以理解呢. infer infer 是在 typescript 2.8中新增的关键字. infer 可以在 extends 条件类型的字句中 ...

  8. Java-学习日记(Atomic,Volatile)

    很早之前在公司就看到了atomicInteger,atomicLong这些变量了,一直不明白是什么意思,今天花了点时间了解下. volatile: 先从volatile开始讲起,volatile是多线 ...

  9. .NET Core/.NET5/.NET6 开源项目汇总7:电商项目

    系列目录     [已更新最新开发文章,点击查看详细] 谈起.NET/.NET Core的企业级实战案例,电商项目是典型代表.其中高负载.高并发.高可用性等问题是考核.NET技术性能的重要指标.下面整 ...

  10. Python-统计目录(文件夹)中Excel文件个数和数据量

    背景:前一阵子在帮客户做Excel文件中的数据处理,但是每周提交周报,领导都需要统计从客户接收的文件数量以及记录数.所以我就简单写了统计的脚本,方便统计目录(文件夹)中的Excel文件个数和数据量. ...