JavaWeb —— JDBC 用户登录验证

一、目录结构

二、登录界面

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="utf-8">
 7 <title>登录界面</title>
 8 </head>
 9 <body>
10     <form action="login" method="post">
11         用户名:<input type="text" name="name"><br>
12         密&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="text" name="pwd"><br>
13         <input type="submit" value="登录" >
14         <input type="reset">
15     </form>
16 </body>
17 </html>

三、实体类

 1 package com.chauncey.po;
 2 
 3 import java.io.Serializable;
 4 
 5 @SuppressWarnings("serial")
 6 public class User implements Serializable {
 7     private Integer id;
 8     private String name;
 9     private String psw;
10     public User() {
11         
12     }
13     public Integer getId() {
14         return id;
15     }
16     public void setId(Integer id) {
17         this.id = id;
18     }
19     public String getName() {
20         return name;
21     }
22     public void setName(String name) {
23         this.name = name;
24     }
25     public String getPwd() {
26         return psw;
27     }
28     public void setPwd(String pwd) {
29         this.psw = pwd;
30     }
31 }

四、JDBC工具类

 1 package com.chauncey.utils;
 2 /*
 3  * 数据库连接
 4  *
 5  */
 6 import java.sql.Connection;
 7 import java.sql.DriverManager;
 8 import java.sql.ResultSet;
 9 import java.sql.SQLException;
10 import java.sql.Statement;
11 
12 public class JDBCUtils {
13     //加载驱动,并建立数据库连接
14     public static Connection getConnection() throws SQLException, ClassNotFoundException {
15         //加载驱动
16         Class.forName("com.mysql.cj.jdbc.Driver");
17         //jdbc:mysql://localhost:3306/user?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8&autoReconnect=true
18         String url = "jdbc:mysql://localhost:3306/info?useSSL=false&serverTimezone=UTC";
19         String user = "root";
20         String password = "*****";//数据库的登录密码
21         Connection con = DriverManager.getConnection(url, user, password);
22         return con;
23     }
24     //关闭数据库,释放连接
25     public static void release(Statement st,Connection con) {
26         if (st != null) {
27             try {
28                 st.close();
29             } catch (SQLException e) {
30                 // TODO Auto-generated catch block
31                 e.printStackTrace();
32             }
33             st = null;
34         }
35         if (con != null) {
36             try {
37                 con.close();
38             } catch (SQLException e) {
39                 // TODO Auto-generated catch block
40                 e.printStackTrace();
41             }
42             con = null;
43         }
44     }
45     //释放资源时先执行该方法
46     public static void release(ResultSet rs,Statement st,Connection con) {
47         if (rs !=null) {
48             try {
49                 rs.close();
50             } catch (SQLException e) {
51                 // TODO Auto-generated catch block
52                 e.printStackTrace();
53             }
54             rs = null;
55         }
56         release(st, con);
57     }
58 }

五、数据访问UserDao

 1 package com.chauncey.dao;
 2 /**
 3  * 查询数据库验证登录
 4  *
 5  */
 6 import java.sql.Connection;
 7 import java.sql.ResultSet;
 8 import java.sql.Statement;
 9 
10 import com.chauncey.utils.JDBCUtils;
11 
12 public class UserDao {
13     Connection con = null;
14     Statement st = null;
15     ResultSet rs = null;
16     
17     public boolean find(String name,String pwd) {
18         //获取数据库连接
19         try {
20             con = JDBCUtils.getConnection();
21             st = con.createStatement();
22             
23             String sql = "select * from user where name = '"+name+"' and pwd = '"+pwd+"'";
24             rs = st.executeQuery(sql);
25             if(rs.next()) {
26                 return true;
27             }else {
28                 return false;
29             }
30         } catch (Exception e) {
31             e.printStackTrace();
32         }finally {
33             JDBCUtils.release(rs, st, con);
34         }
35         return false;
36     }
37     
38 }

六、控制层LoginServlet

 1 @WebServlet("/login")
 2 public class LoginServlet extends HttpServlet {
 3     private static final long serialVersionUID = 1L;
 4 
 5     public LoginServlet() {
 6 
 7     }
 8 
 9     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
10         response.setContentType("text/html;charset=utf-8");
11         request.setCharacterEncoding("utf-8");
12         PrintWriter out = response.getWriter();
13         UserDao userDao = new UserDao();
14         String name = null;
15         String sname = request.getParameter("name");
16         if(sname!=null && sname.trim().length()>0) {
17             name = sname;
18         }
19         String pwd = null;
20         String spwd = request.getParameter("pwd");
21         if(spwd!=null && spwd.trim().length()>0) {
22             pwd = spwd;
23         }
24         boolean tf = userDao.find(name, pwd);
25         if (tf) {
26             out.print("登录成功");
27         }else {
28             out.print("用户名或密码错误");
29         }
30         
31     }
32 
33     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34         doGet(request, response);
35     }
36 
37 }

七、数据库创建

 1 CREATE DATABASE info;
 2 USE info;
 3 
 4 CREATE TABLE USER(
 5 id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
 6 NAME VARCHAR(20),
 7 pwd VARCHAR(30)
 8 )
 9 INSERT INTO USER VALUES(NULL,'小明','xiaoming');
10 INSERT INTO USER VALUES(NULL,'小王','123456');

效果图:

 

posted @ 2021-06-23 20:32  笺笙  阅读(145)  评论(0编辑  收藏  举报