商城建站流程,在线seo推广软件,手机网站 设置,校园互动网站建设5.修改数据 5.1编写查询条件页面 修改单条数据的时候#xff0c;首先是查询出单个数据的详细信息#xff0c;然后根据实际需要部分修改或者全部修改。修改之后#xff0c;数据会提交到数据库#xff0c;数据库中保存更新以后的数据。 查询出单条数据的查询条件页面代码如下…5.修改数据 5.1编写查询条件页面 修改单条数据的时候首先是查询出单个数据的详细信息然后根据实际需要部分修改或者全部修改。修改之后数据会提交到数据库数据库中保存更新以后的数据。 查询出单条数据的查询条件页面代码如下 QueryToUpdate.html !DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
htmlheadtitleQueryToUpdate.html/titlemeta http-equivkeywords contentkeyword1,keyword2,keyword3meta http-equivdescription contentthis is my pagemeta http-equivcontent-type contenttext/html; charsetUTF-8!--link relstylesheet typetext/css href./styles.css--/headbodyform namef1 idf1 action/jdbc_servlet/servlet/QueryToUpdateServlet methodposttable border0trtd请输入要修改的部门编号:/tdtr/trtdinput typetext nameid /td/trtrtd colspan2 alignleftinput typesubmit value提交/td/tr/table/form/body
/html 5.2 编写显示部门详细信息的Servlet 输入要修改的部门编号以后进入根据部门编号查询部门信息的Servlet把部门详细信息显示到页面中一些不可以修改的字段可以设置成只读这样就不会把这些数据修改了该Servlet的代码如下 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 destroy() {super.destroy(); // Just puts destroy string in log// Put your code here}/*** The doGet method of the servlet. br** This method is called when a form has 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 an error occurred* throws IOException if an error occurred*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType(text/html;charsetgb2312);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(创建驱动成功);con DriverManager.getConnection(jdbc:mysql://localhost:3306/bank, root, 1234);System.out.println(数据库连接成功);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 blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}//显示单个部门的信息out.println(html headtitle显示单个部门信息/title/head body);out.println(h1显示单个部门信息/h1brbr);out.print(form action/jdbc_servlet/servlet/UpdateDeptServlet methodpost);try {while(rs.next()){out.println(部门编号);out.print(br);//在文本框中显示部门编号设置成只读out.println(input typetext nameid readonlytrue value);out.println(rs.getString(1).toString());out.print();out.print(br);out.println(部门名称);out.print(br);//在文本框中显示部门名称out.println(input typetext named_name value);out.println(rs.getString(2).toString());out.print();out.print(br);out.println(部门地址);out.print(br);//在文本框中显示部门地址out.println(input typetext nameaddress value);out.println(rs.getString(3).toString());out.print();out.print(br);out.println(部门人数);out.print(br);//在文本框中显示部门人数out.println(input typetext nameempnumber value);out.println(rs.getString(4).toString());out.print();out.print(br);//提交按钮out.print(input typesubmit valueSubmit);out.print(/form);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}out.flush();out.close();}/*** The doPost method of the servlet. br** This method is called when a form has 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 an error occurred* throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType(text/html;charsetgb2312);request.setCharacterEncoding(gb2312);PrintWriter out response.getWriter();this.doGet(request, response);out.flush();out.close();}/*** Initialization of the servlet. br** throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}} 5.3编写处理修改操作的Servlet 在UpdateDeptServlet中修改数据以后显示出数据库表中的全部信息。UpdateDeptServlet的代码如下 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 is called when a form has 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 an error occurred* throws IOException if an error occurred*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {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 is called when a form has 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 an error occurred* throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType(text/html;charsetgb2312);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(创建驱动成功);con DriverManager.getConnection(jdbc:mysql://localhost:3306/bank, root, 1234);System.out.println(数据库连接成功);String sql UPDATE dept SET id?,address?,empnumber?,d_name? WHERE id?;ps con.prepareStatement(sql);//下面设置修改的数据值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(修改成功);/** 添加成功后显示全部信息*/sta con.createStatement();rs sta.executeQuery(SELECT * FROM dept);//在页面中显示表中的所有信息out.println(htmlheadtitle部门表信息/title/headbody);out.println(h1部门表信息:/h1brbr);//循环遍历输出查询结果while(rs.next()){out.print(部门编号);out.print(rs.getString(1)\t);out.print(部门名称);out.print(rs.getString(2)\t);out.print(部门地址);out.print(rs.getString(3)\t);out.print(部门人数);out.print(rs.getString(4)\t);out.println(br);}out.print(/body/html);out.close();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} out.flush();out.close();}/*** Initialization of the servlet. br** throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}} 6. 删除数据 删除数据时要指定删除的条件否则会把表中的所有数据删除。删除以后被删除的数据在表中就不存在了。下面的例子是根据部门编号删除数据的例子。首先在页面中输入要删除的部门编号输入要删除的部门编号的页面代码入下 delete.html !DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
htmlheadtitledelete.html/titlemeta http-equivkeywords contentkeyword1,keyword2,keyword3meta http-equivdescription contentthis is my pagemeta http-equivcontent-type contenttext/html; charsetgb2312!--link relstylesheet typetext/css href./styles.css--/headbodyform namef1 idf1 action/jdbc_servlet/servlet/DeleteByIdServlet methodposttable border0trtd请输入要删除的部门编号:/tdtr/trtdinput typetext nameid /td/trtrtd colspan2 alignleftinput typesubmit value删除/td/tr/table/form/body
/html 当输入删除条件后单击【删除】按钮会进入form表单指定的DeleteByIdServlet中这是一个servlet用来处理删除操作。DeleteByIdServlet中的代码如下 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 is called when a form has 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 an error occurred* throws IOException if an error occurred*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {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 is called when a form has 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 an error occurred* throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType(text/html;charsetgb2312);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(创建驱动成功);con DriverManager.getConnection(jdbc:mysql://localhost:3306/bank, root, 1234);System.out.println(数据库连接成功);String sql DELETE FROM dept WHERE id?;ps con.prepareStatement(sql);ps.setString(1, id);ps.executeUpdate();System.out.println(删除成功);//显示结果信息out.println(htmlheadtitle删除部门表数据/title/headbody);out.println(h1删除部门表数据成功/h1);out.print(/body/html);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}out.flush();out.close();}/*** Initialization of the servlet. br** throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here}} 在DeleteByIdServlet中获得页面传递过来的部门编号然后根据部门编号删除该编号对应的数据删除成功则在页面中提示删除成功。