5.修改数据

5.1编写查询条件页面

修改单条数据的时候,首先是查询出单个数据的详细信息,然后根据实际需要部分修改或者全部修改。修改之后,数据会提交到数据库,数据库中保存更新以后的数据。

查询出单条数据的查询条件页面代码如下:

QueryToUpdate.html

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>QueryToUpdate.html</title>
  5.  
  6. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  7. <meta http-equiv="description" content="this is my page">
  8. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  9.  
  10. <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  11.  
  12. </head>
  13.  
  14. <body>
  15. <form name="f1" id="f1" action="/jdbc_servlet/servlet/QueryToUpdateServlet" method="post">
  16. <table border="0">
  17. <tr>
  18. <td>请输入要修改的部门编号:</td><tr></tr>
  19. <td><input type="text" name="id" ></td>
  20. </tr>
  21. <tr>
  22. <td colspan="2" align="left"><input type="submit" value="提交"></td>
  23. </tr>
  24. </table>
  25. </form>
  26. </body>
  27. </html>

5.2 编写显示部门详细信息的Servlet

输入要修改的部门编号以后,进入根据部门编号查询部门信息的Servlet,把部门详细信息显示到页面中,一些不可以修改的字段可以设置成只读,这样就不会把这些数据修改了,该Servlet的代码如下:

QueryToUpdateServlet.java

  1. package com.cn.update;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10.  
  11. import javax.servlet.ServletException;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15.  
  16. public class QueryToUpdateServlet extends HttpServlet {
  17.  
  18. /**
  19. * Destruction of the servlet. <br>
  20. */
  21. public void destroy() {
  22. super.destroy(); // Just puts "destroy" string in log
  23. // Put your code here
  24. }
  25.  
  26. /**
  27. * The doGet method of the servlet. <br>
  28. *
  29. * This method is called when a form has its tag value method equals to get.
  30. *
  31. * @param request the request send by the client to the server
  32. * @param response the response send by the server to the client
  33. * @throws ServletException if an error occurred
  34. * @throws IOException if an error occurred
  35. */
  36. public void doGet(HttpServletRequest request, HttpServletResponse response)
  37. throws ServletException, IOException {
  38.  
  39. response.setContentType("text/html;charset=gb2312");
  40. request.setCharacterEncoding("gb2312");
  41. PrintWriter out = response.getWriter();
  42. String id = request.getParameter("id");
  43. Connection con = null;
  44. PreparedStatement pstmt = null;
  45. ResultSet rs = null;
  46. try {
  47. Class.forName("com.mysql.jdbc.Driver");
  48. System.out.println("创建驱动成功!");
  49. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "1234");
  50. System.out.println("数据库连接成功!");
  51. String sql = "SELECT * FROM dept WHERE id=?";
  52. pstmt = con.prepareStatement(sql);
  53. pstmt.setString(1, id);
  54. rs = pstmt.executeQuery();
  55. } catch (ClassNotFoundException e) {
  56. // TODO Auto-generated catch block
  57. e.printStackTrace();
  58. } catch (SQLException e) {
  59. // TODO Auto-generated catch block
  60. e.printStackTrace();
  61. }
  62. //显示单个部门的信息
  63. out.println("<html>"
  64. + "<head><title>显示单个部门信息</title></head>"
  65. + "<body>");
  66. out.println("<h1>显示单个部门信息</h1><br><br>");
  67. out.print("<form action='/jdbc_servlet/servlet/UpdateDeptServlet' method='post'>");
  68. try {
  69. while(rs.next()){
  70. out.println("部门编号:");
  71. out.print("<br>");
  72. //在文本框中显示部门编号,设置成只读
  73. out.println("<input type='text' name='id' readonly='true' value=");
  74. out.println(rs.getString(1).toString());
  75. out.print(">");
  76. out.print("<br>");
  77. out.println("部门名称:");
  78. out.print("<br>");
  79. //在文本框中显示部门名称
  80. out.println("<input type='text' name='d_name' value=");
  81. out.println(rs.getString(2).toString());
  82. out.print(">");
  83. out.print("<br>");
  84. out.println("部门地址:");
  85. out.print("<br>");
  86. //在文本框中显示部门地址
  87. out.println("<input type='text' name='address' value=");
  88. out.println(rs.getString(3).toString());
  89. out.print(">");
  90. out.print("<br>");
  91. out.println("部门人数:");
  92. out.print("<br>");
  93. //在文本框中显示部门人数
  94. out.println("<input type='text' name='empnumber' value=");
  95. out.println(rs.getString(4).toString());
  96. out.print(">");
  97. out.print("<br>");
  98. //提交按钮
  99. out.print("<input type='submit' value='Submit'>");
  100. out.print("</form>");
  101.  
  102. }
  103. } catch (SQLException e) {
  104. // TODO Auto-generated catch block
  105. e.printStackTrace();
  106. }
  107. out.flush();
  108. out.close();
  109. }
  110.  
  111. /**
  112. * The doPost method of the servlet. <br>
  113. *
  114. * This method is called when a form has its tag value method equals to post.
  115. *
  116. * @param request the request send by the client to the server
  117. * @param response the response send by the server to the client
  118. * @throws ServletException if an error occurred
  119. * @throws IOException if an error occurred
  120. */
  121. public void doPost(HttpServletRequest request, HttpServletResponse response)
  122. throws ServletException, IOException {
  123.  
  124. response.setContentType("text/html;charset=gb2312");
  125. request.setCharacterEncoding("gb2312");
  126. PrintWriter out = response.getWriter();
  127. this.doGet(request, response);
  128. out.flush();
  129. out.close();
  130. }
  131.  
  132. /**
  133. * Initialization of the servlet. <br>
  134. *
  135. * @throws ServletException if an error occurs
  136. */
  137. public void init() throws ServletException {
  138. // Put your code here
  139. }
  140.  
  141. }

5.3编写处理修改操作的Servlet

在UpdateDeptServlet中,修改数据以后,显示出数据库表中的全部信息。UpdateDeptServlet的代码如下:

UpdateDeptServlet.java

  1. package com.cn.update;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11.  
  12. import javax.servlet.ServletException;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16.  
  17. public class UpdateDeptServlet extends HttpServlet {
  18.  
  19. /**
  20. * The doGet method of the servlet. <br>
  21. *
  22. * This method is called when a form has its tag value method equals to get.
  23. *
  24. * @param request the request send by the client to the server
  25. * @param response the response send by the server to the client
  26. * @throws ServletException if an error occurred
  27. * @throws IOException if an error occurred
  28. */
  29. public void doGet(HttpServletRequest request, HttpServletResponse response)
  30. throws ServletException, IOException {
  31.  
  32. response.setContentType("text/html");
  33. PrintWriter out = response.getWriter();
  34. this.doPost(request, response);
  35. out.flush();
  36. out.close();
  37. }
  38.  
  39. /**
  40. * The doPost method of the servlet. <br>
  41. *
  42. * This method is called when a form has its tag value method equals to post.
  43. *
  44. * @param request the request send by the client to the server
  45. * @param response the response send by the server to the client
  46. * @throws ServletException if an error occurred
  47. * @throws IOException if an error occurred
  48. */
  49. public void doPost(HttpServletRequest request, HttpServletResponse response)
  50. throws ServletException, IOException {
  51.  
  52. response.setContentType("text/html;charset=gb2312");
  53. request.setCharacterEncoding("gb2312");
  54. PrintWriter out = response.getWriter();
  55. Connection con = null;
  56. PreparedStatement ps = null;
  57. ResultSet rs = null;
  58. Statement sta = null;
  59. String id = request.getParameter("id");
  60. String address = request.getParameter("address");
  61. int empnumber = Integer.parseInt(request.getParameter("empnumber"));
  62. String d_name = request.getParameter("d_name");
  63. try {
  64. Class.forName("com.mysql.jdbc.Driver");
  65. System.out.println("创建驱动成功!");
  66. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "1234");
  67. System.out.println("数据库连接成功!");
  68. String sql = "UPDATE dept SET id=?,address=?,empnumber=?,d_name=? WHERE id=?";
  69. ps = con.prepareStatement(sql);
  70. //下面设置修改的数据值
  71. ps.setString(1, id);
  72. ps.setString(2, address);
  73. ps.setInt(3, empnumber);
  74. ps.setString(4, d_name);
  75. ps.setString(5, id);
  76. ps.executeUpdate();
  77. System.out.println("修改成功!");
  78. /*
  79. * 添加成功后,显示全部信息
  80. */
  81. sta = con.createStatement();
  82. rs = sta.executeQuery("SELECT * FROM dept");
  83. //在页面中显示表中的所有信息
  84. out.println(
  85. "<html>"+
  86. "<head><title>部门表信息</title></head>"+
  87. "<body>");
  88. out.println("<h1>部门表信息:</h1><br><br>");
  89. //循环遍历输出查询结果
  90. while(rs.next()){
  91. out.print("部门编号:");
  92. out.print(rs.getString(1)+"\t");
  93. out.print("部门名称:");
  94. out.print(rs.getString(2)+"\t");
  95. out.print("部门地址:");
  96. out.print(rs.getString(3)+"\t");
  97. out.print("部门人数:");
  98. out.print(rs.getString(4)+"\t");
  99. out.println("<br>");
  100. }
  101. out.print("</body></html>");
  102. out.close();
  103. } catch (ClassNotFoundException e) {
  104. // TODO Auto-generated catch block
  105. e.printStackTrace();
  106. } catch (SQLException e) {
  107. // TODO Auto-generated catch block
  108. e.printStackTrace();
  109. }
  110. out.flush();
  111. out.close();
  112. }
  113.  
  114. /**
  115. * Initialization of the servlet. <br>
  116. *
  117. * @throws ServletException if an error occurs
  118. */
  119. public void init() throws ServletException {
  120. // Put your code here
  121. }
  122.  
  123. }

6. 删除数据

删除数据时,要指定删除的条件,否则会把表中的所有数据删除。删除以后,被删除的数据在表中就不存在了。下面的例子是根据部门编号删除数据的例子。首先在页面中输入要删除的部门编号,输入要删除的部门编号的页面代码入下:

delete.html

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>delete.html</title>
  5.  
  6. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  7. <meta http-equiv="description" content="this is my page">
  8. <meta http-equiv="content-type" content="text/html; charset=gb2312">
  9.  
  10. <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  11.  
  12. </head>
  13.  
  14. <body>
  15. <form name="f1" id="f1" action="/jdbc_servlet/servlet/DeleteByIdServlet" method="post">
  16. <table border="0">
  17. <tr>
  18. <td>请输入要删除的部门编号:</td><tr></tr>
  19. <td><input type="text" name="id" ></td>
  20. </tr>
  21. <tr>
  22. <td colspan="2" align="left"><input type="submit" value="删除"></td>
  23. </tr>
  24. </table>
  25. </form>
  26. </body>
  27. </html>

当输入删除条件后,单击【删除】按钮,会进入form表单指定的DeleteByIdServlet中,这是一个servlet,用来处理删除操作。DeleteByIdServlet中的代码如下:

DeleteByIdServlet.java

  1. package com.cn.delete;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10.  
  11. import javax.servlet.ServletException;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15.  
  16. public class DeleteByIdServlet extends HttpServlet {
  17.  
  18. /**
  19. * The doGet method of the servlet. <br>
  20. *
  21. * This method is called when a form has its tag value method equals to get.
  22. *
  23. * @param request the request send by the client to the server
  24. * @param response the response send by the server to the client
  25. * @throws ServletException if an error occurred
  26. * @throws IOException if an error occurred
  27. */
  28. public void doGet(HttpServletRequest request, HttpServletResponse response)
  29. throws ServletException, IOException {
  30.  
  31. response.setContentType("text/html");
  32. PrintWriter out = response.getWriter();
  33. this.doPost(request, response);
  34. out.flush();
  35. out.close();
  36. }
  37.  
  38. /**
  39. * The doPost method of the servlet. <br>
  40. *
  41. * This method is called when a form has its tag value method equals to post.
  42. *
  43. * @param request the request send by the client to the server
  44. * @param response the response send by the server to the client
  45. * @throws ServletException if an error occurred
  46. * @throws IOException if an error occurred
  47. */
  48. public void doPost(HttpServletRequest request, HttpServletResponse response)
  49. throws ServletException, IOException {
  50.  
  51. response.setContentType("text/html;charset=gb2312");
  52. request.setCharacterEncoding("gb2312");
  53. PrintWriter out = response.getWriter();
  54. String id = request.getParameter("id");
  55. Connection con = null;
  56. // ResultSet rs = null;
  57. PreparedStatement ps = null;
  58. try {
  59. Class.forName("com.mysql.jdbc.Driver");
  60. System.out.println("创建驱动成功!");
  61. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bank", "root", "1234");
  62. System.out.println("数据库连接成功!");
  63. String sql = "DELETE FROM dept WHERE id=?";
  64. ps = con.prepareStatement(sql);
  65. ps.setString(1, id);
  66. ps.executeUpdate();
  67. System.out.println("删除成功!");
  68. //显示结果信息
  69. out.println("<html><head><title>"
  70. +"删除部门表数据</title></head>"+"<body>");
  71. out.println("<h1>删除部门表数据成功!</h1>");
  72. out.print("</body></html>");
  73. } catch (ClassNotFoundException e) {
  74. // TODO Auto-generated catch block
  75. e.printStackTrace();
  76. } catch (SQLException e) {
  77. // TODO Auto-generated catch block
  78. e.printStackTrace();
  79. }
  80. out.flush();
  81. out.close();
  82. }
  83.  
  84. /**
  85. * Initialization of the servlet. <br>
  86. *
  87. * @throws ServletException if an error occurs
  88. */
  89. public void init() throws ServletException {
  90. // Put your code here
  91. }
  92.  
  93. }

在DeleteByIdServlet中,获得页面传递过来的部门编号,然后根据部门编号删除该编号对应的数据,删除成功则在页面中提示删除成功。

html调用servlet(JDBC在Servlet中的使用)(2)的更多相关文章

  1. html调用servlet(JDBC在Servlet中的使用)(1)

    1.页面的数据表单 在使用Servlet处理用户请求之前,先准备一个页面,该页面用来提供数据表单.数据表单就是HTML中的<form>...</form>部分,当用户单击Sub ...

  2. Java Servlet(三):Servlet中ServletConfig对象和ServletContext对象

    本文将记录ServletConfig/ServletContext中提供了哪些方法,及方法的用法. ServletConfig是一个抽象接口,它是由Servlet容器使用,在一个servlet对象初始 ...

  3. 登录小项目 js+servlet+jdbc+mvc

    项目名称: 沪上阿姨 实现需求: 实现用户登录 实现用户退出 实现用户注册 功能分析: 用户登录: 根据用户名和密码查询用户信息.查询则登录成功,查不到则登录失败. 用户退出: 销毁session 用 ...

  4. JDBC和servlet设计思路、DAO模式思路、MVC思路粗略总结

    #JDBC和Servlet联合起来使用的项目思路: 说明:建库,最好一开始设置utf8字符集 step1: 在数据库中建表 如   create table t_user{ ...... } step ...

  5. Jsp+Servlet+JDBC的使用复习

    最近对JDBC进行了复习,对事物的理解,连接池的使用等部分都有一个复习,所以使用Servlet+JDBC完成了一个小Demo,在这里对这种底层的操作进行总结.框架的使用的确方便了我们的开发,但是底层的 ...

  6. java web开发基础实例(javabean+jsp+servlet+jdbc)

    JavaBean:用于传递数据,拥有与数据相关的逻辑处理 JSP:从Model接收数据并生成HTML Servlet:接收HTTP请求并控制Model和View jdbc:用于配置环境 一.[建立数据 ...

  7. 使用JSP+Servlet+Jdbc+Echatrs实现对豆瓣电影Top250的展示

    使用JSP+Servlet+Jdbc+Echatrs实现对豆瓣电影Top250的展示 写在前面: 有的小伙伴,会吐槽啦,你这个标题有点长的啊.哈哈 ,好像是的!不过,这个也是本次案例中使用到的关键技术 ...

  8. 使用servlet+jdbc+MD5实现用户加密登录

    /** * 分析流程: * 1.前端页面提交登录请求 * 2.被web.xml拦截,进入到LoginServlet(有两种方式:方式一,在web.xml文件中配置servlet拦截器;方式二,不用在w ...

  9. Servlet从本地文件中读取图片,并显示在页面中

    import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpSer ...

  10. MVC-1(javabean+jsp+servlet+jdbc)

    这是一篇最初版本的mvc设计模式的demo.路要一步一步走,弄明白这其中的逻辑,对后面掌握ssh,ssm等框架大有裨益. 计算机系的同学们也要为毕设做准备了,希望可以帮你们迈出自己做毕设的第一步(微笑 ...

随机推荐

  1. LoadRunner FAQ2

    LR中参数的定义和赋值 1.参数的赋值和取值 lr_save_string("hello world","param"); lr_eval_string(&qu ...

  2. log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).

    一.异常描述: log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLo ...

  3. hdoj1171 Big Event in HDU(01背包 || 多重背包)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1171 题意 老师有一个属性:价值(value).在学院里的老师共有n种价值,每一种价值value对应着 ...

  4. [漏洞复现]CVE-2010-2883 Adobe Reader 打开pdf电脑即刻中招

    1.漏洞概述: CVE-2010-2883漏洞原理:“Adobe Reader在处理CoolType字体文件的sing表时,存在栈溢出漏洞,当打开特制的恶意PDF文件时,可允许任意代码远程执行.” 影 ...

  5. Block的基本用法

    NSString* (^myBlock)(NSString*, int); myBlock = ^(NSString *name, int age){ return [NSString stringW ...

  6. win 10 文件夹 背景 没效果

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha  313134555@qq.com

  7. Git 历险记

    Git历险记(一) 作为分布式版本控制系统的重要代表--Git已经为越来越多的人所认识,它相对于我们熟悉的CVS.SVN甚至同时分布式控制系统的Mercurial,有哪些优势和不足呢.这次InfoQ中 ...

  8. bzoj 3669 lct维护最小生成树

    大概题意:给一个无向图,有a,b两种边权,找一条从1到n的路径,使得max(a[i])+max(b[i])最小a[i],b[i]表示该路径上的边的对应权. 如果用类似最短路的DP来做,显然每个点的状态 ...

  9. Codeforces Round #354 (Div. 2) B. Pyramid of Glasses 模拟

    B. Pyramid of Glasses 题目连接: http://www.codeforces.com/contest/676/problem/B Description Mary has jus ...

  10. Codeforces Round #287 (Div. 2) A. Amr and Music 水题

    A. Amr and Music time limit per test 1 second memory limit per test 256 megabytes input standard inp ...