学习java的第二十天
- 实体类Users
- 工具类DBManager
- 数据库操作类UsersDAO
- 注册servlet RegServlet
- 登录servlet LoginServlet
- 注册页面 reg.html
- 登录页面 login.html
- 成功页面 success.html
- 失败页面 fail.html
实体类Users
package com.qf.entity;
public class Users {
private int id;
private String username;
private String pwd;
private int age;
private String sex;
public Users() {
super();
}
public Users(int id, String username, String pwd, int age, String sex) {
super();
this.id = id;
this.username = username;
this.pwd = pwd;
this.age = age;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Users [id=" + id + ", username=" + username + ", pwd=" + pwd + ", age=" + age + ", sex=" + sex + "]";
}
}
工具类DBManager
package com.qf.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBManager {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
return DriverManager
.getConnection("jdbc:mysql://localhost:3306/j1904?useUnicode=true&characterEncoding=utf-8",
"root", "root");
}
public static void closeAll(Connection connection, PreparedStatement statement) {
try {
if(statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void closeAll(Connection connection, PreparedStatement statement, ResultSet resultSet) {
try {
if(resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
closeAll(connection, statement);
}
}
}
数据库操作类UsersDAO
package com.qf.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.qf.entity.Users;
import com.qf.util.DBManager;
public class UsersDAO {
public static boolean insert(Users user) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBManager.getConnection();
String sql = "INSERT INTO users(username, pwd, age, sex) VALUES (?, ?, ?, ?);";
statement = connection.prepareStatement(sql);
statement.setString(1, user.getUsername());
statement.setString(2, user.getPwd());
statement.setInt(3, user.getAge());
statement.setString(4, user.getSex());
int count = statement.executeUpdate();
return count > 0;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBManager.closeAll(connection, statement);
}
return false;
}
public static boolean update(Users user) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBManager.getConnection();
String sql = "UPDATE users SET username = ?, pwd = ?, age = ?, sex = ? WHERE id = ?;";
statement = connection.prepareStatement(sql);
statement.setString(1, user.getUsername());
statement.setString(2, user.getPwd());
statement.setInt(3, user.getAge());
statement.setString(4, user.getSex());
statement.setInt(5, user.getId());
int count = statement.executeUpdate();
return count > 0;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBManager.closeAll(connection, statement);
}
return false;
}
public static boolean delete(int id) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBManager.getConnection();
String sql = "DELETE FROM users WHERE id = ?;";
statement = connection.prepareStatement(sql);
statement.setInt(1, id);
int count = statement.executeUpdate();
return count > 0;
} catch (Exception e) {
e.printStackTrace();
}finally {
DBManager.closeAll(connection, statement);
}
return false;
}
public static Users findById(int id) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = DBManager.getConnection();
String sql = "SELECT * FROM users WHERE id = ?;";
statement = connection.prepareStatement(sql);
statement.setInt(1, id);
resultSet = statement.executeQuery();
while(resultSet.next()) {
return new Users(resultSet.getInt("id"),
resultSet.getString("username"), resultSet.getString("pwd"), resultSet.getInt("age"), resultSet.getString("sex"));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBManager.closeAll(connection, statement, resultSet);
}
return null;
}
public static List<Users> findAll() {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
List<Users> list = new ArrayList<>();
try {
connection = DBManager.getConnection();
String sql = "SELECT * FROM users";
statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery();
while(resultSet.next()) {
Users user = new Users(resultSet.getInt("id"),
resultSet.getString("username"), resultSet.getString("pwd"),
resultSet.getInt("age"), resultSet.getString("sex"));
list.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBManager.closeAll(connection, statement, resultSet);
}
return list;
}
}
注册servlet RegServlet
package com.qf.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qf.dao.UsersDAO;
import com.qf.entity.Users;
@WebServlet("/reg")
public class RegServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
String age = request.getParameter("age");
String sex = request.getParameter("sex");
int nage = Integer.parseInt(age);
Users user = new Users(0,username, pwd, nage, sex);
if(UsersDAO.insert(user)){
response.sendRedirect("success.html");
}else {
response.sendRedirect("fail.html");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
登录servlet LoginServlet
package com.qf.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qf.dao.UsersDAO;
import com.qf.entity.Users;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
List<Users> list = UsersDAO.findAll();
boolean flag = false;
for (Users users : list) {
if (users.getUsername().equals(username) && users.getPwd().equals(pwd)) {
flag = true;
}
}
if (flag) {
response.sendRedirect("success.html");
} else {
response.sendRedirect("fail.html");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
注册页面 reg.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="reg" method="post">
<input type="text" name="username" placeholder="用户名"/><br/>
<input type="password" name="pwd" placeholder="密码"/><br/>
<input type="text" name="age" placeholder="年龄"/><br/>
<input type="text" name="sex" placeholder="性别"/><br/>
<input type="submit" value="注册"/>
</form>
</body>
</html>
登录页面 login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
<input type="text" name="username" placeholder="用户名"/><br/>
<input type="password" name="pwd" placeholder="密码"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
成功页面 success.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>恭喜您成功了</h1>
</body>
</html>
失败页面 fail.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>操作失败</h1>
</body>
</html>