web实现增删改查的方式有很多啊,对于初学者来说当然是要先了解各部分的传值的方式.本篇博客从jsp技术的最基础方面进行说明.
一、什么是jsp技术
首先,我们要了解什么是jsp技术.
jsp技术是基于JAVA语言的动态网页技术,通常使用HTML语言来设计和格式化静态页面内容,然后通过jsp标签来实现动态部分,也就是数据间的互交.在这里推荐一篇博客来供初学者进行学习.
"的博客:
二、代码实现
1.创建数据库表
创建一下数据库表,数据库名Student,表名student.
2.创建web项目,项目名为Student并建立以下jsp页面
1)主页面 index.jsp
1 <%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%> 2 <%@ page errorPage="error.jsp"%> 3 4 5学生管理系统 6 7 8 91050 59 60学生管理系统
11 添加学生信息 1213
48 4914 21 <%22 try {23 Class.forName("com.mysql.jdbc.Driver");24 Connection con = DriverManager.getConnection(25 "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root",26 "root");27 //使用Statement对象28 Statement stmt = con.createStatement();29 String s = "select * from student";30 ResultSet rs = stmt.executeQuery(s);31 32 while (rs.next()) {33 int id = rs.getInt(1);34 out.println("学号 15姓名 16性别 17出生日期 18家庭住址 19操 作 20");38 }39 rs.close();40 stmt.close();41 con.close();42 } catch (Exception e) {43 out.println("Exception:" + e.getMessage());44 }45 %>46 47 " + rs.getString(1) + " " + rs.getString(2) + " "35 + rs.getString(3) + " " + rs.getString(4) + " " + rs.getString(5)36 + " 修改 删除
这里采用JDBC来连接数据库,并指定字符编码与解码方式为utf-8.并通过遍历表student的数据将每一行的每一列数据依次out.printf输出到table中.设计修改、操作的操作按钮传值id进行相应操作.
其中,script中为javascript脚本语言,用来限制删除的操作,防止用户误删.
2)添加操作
add.jsp
1 <%@ page contentType="text/html; charset=utf-8" import="java.sql.*" 2 errorPage="error.jsp"%> 3 4 5添学生信息 6 7 8 91049 50添加成员信息
11 48
设计添加页面,通过form表单传值到addsave.jsp
addsave.jsp
addsave.jsp进行数据的获取并将数据添加到数据库.因为传值的时候是以String的形式传输的,所以对于id要进行类型转换 1 int id = Integer.parseInt(request.getParameter("id")); .
1 <%@ page contentType="text/html; charset=utf-8" import="java.sql.*" 2 errorPage="error.jsp"%> 3 4 5添加学生信息 6 7 8 9 <%10 request.setCharacterEncoding("utf-8");11 String submit = request.getParameter("submit");12 13 int id = Integer.parseInt(request.getParameter("id"));14 String name = request.getParameter("name");15 String sex = request.getParameter("sex");16 String birthday = request.getParameter("birthday");17 String address = request.getParameter("address");18 Class.forName("com.mysql.jdbc.Driver");19 Connection con = DriverManager.getConnection(20 "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root", "root");21 //使用Statement对象22 Statement stmt = con.createStatement();23 String sql = "insert into student(id,name,sex,birthday,address) values('" + id + "','" + name + "','" + sex24 + "','" + birthday + "'," + address + ")";25 int i = stmt.executeUpdate(sql);26 27 if (i == 1) {28 %>29 30 34 <%35 } else {36 %>37 41 <%42 }43 stmt.close();44 con.close();45 %>46 47
3)修改操作
edit.jsp
1 <%@ page import="java.sql.*" pageEncoding="utf-8" errorPage="error.jsp"%> 2 3 4修改学生信息 5 6 7 8 <% 9 request.setCharacterEncoding("utf-8");10 Class.forName("com.mysql.jdbc.Driver");11 Connection con = DriverManager.getConnection(12 "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root", "20173673");13 Statement stmt = con.createStatement();14 String id = request.getParameter("id");15 ResultSet rs = stmt.executeQuery("select * from student where id=" + id);16 rs.next();17 %>1819 5556 <%57 rs.close();58 stmt.close();59 con.close();60 %>61 62 63
通过id获取要修改的内容,显示到table的input输入框中,进行修改.然后将值传输到editsave.jsp中,进行信息修改的操作.
editsave.jsp
1 <%@ page import="java.sql.*" pageEncoding="utf-8" errorPage="error.jsp"%> 2 3 4修改完成 5 6 7 8 <% 9 request.setCharacterEncoding("utf-8");10 int id = Integer.parseInt(request.getParameter("id"));11 String name = request.getParameter("name");12 String sex = request.getParameter("sex");13 String birthday = request.getParameter("birthday");14 String address = request.getParameter("address");15 Class.forName("com.mysql.jdbc.Driver");16 Connection con = DriverManager.getConnection(17 "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root", "root");18 Statement stmt = con.createStatement();19 String sql = "update student set id = '" + id + "',name='" + name + "',sex = '" + sex + "',birthday='"20 + birthday + "', address = '" + address + "'where id=" + id;21 22 int i = stmt.executeUpdate(sql);23 if (i == 1) {24 %>25 26 30 <%31 } else {32 %>33 38 39 <%40 }41 stmt.close();42 con.close();43 %>44 45
4)删除操作
delete.jsp
1 <%@ page contentType="text/html; charset=gbk" language="java" 2 import="java.sql.*" pageEncoding="utf-8"%> 3 4 5删除学生信息 6 7 8 9 <%10 request.setCharacterEncoding("gbk");11 Class.forName("com.mysql.jdbc.Driver");12 Connection con = DriverManager.getConnection(13 "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root", "root");14 Statement stmt = con.createStatement();15 String id = request.getParameter("id");16 int i = stmt.executeUpdate("delete from student where id=" + id);17 if (i == 1) {18 %>19 23 <%24 } else {25 %>26 30 <%31 }32 con.close();33 stmt.close();34 %>35 36
三、效果截图
index
add
edit
大体就是这样啦,有什么不对的希望大家多多指正.