html调用servlet(JDBC在Servlet中的使用)(2)
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(JDBC在Servlet中的使用)(2)的更多相关文章
- html调用servlet(JDBC在Servlet中的使用)(1)
1.页面的数据表单 在使用Servlet处理用户请求之前,先准备一个页面,该页面用来提供数据表单.数据表单就是HTML中的<form>...</form>部分,当用户单击Sub ...
- Java Servlet(三):Servlet中ServletConfig对象和ServletContext对象
本文将记录ServletConfig/ServletContext中提供了哪些方法,及方法的用法. ServletConfig是一个抽象接口,它是由Servlet容器使用,在一个servlet对象初始 ...
- 登录小项目 js+servlet+jdbc+mvc
项目名称: 沪上阿姨 实现需求: 实现用户登录 实现用户退出 实现用户注册 功能分析: 用户登录: 根据用户名和密码查询用户信息.查询则登录成功,查不到则登录失败. 用户退出: 销毁session 用 ...
- JDBC和servlet设计思路、DAO模式思路、MVC思路粗略总结
#JDBC和Servlet联合起来使用的项目思路: 说明:建库,最好一开始设置utf8字符集 step1: 在数据库中建表 如 create table t_user{ ...... } step ...
- Jsp+Servlet+JDBC的使用复习
最近对JDBC进行了复习,对事物的理解,连接池的使用等部分都有一个复习,所以使用Servlet+JDBC完成了一个小Demo,在这里对这种底层的操作进行总结.框架的使用的确方便了我们的开发,但是底层的 ...
- java web开发基础实例(javabean+jsp+servlet+jdbc)
JavaBean:用于传递数据,拥有与数据相关的逻辑处理 JSP:从Model接收数据并生成HTML Servlet:接收HTTP请求并控制Model和View jdbc:用于配置环境 一.[建立数据 ...
- 使用JSP+Servlet+Jdbc+Echatrs实现对豆瓣电影Top250的展示
使用JSP+Servlet+Jdbc+Echatrs实现对豆瓣电影Top250的展示 写在前面: 有的小伙伴,会吐槽啦,你这个标题有点长的啊.哈哈 ,好像是的!不过,这个也是本次案例中使用到的关键技术 ...
- 使用servlet+jdbc+MD5实现用户加密登录
/** * 分析流程: * 1.前端页面提交登录请求 * 2.被web.xml拦截,进入到LoginServlet(有两种方式:方式一,在web.xml文件中配置servlet拦截器;方式二,不用在w ...
- Servlet从本地文件中读取图片,并显示在页面中
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpSer ...
- MVC-1(javabean+jsp+servlet+jdbc)
这是一篇最初版本的mvc设计模式的demo.路要一步一步走,弄明白这其中的逻辑,对后面掌握ssh,ssm等框架大有裨益. 计算机系的同学们也要为毕设做准备了,希望可以帮你们迈出自己做毕设的第一步(微笑 ...
随机推荐
- Think PHP 3.2 界面及JS多语言实现
1.多语言实现的原理在实现多语言的时候需要调用L()函数.那么L函数是如何实现多语言的输出呢?在L函数内部有一个静态变量$_lang一维数组.所有的语言数据都存在在这个数组中.系统在加载的时候根据选择 ...
- 【运维理论】RAID级别简介
独立硬盘冗余阵列(RAID, Redundant Array of Independent Disks),旧称廉价磁盘冗余阵列(RAID, Redundant Array of Inexpensive ...
- Mybatis框架简单使用
Mybatis框架简单使用 环境搭建 新建一个JavaWeb项目,在web\WEB-INF\创建lib文件,并且在其下添加Mybatis的核心包以及依赖包,以及Mysql驱动包,junit4测试包等. ...
- python opencv3 圆检测
git:https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 import numpy as np img_ori ...
- Git 统计提交代码行数
指定用户名 git log --author="your_name_here" --pretty=tformat: --numstat | awk '{ add += $1; su ...
- BZOJ 2726: [SDOI2012]任务安排 [斜率优化DP 二分 提前计算代价]
2726: [SDOI2012]任务安排 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 868 Solved: 236[Submit][Status ...
- luoguP3979 遥远的国度 树链剖分
\(1, 2\)操作没什么好说的 对于\(3\)操作,分三种情况讨论下 \(id = rt\)的情况下,查整棵树的最小值即可 如果\(rt\)在\(1\)号点为根的情况下不在\(id\)的子树中,那么 ...
- 【BFS】【最小生成树】Petrozavodsk Winter Training Camp 2018 Day 1: Jagiellonian U Contest, Tuesday, January 30, 2018 Problem G. We Need More Managers!
题意:给你n个点,点带权,任意两点之间的边权是它们的点权的异或值中“1”的个数,问你该图的最小生成树. 看似是个完全图,实际上有很多边是废的.类似……卡诺图的思想?从读入的点出发BFS,每次只到改变它 ...
- XP下安装Centos 6.4 双系统 :Linux系统分区及挂载点,关键引导程序启动设置
一.关于Linux的分区情况 虽然硬盘分区表中最多能存储四个分区,但我们实际使用时一般只分为两个分区,一个是主分区(Primary Partion)一个是扩展分区(extended partition ...
- 可变参数模拟printf()函数实现一个my_print()函数以及调用可变参数需注意的陷阱
入栈规则 可变参数函数的实现与函数调用的栈帧结构是密切相关的.所以在我们实现可变参数之前,先得搞清楚 栈是怎样传参的. 正常情况下,C的函数参数入栈遵照__stdcall规则, 它是从右到左的,即函数 ...