Jsp + servlet implements the simplest addition, deletion, modification, and query code sharing, jspservlet
For more information, see the code.
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 () {// query all information lists <Admin> List = new ArrayList <Admin> (); // create a collection Connection conn = DbHelper. getConnection (); String SQL = "select * From admin "; // SQL query statement try {PreparedStatement pst = conn. prepareStatement (SQL); ResultSet rst = pst.exe cuteQuery (); while (rst. next () {Admin admin = new Admin (); admin. setId (rst. getInt ("id"); // obtain the ID admin. setUsername (rst. getString ("username"); admin. setUserpwd (rst. getString ("userpwd"); list. add (admin);} rst. close (); // close pst. close (); // close} catch (SQLException e) {e. printStackTrace (); // throw Exception} return list; // return a collection} public boolean addAdmin (Admin admin) {// Add information String SQL = "INSERT INTO 'admin' ('id ', 'username', 'userpwd') VALUES (?,?,?) "; // The added SQL statement 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.exe cuteUpdate (); pst. close (); return count> 0? True: false; // judge whether to add} catch (SQLException e) {e. printStackTrace ();} return false;} public boolean updateAdmin (Admin admin) {// modify String SQL = "UPDATE 'admin' SET 'username' = ?, 'Userpwd' =? WHERE 'id' =? "; // Modify the SQL statement, and modify Connection conn = DbHelper according to the ID. getConnection (); try {PreparedStatement pst = conn. prepareStatement (SQL); pst. setString (1, admin. getUsername (); pst. setString (2, admin. getUserpwd (); pst. setInt (3, admin. getId (); // The ID int count = pst.exe cuteUpdate (); pst. close (); // close return count> 0? True: false; // determine whether to modify} catch (SQLException e) {e. printStackTrace ();} return false;} public boolean deleteAdmin (int id) {// delete String SQL = "delete from admin where id =? "; // Delete the SQL statement, and delete Connection conn = DbHelper according to the ID. getConnection (); try {PreparedStatement pst = conn. prepareStatement (SQL); pst. setInt (1, id); int count = pst.exe cuteUpdate (); pst. close (); return count> 0? True: false; // determine whether to delete} catch (SQLException e) {e. printStackTrace ();} return false;} public Admin selectAdminById (int id) {// query Connection conn = DbHelper by ID. getConnection (); String SQL = "select * from admin where id =" + id; Admin admin = null; try {PreparedStatement pst = conn. prepareStatement (SQL); ResultSet rst = pst.exe cuteQuery (); 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; // return }}
Package ceet.ac.cn. dao; import java. SQL. connection; import java. SQL. driverManager;/*** connect to the database * @ author **/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 {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) {// test whether the database is connected to the System. err. println (getConnection ());}}
Package ceet.ac.cn. model; import java. io. serializable; 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) {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 {// Add 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"); // convert the value to 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 {// delete data 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"); // the ID of the data to be deleted. Delete 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 {// display all data 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 {// modify private static final long serialVersionUID = 1L; protected void do Get (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// query the data String idStr = req corresponding to the value of the selected ID. 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 {// modify String username = req based on this ID. getParameter ("username"); String userpwd = req. getParameter ("userpwd"); String idStr = req. getParameter ("id"); Admin 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">
<% @ 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>
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
@ 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 ;}
The simplest jsp + servlet addition, deletion, modification, and query code. Write clearly, that's it.
Implementation principle:
Add an edit and delete button next to each row of data. The button is submitted to the background and contains the main parameters of this row of data.
Click the edit button and use the servlet operation jsp to replace each column of the row with a text box and include the existing values, the next submit button submits data through submit and changes the text box to a table cell.
Add, just like editing, add a row, all of which are text boxes...
The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!