SoFunction
Updated on 2025-03-08

jsp+servlet implements the simplest code sharing for adding, deleting, modifying and checking

Without further ado, please see the code

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class AdminDao {
 public List<Admin> getAllAdmin(){ //Query all information List<Admin> list = new ArrayList<Admin>(); //Create a collection Connection conn = ();
 String sql = "select * from admin"; //SQL query statement try {
  PreparedStatement pst = (sql);
  ResultSet rst = ();
  while (()) {
  Admin admin = new Admin();
  (("id")); //Get ID  (("username"));
  (("userpwd"));
  (admin);
  }
  (); //closure  (); //closure } catch (SQLException e) {
  (); //throw an exception }
 return list; //Return a collection }
 public boolean addAdmin(Admin admin){ //Add information String sql = "INSERT INTO `admin`(`id`,`username`,`userpwd`) VALUES (?,?,?)"; //Added SQL statement Connection conn = ();
 try {
  PreparedStatement pst = (sql);
  (1, ());
  (2, ());
  (3, ());
  int count = ();
  ();
  return count>0?true:false; //Judgement of whether to add } catch (SQLException e) {
  ();
 }
 return false;
 }
 public boolean updateAdmin(Admin admin){ //Revise String sql = "UPDATE `admin` SET `username`=?,`userpwd`=? WHERE `id` = ?"; //Modified SQL statement, modify it according to ID Connection conn = ();
 try {
  PreparedStatement pst = (sql);
  (1, ());
  (2, ());
  (3, ()); //Based ID  int count = ();
  (); //closure  return count>0?true:false; //Judgement of whether to modify } catch (SQLException e) {
  ();
 }
 return false;
 }
 public boolean deleteAdmin(int id){ //delete String sql = "delete from admin where id = ?"; //Deleted SQL statements, deleted according to ID Connection conn = ();
 try {
  PreparedStatement pst = (sql);
  (1, id);
  int count = ();
  ();
  return count>0?true:false; //Judgement on whether to delete } catch (SQLException e) {
  ();
 }
 return false;
 }
 public Admin selectAdminById(int id){ //Check according to ID Connection conn = ();
 String sql = "select * from admin where id = "+id;
 Admin admin = null;
 try {
  PreparedStatement pst = (sql);
  ResultSet rst = ();
  while (()) {
  admin = new Admin();
  (("id"));
  (("username"));
  (("userpwd"));
  }
  ();
  ();
 } catch (SQLException e) {
  ();
 }
 return admin; //return }
}
package ;
import ;
import ;
/**
  * Connect to the database
  * @author Draw a boat and listen to the rain and sleep
  *
  */
public class DbHelper {
 private static String url = "jdbc:mysql://localhost:3306/admin"; //Database address private static String userName = "root"; //Database username private static String passWord = "359129127"; //Database Password private static Connection conn = null;
 private DbHelper(){
 }
 public static Connection getConnection(){
 if(null == conn){
  try {
  ("");
  conn = (url, userName, passWord);
  } catch (Exception e) {
  ();
  }
 }
 return conn;
 }
 public static void main(String[] args) { //Test whether the database is connected (getConnection());
 }
}
package ;
import ;
public class Admin implements Serializable{ //Data encapsulation class 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) {
  = id;
 }
 public String getUsername() {
 return username;
 }
 public void setUsername(String username) {
  = username;
 }
 public String getUserpwd() {
 return userpwd;
 }
 public void setUserpwd(String userpwd) {
  = userpwd;
 }
}
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class AddServlet extends HttpServlet{ //Add data private static final long serialVersionUID = 1L;
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 (req, resp);
 }
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 String username = ("username");
 String userpwd = ("userpwd");
 Admin admin = new Admin();
 (new String(("ISO-8859-1"),"UTF-8")); //Transfer value, Chinese needs to be converted to utf-8 (new String(("ISO-8859-1"),"UTF-8"));
 AdminDao dao = new AdminDao();
 (admin);
 ("ShowServlet").forward(req, resp);
 } 
}
package ;
import ;
import ;
import ;
import ;
import ;
import ;
public class DeleteServlet extends HttpServlet{ //Delete data private static final long serialVersionUID = 1L;
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 (req, resp);
 }
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 String idStr = ("id"); //Delete the ID of the data and delete it according to the ID if(idStr != null && !("")){
  int id = (idStr);
  AdminDao dao = new AdminDao();
  (id);
 }
 ("ShowServlet").forward(req, resp);
 }
}
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class ShowServlet extends HttpServlet{ //Show all data private static final long serialVersionUID = 1L;
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 (req, resp);
 }
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
 AdminDao dao = new AdminDao();
 List<Admin> list = ();
 ("list", list);
 ("").forward(req, resp);
 } 
}
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class UpdateServlet extends HttpServlet{ //Revise private static final long serialVersionUID = 1L;
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException { //Query the data corresponding to the selected ID value String idStr = ("id");
 if(idStr != null && !("")){
  int id = (idStr);
  AdminDao dao = new AdminDao();
  Admin admin = (id);
  ("admin", admin);
 }
 ("").forward(req, resp);

 }
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException { //Modify the value of the data according to this ID String username = ("username");
 String userpwd = ("userpwd");
 String idStr = ("id");
 Admin admin = new Admin();
 ((idStr));
 (new String(("ISO-8859-1"),"UTF-8"));
 (new String(("ISO-8859-1"),"UTF-8"));
 AdminDao dao = new AdminDao();
 (admin);
 ("ShowServlet").forward(req, resp);
 }
}
<%@ page language="java" import=".*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head> 
 <title>Add to</title>
 <link rel="stylesheet" href="css/" type="text/css" />
 </head>
 <body>
 <form action="AddServlet" method="post">
 <table border="1" class="t1">
 <tr>
  <td colspan="2"><h1>Add to管理员</h1></td>
 </tr>
 <tr>
  <td>Administrator account:</td>
  <td><input type="text" name="username"/></td>
 </tr>
 <tr>
  <td>Administrator password:</td>
  <td><input type="password" name="userpwd"/></td>
 </tr>
 <tr>
  <td colspan="2">
  <input type="submit" value="submit"/>
  <input type="reset" value="Clear"/>
  </td>
 </tr>
 </table>
 </form>
 </body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
 pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:///TR/html4/">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>show</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>serial number</td>
  <td>account number</td>
  <td>password</td>
  <td>operate</td>
 </tr>
 <c:forEach items="${list}" var="item">
  <tr>
  <td>${ }</td>
  <td>${ }</td>
  <td>${ }</td>
  <td><a href="DeleteServlet?id=${ }">delete</a>|<a href="UpdateServlet?id=${ }">Revise</a></td>
  </tr>
 </c:forEach>
 <tr>
  <td colspan="6" style="text-align: left;"><a href="">Add an administrator</a></td>
 </tr>
 </table>
</body>
</html>
<%@ page language="java" import=".*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head> 
 <title>Revise</title>
 <link rel="stylesheet" href="css/" type="text/css" />
 </head>
 <body>
 <form action="UpdateServlet" method="post">
 <table border="1" class="t1">
 <tr>
  <td colspan="2"><h1>Revise管理员信息</h1></td>
 </tr>
 <tr>
  <td>serial number:</td>
  <td><input type="text" name="id" value="${}" readonly="readonly"/></td>
 </tr>
 <tr>
  <td>Administrator account:</td>
  <td><input type="text" name="username" value="${}"/></td>
 </tr>
 <tr>
  <td>Administrator password:</td>
  <td><input type="text" name="userpwd" value="${}"/></td>
 </tr>
 <tr>
  <td colspan="2">
  <input type="submit" value="submit"/>
  <input type="button" value="return" onclick="(-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 "Songyi";
  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;
 }

The simplest jsp+servlet adds, deletes, modify and checks the code. It was written very clearly, that's it.

Implementation principle:

Each row of data is followed by an edit and delete button, which is submitted to the background and has the main parameters of this row of data.

Click the edit button, replace each column of this line with a text box through servlet operation jsp and bring the existing value in it. The latter submit button submits the data through submit and turns the text box back into a cell of the table.

Add, just like editing, add a line, all text boxes. . .

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!