先创建dao package 下的UserDao类 model下的UserService类,sevlet下的AddServlet、HelloServlet、LoginServlet类,util下的DButil类。
在web下导入两个jar包并创建add.jsp、delete.jsp,index.jsp
实体类User属性设置
package com.zr0629.model;
public class User {
private int id;
private String name;
private String password;
private String age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", age='" + age + '\'' +
'}';
}
}
数据库连接
package com.zr0629.util;
import java.sql.*;
public class DButil {
public static Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("连接数据库");
//创建链接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/testtt?useSSL=false","root","123456");
return connection;
}
public static void closeAll(ResultSet rs, Statement stmt,Connection conn) throws SQLException{
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
}
UserDao类
package com.zr0629.dao;
import com.zr0629.model.User;
import com.zr0629.util.DButil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class UserDao {
public int add(User user) {
Connection connection=null;
PreparedStatement pstmt = null;
int count=0;
try{
connection = DButil.getConnection();
//获得执行sql的Statement对象
pstmt = connection.prepareStatement("insert into a(name,password,age) values (?,?,?)");
pstmt.setString(1,user.getName());
pstmt.setString(2,user.getPassword());
pstmt.setString(3,user.getAge());
//执行sql,获得结果
count=pstmt.executeUpdate();
System.out.println("insert操作:"+count);
return count;
}catch (Exception e){
e.printStackTrace();
}finally {
try {
DButil.closeAll(null,pstmt,connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return count;
}
public int del(User user) {
Connection connection=null;
PreparedStatement pstmt = null;
int count=0;
try{
connection = DButil.getConnection();
//获得执行sql的Statement对象
pstmt = connection.prepareStatement("DELETE FROM `a` WHERE name=? AND password=? AND age = ?");
pstmt.setString(1,user.getName());
pstmt.setString(2,user.getPassword());
pstmt.setString(3,user.getAge());
//执行sql,获得结果
count=pstmt.executeUpdate();
System.out.println("delete操作:"+count);
return count;
}catch (Exception e){
e.printStackTrace();
}finally {
try {
DButil.closeAll(null,pstmt,connection);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return count;
}
}
UserService类
package com.zr0629.service;
import com.zr0629.dao.UserDao;
import com.zr0629.model.User;
import java.sql.SQLException;
public class UserService {
UserDao userDao=new UserDao();
public int add(User user) {
System.out.println("service中add方法被调用");
return userDao.add(user);
}
public int del(User user) {
System.out.println("service中del方法被调用");
return userDao.del(user);
}
}
AddServlet类
package com.zr0629.servlet;
import com.zr0629.model.User;
import com.zr0629.service.UserService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
public class AddServlet extends HttpServlet {
UserService userService=new UserService();
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("执行了doPost");
String method = req.getParameter("method");
if(method.equals("save")){
insert(req, resp);
} else if(method.equals("delete")){
delete(req,resp);
}
}
public void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User user=new User();
String name=req.getParameter("name");
String password=req.getParameter("password");
String age=req.getParameter("age");
user.setName(name);
user.setPassword(password);
user.setAge(age);
System.out.println(user);
int count= userService.add(user);
if(count>0){
resp.sendRedirect("/index.jsp");
}else{
resp.getWriter().write("failed");
}
}
public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User user=new User();
String name=req.getParameter("name");
String password=req.getParameter("password");
String age=req.getParameter("age");
user.setName(name);
user.setPassword(password);
user.setAge(age);
System.out.println(user);
int count= userService.del(user);
if(count>0){
resp.sendRedirect("/index.jsp");
}else{
resp.getWriter().write("failed");
}
}
}
登录index.jsp
<head>
<title>$Title$</title>
</head>
<body>
<h1>登陆页面</h1>
<form action="/login" method="post">
name:<input name="name" type="text">
password:<input name="password" type="password">
<input type="submit" value="login">
</form>
<a href="add.jsp">跳转到注册页面</a>
<a href="delete.jsp">跳转到删除页面</a>
</body>
</html>
注册add.jsp
<head>
<title>注册</title>
</head>
<body>
<h1>注册页面</h1>
<form action="/add?method=save"method="post">
name:<input name="name"type="text">
password:<input name="password"type="password">
age:<input name="age"type="text">
<input type="submit" value="注册">
</form>
</body>
</html>
删除(注销)delete.jsp
<html>
<head>
<title>删除</title>
</head>
<body>
<h1>删除页面</h1>
<form action="/add?method=delete"method="post">
name:<input name="name"type="text">
password:<input name="password"type="password">
age:<input name="age"type="text">
<input type="submit" value="删除">
</form>
</body>
</html>