HTML call servlet (ii)

Source: Internet
Author: User

5. Modifying data 5.1 Writing the query criteria page

When modifying a single piece of data, the first is to query the details of the individual data, and then modify or modify it as needed. Once modified, the data is submitted to the database, and the updated data is saved in the database.

The query criteria page code for querying a single piece of data is as follows:

Querytoupdate.html

 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >5.2 Writing a servlet that displays departmental details

After entering the department number to be modified, enter the servlet to query the department information according to the department number, display the Department details to the page, some non-modifiable fields can be set to read-only, so that the data will not be modified, the servlet code is as follows:

Querytoupdateservlet.java

Package Com.cn.update;import Java.io.ioexception;import Java.io.printwriter;import java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import java.sql.SQLException; Import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public Class Querytoupdateservlet extends HttpServlet {/** * destruction of the servlet. <br> * * public void Destr Oy () {Super.destroy ();//Just puts "destroy" string in Log//Put your code here}/** * the DoG Et method of the servlet.     <br> * This method was called when a form have its tag value method equals to get.  * * @param request the request send by the client to the server * @param response the response send by the server To the client * @throws servletexception If a error occurred * @throws IOException if an erroroccurred */public void doget (HttpServletRequest request, httpservletresponse response) throws Servletex        Ception, IOException {response.setcontenttype ("text/html;charset=gb2312");        Request.setcharacterencoding ("gb2312");        PrintWriter out = Response.getwriter ();        String id = request.getparameter ("id");        Connection con = null;        PreparedStatement pstmt = null;        ResultSet rs = null;            try {class.forname ("com.mysql.jdbc.Driver"); System.out.println ("Create driver success!")            ");            con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/bank", "root", "1234"); SYSTEM.OUT.PRINTLN ("Database connection succeeded!            ");            String sql = "SELECT * FROM dept WHERE id=?";            pstmt = con.preparestatement (sql);            Pstmt.setstring (1, id);        rs = Pstmt.executequery ();        } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); } CatCH (SQLException e) {//TODO auto-generated catch block E.printstacktrace (); }//Displays information for individual departments out.println ("5.3 Writing a servlet that handles modification operations

In Updatedeptservlet, after the data is modified, all the information in the database table is displayed. The code for Updatedeptservlet is as follows:

Updatedeptservlet.java

Package Com.cn.update;import Java.io.ioexception;import Java.io.printwriter;import java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import java.sql.SQLException; Import Java.sql.statement;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public Class Updatedeptservlet extends HttpServlet {/** * The Doget method of the servlet. <br> * This method i     s called when A is a form has it tag value method equals to get.  * * @param request the request send by the client to the server * @param response the response send by the server    To the client * @throws servletexception If a error occurred * @throws IOException If an error occurred */ public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, ioexcept Ion {ResponSe.setcontenttype ("text/html");        PrintWriter out = Response.getwriter ();        This.dopost (request, response);        Out.flush ();    Out.close (); }/** * The DoPost method of the servlet.     <br> * This method was called when a form have its tag value method equals to post.  * * @param request the request send by the client to the server * @param response the response send by the server    To the client * @throws servletexception If a error occurred * @throws IOException If an error occurred */ public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcep        tion {response.setcontenttype ("text/html;charset=gb2312");        Request.setcharacterencoding ("gb2312");        PrintWriter out = Response.getwriter ();        Connection con = null;        PreparedStatement PS = null;        ResultSet rs = null;        Statement sta = null; String id = request.getparameteR ("id");        String address = Request.getparameter ("Address");        int empnumber = Integer.parseint (Request.getparameter ("Empnumber"));        String d_name = Request.getparameter ("D_name");            try {class.forname ("com.mysql.jdbc.Driver"); System.out.println ("Create driver success!")            ");            con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/bank", "root", "1234"); SYSTEM.OUT.PRINTLN ("Database connection succeeded!            "); String sql = "UPDATE dept SET id=?,address=?,empnumber=?,d_name=?"            WHERE id=? ";            PS = con.preparestatement (SQL);            The following settings modify the data value ps.setstring (1, id);            Ps.setstring (2, address);            Ps.setint (3, Empnumber);            Ps.setstring (4, d_name);            Ps.setstring (5, id);            Ps.executeupdate (); SYSTEM.OUT.PRINTLN ("Modified successfully!            ");            /* * Add success, display all information */sta = con.createstatement ();    rs = Sta.executequery ("SELECT * from dept");        Displays all the information in the table in the page out.println ("6. Delete data

When you delete data, you specify the criteria for deletion, otherwise all the data in the table is deleted. After deletion, the deleted data does not exist in the table. The following example is an example of deleting data based on the department number. First enter the department number you want to delete in the page, enter the page code for the department number you want to delete:

Delete.html

 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

When the delete condition is entered, clicking the Delete button enters the deletebyidservlet specified in the form form, which is a servlet that handles the delete operation. The code in Deletebyidservlet is as follows:

Deletebyidservlet.java

Package Com.cn.delete;import Java.io.ioexception;import Java.io.printwriter;import java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import java.sql.SQLException; Import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public Class Deletebyidservlet extends HttpServlet {/** * The Doget method of the servlet. <br> * This method i     s called when A is a form has it tag value method equals to get.  * * @param request the request send by the client to the server * @param response the response send by the server    To the client * @throws servletexception If a error occurred * @throws IOException If an error occurred */ public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, ioexcept Ion {Response.setcontenttype ("Text/htML ");        PrintWriter out = Response.getwriter ();        This.dopost (request, response);        Out.flush ();    Out.close (); }/** * The DoPost method of the servlet.     <br> * This method was called when a form have its tag value method equals to post.  * * @param request the request send by the client to the server * @param response the response send by the server    To the client * @throws servletexception If a error occurred * @throws IOException If an error occurred */ public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcep        tion {response.setcontenttype ("text/html;charset=gb2312");        Request.setcharacterencoding ("gb2312");        PrintWriter out = Response.getwriter ();        String id = request.getparameter ("id");        Connection con = null;//ResultSet rs = null;        PreparedStatement PS = null; try {class.forname ("COM.MYsql.jdbc.Driver "); System.out.println ("Create driver success!")            ");            con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/bank", "root", "1234"); SYSTEM.OUT.PRINTLN ("Database connection succeeded!            ");            String sql = "DELETE from dept WHERE id=?";            PS = con.preparestatement (SQL);            Ps.setstring (1, id);            Ps.executeupdate (); System.out.println ("Delete succeeded!            "); Display result information Out.println ("

In Deletebyidservlet, the department number passed by the page is obtained, and the data corresponding to the number is deleted according to the department number, and the deletion succeeds in the page to prompt the deletion.

HTML call servlet (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.