话不多说,请看代码
package ceet.ac.cn.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import ceet.ac.cn.model.Admin; public class AdminDao { public List<Admin> getAllAdmin(){ //查询所有信息 List<Admin> list = new ArrayList<Admin>(); //创建集合 Connection conn = DbHelper.getConnection(); String sql = "select * from admin"; //SQL查询语句 try { PreparedStatement pst = conn.prepareStatement(sql); ResultSet rst = pst.executeQuery(); while (rst.next()) { Admin admin = new Admin(); admin.setId(rst.getInt("id")); //得到ID admin.setUsername(rst.getString("username")); admin.setUserpwd(rst.getString("userpwd")); list.add(admin); } rst.close(); //关闭 pst.close(); //关闭 } catch (SQLException e) { e.printStackTrace(); //抛出异常 } return list; //返回一个集合 } public boolean addAdmin(Admin admin){ //添加信息 String sql = "INSERT INTO `admin`(`id`,`username`,`userpwd`) VALUES ("; //添加的SQL语句 Connection conn = DbHelper.getConnection(); try { PreparedStatement pst = conn.prepareStatement(sql); pst.setInt(1, admin.getId()); pst.setString(2, admin.getUsername()); pst.setString(3, admin.getUserpwd()); int count = pst.executeUpdate(); pst.close(); return count>0"UPDATE `admin` SET `username`="; //修改的SQL语句,根据ID修改 Connection conn = DbHelper.getConnection(); try { PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, admin.getUsername()); pst.setString(2, admin.getUserpwd()); pst.setInt(3, admin.getId()); //根据的ID int count = pst.executeUpdate(); pst.close(); //关闭 return count>0"delete from admin where id = "; //删除的SQL语句,根据ID删除 Connection conn = DbHelper.getConnection(); try { PreparedStatement pst = conn.prepareStatement(sql); pst.setInt(1, id); int count = pst.executeUpdate(); pst.close(); return count>0"select * from admin where id = "+id; Admin admin = null; try { PreparedStatement pst = conn.prepareStatement(sql); ResultSet rst = pst.executeQuery(); while (rst.next()) { admin = new Admin(); admin.setId(rst.getInt("id")); admin.setUsername(rst.getString("username")); admin.setUserpwd(rst.getString("userpwd")); } rst.close(); pst.close(); } catch (SQLException e) { e.printStackTrace(); } return admin; //返回 } }
package ceet.ac.cn.dao; import java.sql.Connection; import java.sql.DriverManager; /** * 连接数据库 * @author 画船听雨眠 * */ public class DbHelper { private static String url = "jdbc:mysql://localhost:3306/admin"; //数据库地址 private static String userName = "root"; //数据库用户名 private static String passWord = "359129127"; //数据库密码 private static Connection conn = null; private DbHelper(){ } public static Connection getConnection(){ if(null == conn){ try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, userName, passWord); } catch (Exception e) { e.printStackTrace(); } } return conn; } public static void main(String[] args) { //测试数据库是否连通 System.err.println(getConnection()); } }
package ceet.ac.cn.model; import java.io.Serializable; public class Admin implements Serializable{ //数据封装类 private static final long serialVersionUID = 1L; private int id; private String username; private String userpwd; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpwd() { return userpwd; } public void setUserpwd(String userpwd) { this.userpwd = userpwd; } }
package ceet.ac.cn.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import ceet.ac.cn.dao.AdminDao; import ceet.ac.cn.model.Admin; public class AddServlet extends HttpServlet{ //添加数据 private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); String userpwd = req.getParameter("userpwd"); Admin admin = new Admin(); admin.setUsername(new String(username.getBytes("ISO-8859-1"),"UTF-8")); //转值,中文需要转换为utf-8 admin.setUserpwd(new String(userpwd.getBytes("ISO-8859-1"),"UTF-8")); AdminDao dao = new AdminDao(); dao.addAdmin(admin); req.getRequestDispatcher("ShowServlet").forward(req, resp); } }
package ceet.ac.cn.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import ceet.ac.cn.dao.AdminDao; public class DeleteServlet extends HttpServlet{ //删除数据 private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String idStr = req.getParameter("id"); //删除数据的ID,根据ID删除 if(idStr != null && !idStr.equals("")){ int id = Integer.valueOf(idStr); AdminDao dao = new AdminDao(); dao.deleteAdmin(id); } req.getRequestDispatcher("ShowServlet").forward(req, resp); } }
package ceet.ac.cn.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import ceet.ac.cn.dao.AdminDao; import ceet.ac.cn.model.Admin; public class ShowServlet extends HttpServlet{ //显示全部数据 private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { AdminDao dao = new AdminDao(); List<Admin> list = dao.getAllAdmin(); req.setAttribute("list", list); req.getRequestDispatcher("index.jsp").forward(req, resp); } }
package ceet.ac.cn.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import ceet.ac.cn.dao.AdminDao; import ceet.ac.cn.model.Admin; public class UpdateServlet extends HttpServlet{ //修改 private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //查询到选中ID的值所对应的数据 String idStr = req.getParameter("id"); if(idStr != null && !idStr.equals("")){ int id = Integer.valueOf(idStr); AdminDao dao = new AdminDao(); Admin admin = dao.selectAdminById(id); req.setAttribute("admin", admin); } req.getRequestDispatcher("update.jsp").forward(req, resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //根据此ID对数据的值进行修改 String username = req.getParameter("username"); String userpwd = req.getParameter("userpwd"); String idStr = req.getParameter("id"); Admin admin = new Admin(); admin.setId(Integer.valueOf(idStr)); admin.setUsername(new String(username.getBytes("ISO-8859-1"),"UTF-8")); admin.setUserpwd(new String(userpwd.getBytes("ISO-8859-1"),"UTF-8")); AdminDao dao = new AdminDao(); dao.updateAdmin(admin); req.getRequestDispatcher("ShowServlet").forward(req, resp); } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>添加</title> <link rel="stylesheet" href="css/index.css" type="text/css" /> </head> <body> <form action="AddServlet" method="post"> <table border="1" class="t1"> <tr> <td colspan="2"><h1>添加管理员</h1></td> </tr> <tr> <td>管理员帐号:</td> <td><input type="text" name="username"/></td> </tr> <tr> <td>管理员密码:</td> <td><input type="password" name="userpwd"/></td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"/> <input type="reset" value="清空"/> </td> </tr> </table> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!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> <style type="text/css"> table { border: 1px solid pink; margin: 0 auto; } td{ width: 150px; border: 1px solid pink; text-align: center; } </style> </head> <body> <table> <tr> <td>编号</td> <td>帐号</td> <td>密码</td> <td>操作</td> </tr> <c:forEach items="${list}" var="item"> <tr> <td>${item.id }</td> <td>${item.username }</td> <td>${item.userpwd }</td> <td><a href="DeleteServlet">删除</a>|<a href="UpdateServlet">修改</a></td> </tr> </c:forEach> <tr> <td colspan="6" style="text-align: left;"><a href="add.jsp">添加管理员</a></td> </tr> </table> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>修改</title> <link rel="stylesheet" href="css/index.css" type="text/css" /> </head> <body> <form action="UpdateServlet" method="post"> <table border="1" class="t1"> <tr> <td colspan="2"><h1>修改管理员信息</h1></td> </tr> <tr> <td>编号:</td> <td><input type="text" name="id" value="${admin.id}" readonly="readonly"/></td> </tr> <tr> <td>管理员帐号:</td> <td><input type="text" name="username" value="${admin.username}"/></td> </tr> <tr> <td>管理员密码:</td> <td><input type="text" name="userpwd" value="${admin.userpwd}"/></td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交"/> <input type="button" value="返回" onclick="history.go(-1)"/> </td> </tr> </table> </form> </body> </html>
@CHARSET "UTF-8"; table.t1 { margin-top:10px; margin-left:20px; margin-right:20px; margin-bottom:5px; #background-color: #FFF; #background:#EEF4F9; #border: none; border: 1; #color:#003755; border-collapse:collapse; font: 14px "宋体"; text-align: center; } table.t1 th{ background:#7CB8E2; color:#fff; padding:6px 4px; text-align:center; } table.t1 td{ background:#C7DDEE none repeat-x scroll center left; color:#000; padding:4px 2px; } table.t1 a{ text-decoration:none; height:1em; } table.t1 a:link, table.t1 a:visited{ color:#3366CC; } table.t1 a:hover{ color:#B50000; text-decoration:underline; }
最简单的jsp+servlet的增删改查代码。写的很清楚,就这样了。
实现原理:
每行数据后面加一个编辑和删除按钮,按钮提交到后台并且带有此行数据的主要参数。
点击编辑按钮,通过servlet操作jsp将此行的每一列替换为一个文本框并把已有的值带进去,后面一个提交按钮通过submit提交数据并将文本框重新变为表格的单元格。
新增,就像编辑一样,添加一行,全部是文本框。。。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“jsp+servlet实现最简单的增删改查代码分享”评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新动态
20240901
20240901
- 《庄心妍 实力唱将 发烧音乐精选集》[WAV+CUE][380MB]
- [音展纪念碟]2006年《TAA台湾第十六届国际HI-ENDHI-FI音响展》2cd[WAV]
- 中国音乐地图之听见西藏山歌牧歌酒歌2020[WAV分轨]
- 王宏伟2017-永远的红飘带[太平洋影音][WAV+CUE]
- 费翔.1997-NOW现在【第一中国】【WAV+CUE】
- 新格合唱团.1981-我唱你和2辑(滚石新格民歌系列)【新格】【WAV+CUE】
- 陈柏宇.2011-QUINQUENNIUM【SONY】【WAV+CUE】
- 雨果唱片-《金唛系列-蔷薇依韵琼瑶》WAV
- 陈百强《凝望(2012华纳LPCD45限量版)[DMI]》[WAV整轨]
- 模拟之声慢刻CD《徐小凤45周年》[德国母带][低速原抓WAV+CUE]
- 《庄心妍 实力唱将 发烧音乐精选集》[WAV+CUE][380MB]
- 《王菲 单曲合集》[FlAC/分轨][520MB]
- 《费玉清 原味再现 水长流》[WAV+CUE][430MB]
- 群星.1992-畅销榜老歌金曲10CD【风格】【WAV+CUE】
- 孙悦.2024-时光音乐会【金蜂】【WAV+CUE】