5.修改数据

5.1编写查询条件页面

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

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

QueryToUpdate.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>QueryToUpdate.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body>
<form name="f1" id="f1" action="/jdbc_servlet/servlet/QueryToUpdateServlet" method="post">
<table border="0">
<tr>
<td>请输入要修改的部门编号:</td><tr></tr>
<td><input type="text" name="id" ></td>
</tr>
<tr>
<td colspan="2" align="left"><input type="submit" 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;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("创建驱动成功!");
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 block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//显示单个部门的信息
out.println("<html>"
+ "<head><title>显示单个部门信息</title></head>"
+ "<body>");
out.println("<h1>显示单个部门信息</h1><br><br>");
out.print("<form action='/jdbc_servlet/servlet/UpdateDeptServlet' method='post'>");
try {
while(rs.next()){
out.println("部门编号:");
out.print("<br>");
//在文本框中显示部门编号,设置成只读
out.println("<input type='text' name='id' readonly='true' value=");
out.println(rs.getString(1).toString());
out.print(">");
out.print("<br>");
out.println("部门名称:");
out.print("<br>");
//在文本框中显示部门名称
out.println("<input type='text' name='d_name' value=");
out.println(rs.getString(2).toString());
out.print(">");
out.print("<br>");
out.println("部门地址:");
out.print("<br>");
//在文本框中显示部门地址
out.println("<input type='text' name='address' value=");
out.println(rs.getString(3).toString());
out.print(">");
out.print("<br>");
out.println("部门人数:");
out.print("<br>");
//在文本框中显示部门人数
out.println("<input type='text' name='empnumber' value=");
out.println(rs.getString(4).toString());
out.print(">");
out.print("<br>");
//提交按钮
out.print("<input type='submit' value='Submit'>");
out.print("</form>"); }
} catch (SQLException e) {
// TODO Auto-generated catch block
e.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;charset=gb2312");
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;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("创建驱动成功!");
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(
"<html>"+
"<head><title>部门表信息</title></head>"+
"<body>");
out.println("<h1>部门表信息:</h1><br><br>");
//循环遍历输出查询结果
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 block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.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">
<html>
<head>
<title>delete.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=gb2312"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body>
<form name="f1" id="f1" action="/jdbc_servlet/servlet/DeleteByIdServlet" method="post">
<table border="0">
<tr>
<td>请输入要删除的部门编号:</td><tr></tr>
<td><input type="text" name="id" ></td>
</tr>
<tr>
<td colspan="2" align="left"><input type="submit" 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;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("创建驱动成功!");
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("<html><head><title>"
+"删除部门表数据</title></head>"+"<body>");
out.println("<h1>删除部门表数据成功!</h1>");
out.print("</body></html>");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.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中,获得页面传递过来的部门编号,然后根据部门编号删除该编号对应的数据,删除成功则在页面中提示删除成功。

HTML调用servlet(二)的更多相关文章

  1. JavaWeb学习 (六)————Servlet(二)

    一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...

  2. web开发之Servlet 二

    在上一篇文章中,我们演示也证明了Servlet 是一种动态web资源开发的技术,即我可以在浏览器中输入URL,然后就可以在浏览器中看到我们编写的Servlet资源. 那当我们在浏览器上一起一个HTTP ...

  3. 关于jsp的action如何调用servlet的自定义方法

    一.起因: 希望将同属于某个模块的简单功能整合到一起,不创建太多的servlet 二.问题描述: action或者method属性是否能直接调用自定义方法 三.补充知识点: 查询得知:servelet ...

  4. .Net组件程序设计之远程调用(二)

    .Net组件程序设计之远程调用(二) 激活模式 引用封送对象激活类型两种, 一种是客户端激活类型,一种是服务器端激活. 客户端激活对象 客户端激活方式:当客户端创建一个远程对象时,客户端得到的是一个新 ...

  5. servlet二

    ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些初始 ...

  6. Java Servlet(二):servlet配置及生命周期相关(jdk7+tomcat7+eclipse)

    该篇文章记录了Servlet配置相关用法及Servlet在Servlet容器中生命周期方法. Tomcat是一个Servlet容器: 1.Servlet容器管理了Servlet的整个生命周期,并调用s ...

  7. C/C++调用Golang 二

    C/C++调用Golang 二 <C/C++调用Golang 一>简单介绍了C/C++调用Golang的方法步骤,只涉及一个简单的函数调用.本文总结具体项目中的使用场景,将介绍三种较复杂的 ...

  8. Servlet主要相关类核心类 容器调用的过程浅析 servlet解读 怎么调用 Servlet是什么 工作机制

      WEB简介   Web项目 是 B/S结构 浏览器/服务器模式的 浏览器发起请求,服务器作出响应   请求的发起和响应使用HTTP协议进行通讯 所谓协议也就是一种固定格式   而Socket是应用 ...

  9. html文件form表单action调用servlet连接mysql数据库实例

    web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&qu ...

随机推荐

  1. OpenGL完全教程 第一章 初始化OpenGL

    第一章 初始化OpenGL 无论是什么东西,要使用它,就必须对它进行初始化.如果你之前使用过GDI,你应该也多多少少了解到GDI在绘制图形之前要为之创建渲染环境.OpenGL也一样.本章给出的代码,大 ...

  2. WSDL Style和use的组合方式说明

    原文地址:http://www.ibm.com/developerworks/cn/webservices/ws-whichwsdl/#listing9 use属性可以为literal,encoded ...

  3. J2msi 自己制作的把exe打成安装包简易GUI程序(第二版 带DLL注册)

    J2msi 自己制作的把exe打成安装包简易GUI程序(第二版 带DLL注册) 之前那一版本(http://www.cnblogs.com/rojas/p/4794684.html)没考虑 DLL 注 ...

  4. C#窗体:关于DataGridView的数据源绑定字符串两个值得注意的问题

    无意间遇到的问题,然后就GOOGLE了下,搜到些资料,总结整理如下(注:是转载的) 1. LINQ的查询结果无法直接作为DataGridView的数据源 DataGridView的DataSource ...

  5. CRUD操作 create创建 read读取 update修改 delete删除

    1.注释语法:--,#2.后缀是.sql的文件是数据库查询文件3.保存查询4.在数据库里面 列有个名字叫字段 行有个名字叫记录 CRUD操作:create 创建(添加)read 读取update 修改 ...

  6. LinQ总结

    不管是在Mvc还是在别的架构中的项目LinQ和Lambda总是经常会遇到的. 而有些LinQ的语法并不是很长用(我大部分用的是Lambda),所以有必要记录一下万一用到的时候我能很方便的找到我想找到的 ...

  7. jquerymobile动态添加元素之后不能正确渲染解决方法

    jquerymobile动态添加元素之后有些不能被正确渲染的解决方法: listview:               添加 jq(".detail").listview(&quo ...

  8. 经常在eclipse中导入web项目时,出现转不了项目类型的问题,导入后就是一个java项目。

    1.在eclipse的项目上点右键,刷新项目.2.在项目上点右键,进入属性(properties)3.在左侧列表项目中点击选择“Project Facets”,在右侧选择“Dynamic Web Mo ...

  9. memory_limit的一个bug | 风雪之隅

    原文:memory_limit的一个bug | 风雪之隅 27 Nov 09 memory_limit的一个bug 作者: Laruence( ) 本文地址: http://www.laruence. ...

  10. H264关于RTP协议的实现

    完整的C/S架构的基于RTP/RTCP的H.264视频传输方案.此方案中,在服务器端和客户端分别进行了功能模块设计. 服务器端:RTP封装模块主要是对H.264码流进行打包封装:RTCP分析模块负责产 ...