使用Mysql数据库完成增删改查综合案例(JSP页面)

Posted 小廖的记录箱的博客园

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Mysql数据库完成增删改查综合案例(JSP页面)相关的知识,希望对你有一定的参考价值。

本案例页面如下:

 


这是index.jsp页面(包含模糊查询)


<%
@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import=" java.sql.* "%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>首页</title> </head> <body> <% //取查询的名字 String selectName = request.getParameter("txtSelect"); %> <form action="index.jsp"> <h3 align=\'center\'> 请输入要查询的的名字:<input type="text" name="txtSelect" value="<%=selectName==null?"":selectName%>"> <input type="submit" name="btnSelect" value="开始查询"> </h3> </form> <table border="1" width="80%" align=\'center\'> <tr> <td></td> <td>序号</td> <td>姓名</td> <td>性别</td> <td>出生日期</td> <td>操作</td> </tr> <% //连接数据库的五大参数 String driverClass = "com.mysql.cj.jdbc.Driver"; String serverIp = "localhost"; String databaseName = "test3"; String userName = "root"; String pwd = "123456"; String jdbcUrl = "jdbc:mysql://"+serverIp+":3306/"+databaseName+"?serverTimezone=Asia/Shanghai&useSSL=true"; String sql = "select * from test3"; //读取JDBC Class.forName(driverClass); //链接数据库 Connection con = DriverManager.getConnection(jdbcUrl,userName,pwd); //如果为空,代表当前的状态不是查询,而是查询所有的内容 PreparedStatement ps ; if(selectName == null){ //判断查询文本框里面是否有内容,如果没有则执行查询全部信息 ps = con.prepareStatement(sql); }else{ //如果有,则根据文本框的里面的内容在数据库根据姓名查询 ps = con.prepareStatement("select * from test3 where user_name like ?"); ps.setString(1,\'%\'+selectName+\'%\'); } //ResultSet是一个指向数据库的变量,本质上是不保存任何数据的,执行查询 ResultSet rs = ps.executeQuery(); //boolean flag = rs.next(); //判断返回指针是否还能继续往下移动 //显示序号 int i = 1; while( rs.next()){ %> <tr> <td><%out.print(i++);%></td> <td><%=rs.getString("id") %></td> <td><%=rs.getString("user_name")%></td> <td><%=rs.getString("sex")%></td> <td><%=rs.getString("birt") %></td> <td> <a href="insert_Jsp.jsp">添加</a> <a href="editView.jsp?id=<%=rs.getString("id")%> ">编辑</a> <a href="delete_action.jsp?id=<%=rs.getString("id")%>">删除</a> </td> </tr> <% } %> </table> <h1 align="center">共计<%=i-1%>条记录</h1> <% ps.close(); con.close(); %> </body> </html>
这是删除逻辑页面:delete_action.jsp(后台运行,不显示)

<%
@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>删除的业务逻辑执行页面</title> </head> <body> <% //获取参数 String id = request.getParameter("id"); //连接数据库的五大参数 String driverClass = "com.mysql.cj.jdbc.Driver"; String serverIp = "localhost"; String databaseName ="test3"; String user = "root"; String pwd = "123456"; //拼凑成一个完整的Url地址 String jdbcUrl ="jdbc.mysql://"+serverIp+":3306/"+databaseName+"?serverTimezone=Asia/Shanghai%useSSL=true"; //创建对象 Connection con = DriverManager.getConnection(jdbcUrl,user,pwd); String sql = "delete from test3 where id = ?"; PreparedStatement ps = con.prepareStatement(sql); //对应sql语句的问号跟上面从页面获取的参数相对应 ps.setObject(1,id); //执行删除 ps.executeUpdate(); //关闭连接 ps.close(); con.close(); //完成删除后跳转回index页面 response.sendRedirect("index.jsp"); %> </body> </html>
这是用于添加的业务逻辑的运算:index_action.jsp(不显示,后台运算)

<%
@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import = "java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>添加</title> </head> <body> <% //获取参数 String name = request.getParameter("txtName"); String sex = request.getParameter("txtSex"); String birt = request.getParameter("txtBirthday"); //连接数据库的五大参数 String driverClass = "com.mysql.cj.jdbc.Driver"; String serverIp = "localhost"; String databaseName = "test3"; String userName = "root"; String pwd = "123456"; String jdbcUrl = "jdbc:mysql://"+serverIp+":3306/"+databaseName+"?serverTimezone=Asia/Shanghai&useSSL=true"; Class.forName(driverClass); Connection con = DriverManager.getConnection(jdbcUrl,userName,pwd); String sql ="insert into test3(user_name,sex,birt) values(?,?,?)"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1,name); ps.setString(2,sex); ps.setString(3,birt); ps.executeUpdate(); con.close(); response.sendRedirect("index.jsp"); %> </body> </html>
这是添加的显示页面:index.jsp(用户客户端)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加信息</title>
</head>
<body>
<%

%>

<form action="insert.jsp">
    学号:<input type="text" name="txtId" ><br>
    姓名:<input type="text" name="txtName"><br>
    性别:<input type="text" name="txtSex"><br>
    出生年月日:<input type="text" name="txtBirthday"><br>
    <input type="submit" value="确认添加"jsp为啥要用增删改查

jsp连接mysql数据库后增删改查怎么写

微信小程序云开发— “增删改查综合案例(用户输入数据并动态添加到数据库)”

jsp做一个最简单的,连接数据库,实现增删改查人员姓名的功能。一定要非常简单的那种。

Web对数据库的增删改查(servlet+jsp+javaBean增删改查)

jsp-2 简单的servlet连接mysql数据库 增删改查