javaweb中servlet实现删除,删除数据库中的信息爆红
SQL语句不对,可以改为delete from t_user where id=?
这跟上个问题一样,你这是用类名调用的update,这个方法是非静态的,你需要用实例对象才行
另外update是更新吧,你是删除语句,删除语句 where后面 缺少条件字段
直接类名.方法名,那你这个方法需要是static修饰的静态方法,sql语句也不对 delete from t_user where id=?
这不是跟上个问题一样吗
调用非静态的update方法
import org.apache.commons.dbutils.QueryRunner;
public class YourServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 创建QueryRunner实例
QueryRunner queryRunner = new QueryRunner();
// 调用非静态的update方法
try {
int result = queryRunner.update("DELETE FROM your_table WHERE id = ?", yourId);
// 处理结果
} catch (SQLException e) {
e.printStackTrace();
// 处理异常
}
}
}
代码:
reg.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'reg.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
.label{
width: 20%
}
.controler{
width: 80%
}
</style>
<script type="text/javascript" src="js/Calendar3.js"></script>
</head>
<body>
<h1>用户注册</h1>
<hr>
<form name="regForm" action="servlet/RegServlet" method="post" >
<table border="0" width="800" cellspacing="0" cellpadding="0">
<tr>
<td class="lalel">用户名:</td>
<td class="controler"><input type="text" name="username" /></td>
</tr>
<tr>
<td class="label">密码:</td>
<td class="controler"><input type="password" name="mypassword" ></td>
</tr>
<tr>
<td class="label">确认密码:</td>
<td class="controler"><input type="password" name="confirmpass" ></td>
</tr>
<tr>
<td class="label">电子邮箱:</td>
<td class="controler"><input type="text" name="email" ></td>
</tr>
<tr>
<td class="label">性别:</td>
<td class="controler"><input type="radio" name="gendar" checked="checked" value="Male">男<input type="radio" name="gendar" value="Female">女</td>
</tr>
<tr>
<td class="label">出生日期:</td>
<td class="controler">
<input name="birthday" type="text" id="control_date" size="10"
maxlength="10" onclick="new Calendar().show(this);" readonly="readonly" />
</td>
</tr>
<tr>
<td class="label">爱好:</td>
<td class="controler">
<input type="checkbox" name="favorite" value="nba"> NBA
<input type="checkbox" name="favorite" value="music"> 音乐
<input type="checkbox" name="favorite" value="movie"> 电影
<input type="checkbox" name="favorite" value="internet"> 上网
</td>
</tr>
<tr>
<td class="label">自我介绍:</td>
<td class="controler">
<textarea name="introduce" rows="10" cols="40"></textarea>
</td>
</tr>
<tr>
<td class="label">接受协议:</td>
<td class="controler">
<input type="checkbox" name="isAccept" value="true">是否接受霸王条款
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="注册"/>
<input type="reset" value="取消"/>
</td>
</tr>
</table>
</form>
</body>
</html>
userinfo.jsp:
<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'userinfo.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
.title{
width: 30%;
background-color: #CCC;
font-weight: bold;
}
.content{
width:70%;
background-color: #CBCFE5;
}
</style>
</head>
<body>
<h1>用户信息</h1>
<hr>
<center>
<jsp:useBean id="regUser" class="entity.Users" scope="session" />
<table width="600" cellpadding="0" cellspacing="0" border="1">
<tr>
<td class="title">用户名:</td>
<td class="content"> <jsp:getProperty name="regUser" property="username"/></td>
</tr>
<tr>
<td class="title">密码:</td>
<td class="content"> <jsp:getProperty name="regUser" property="mypassword"/></td>
</tr>
<tr>
<td class="title">性别:</td>
<td class="content"> <jsp:getProperty name="regUser" property="gendar"/></td>
</tr>
<tr>
<td class="title">E-mail:</td>
<td class="content"> <jsp:getProperty name="regUser" property="email"/></td>
</tr>
<tr>
<td class="title">出生日期:</td>
<td class="content">
<%
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
String date=sdf.format(regUser.getBirthday());
%>
<%=date %>
</td>
</tr>
<tr>
<td class="title">爱好:</td>
<td class="content">
<%
String[] favorites=regUser.getFavorites();
for(String f:favorites){
%>
<%=f %>
<%
}
%>
</td>
</tr>
<tr>
<td class="title">自我介绍:</td>
<td class="content"> <jsp:getProperty name="regUser" property="introduce"/></td>
</tr>
<tr>
<td class="title">是否介绍协议:</td>
<td class="content"> <jsp:getProperty name="regUser" property="flag"/></td>
</tr>
</table>
</center>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>RegServlet</servlet-name>
<servlet-class>servlet.RegServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegServlet</servlet-name>
<url-pattern>/servlet/RegServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Users.java:
package entity;
import java.util.Date;
//用户实体类
public class Users {
private String username;
private String mypassword;
private String email;
private String gendar;//性别
private Date birthday;
private String[] favorites;//爱好
private String introduce;//自我介绍
private boolean flag;//是否接受协议
public Users() {
super();
}
public Users(String username, String mypassword, String email,
String gendar, Date birthday, String[] favorites, String introduce,
boolean flag) {
super();
this.username = username;
this.mypassword = mypassword;
this.email = email;
this.gendar = gendar;
this.birthday = birthday;
this.favorites = favorites;
this.introduce = introduce;
this.flag = flag;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getMypassword() {
return mypassword;
}
public void setMypassword(String mypassword) {
this.mypassword = mypassword;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGendar() {
return gendar;
}
public void setGendar(String gendar) {
this.gendar = gendar;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String[] getFavorites() {
return favorites;
}
public void setFavorites(String[] favorites) {
this.favorites = favorites;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
regServlet.java:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Users;
public class RegServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public RegServlet() {
super();
}
/**
* 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 {
doPost(request,response);
}
/**
* 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 {
request.setCharacterEncoding("utf-8");
Users u = new Users();
String username,mypassword,gendar,email,introduce,isAccept;
Date birthday;
String[] favorites;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try
{
username = request.getParameter("username");
mypassword = request.getParameter("mypassword");
gendar = request.getParameter("gendar");
email = request.getParameter("email");
introduce = request.getParameter("introduce");
birthday = sdf.parse(request.getParameter("birthday"));
if(request.getParameterValues("isAccpet")!=null)
{
isAccept = request.getParameter("isAccept");
}
else
{
isAccept = "false";
}
//用来获取多个复选按钮的值
favorites = request.getParameterValues("favorite");
u.setUsername(username);
u.setMypassword(mypassword);
u.setGendar(gendar);
u.setEmail(email);
u.setFavorites(favorites);
u.setIntroduce(introduce);
if(isAccept.equals("true"))
{
u.setFlag(true);
}
else
{
u.setFlag(false);
}
u.setBirthday(birthday);
//把注册成功的用户对象保存在session中
//regUser和userinfo.jsp中的jsp:useBean的id进行对应
request.getSession().setAttribute("regUser", u);
//跳转到注册成功页面
request.getRequestDispatcher("../userinfo.jsp").forward(request,response);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
结果:
用户信息
用户名: 123
密码: 123
性别: Male
E-mail: 123
出生日期: 2016年10月02日
爱好: music movie internet
自我介绍: 123
是否介绍协议: false
小结:
1、我们自己编写的Servlet继承了HttpServlet类,一般只需覆盖doPost或者doGet方法,不必覆盖service( )方法,因为一个sevrvice( )方法是空的。
2、HttpServlet类扩展了GenericServlet类,实现了GenericServlet类的抽象方法sevrvice( )
3、HttpServlet类有两个sevrvice( )方法