JSP学习-JSP访问数据库-JavaBean封装

通过index.jsp输入用户名、密码登录,跳转到check.jsp验证用户名、密码;

在Check.jsp中,访问数据库  login  ,查看输入的用户名、密码是否在数据库中匹配。

注意:这里使用的是Oracle数据库,要将Oracle的驱动放入项目指定目录,如图:

 

 

 

 

Index.jsp配置如下:

 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>Insert title here</title>
 8 </head>
 9 <body>
10     <form action="check.jsp">
11         用户名:<input type="text" name="uname" ><br>
12         密码:<input type="password" name="upwd" ><br>
13         <input type="submit" value="登录" ><br>
14     
15     </form>
16 </body>
17 </html>

Check.jsp配置如下:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!-- 注意JSP中import的写法 -->
 4 <%@ page import="java.sql.*" import="java.io.IOException"%>
 5 <!DOCTYPE html>
 6 <html>
 7 <head>
 8 <meta charset="UTF-8">
 9 <title>Insert title here</title>
10 </head>
11 <body>
12     <%
13         String URL = "jdbc:oracle:thin:@localhost:1521:ORCL";
14         String USERNAME = "scott";
15         String PWD = "tiger";
16         try {
17             Class.forName("oracle.jdbc.OracleDriver");
18         } catch (ClassNotFoundException e) {
19             // TODO Auto-generated catch block
20             e.printStackTrace();
21         }
22         // 与数据库建立连接
23         try (Connection connection = DriverManager.getConnection(URL, USERNAME, PWD);) {
24             String sql = "select count(*) from login where name=? and pwd=?";
25             PreparedStatement pstmt = connection.prepareStatement(sql); //预编译
26 
27             String name = request.getParameter("uname");
28             String pwd = request.getParameter("upwd");
29 
30             pstmt.setString(1, name);
31             pstmt.setString(2, pwd);
32             ResultSet rs = pstmt.executeQuery();
33 
34             int count = -1;
35             if (rs.next()) {
36                 count = rs.getInt(1);
37             }
38             if (count > 0) {
39                 out.println("登陆成功!");
40             } else {
41                 out.println("登陆失败");
42             }
43 
44         } catch (SQLException e) {
45             // TODO Auto-generated catch block
46             e.printStackTrace();
47         }
48     %>
49 </body>
50 </html>

 

 

 改进版本:利用JavaBean封装用户数据和登录逻辑

 

 

 

 Login.java:

 1 package org.myDAO;
 2 //Login类  封装了一条用户记录:包括用户名、密码【这里的id暂时用不到】
 3 public class Login {
 4     public int getId() {
 5         return id;
 6     }
 7     public void setId(int id) {
 8         this.id = id;
 9     }
10     public String getName() {
11         return name;
12     }
13     public Login() {
14     }
15     public Login( String name, String pwd) {
16         super();
17         this.name = name;
18         this.pwd = pwd;
19     }
20     public void setName(String name) {
21         this.name = name;
22     }
23     public String getPwd() {
24         return pwd;
25     }
26     public void setPwd(String pwd) {
27         this.pwd = pwd;
28     }
29     private int id;
30     private String name;
31     private String pwd;
32 }

loginDAO.java:

 1 package org.myDAO;
 2 import java.sql.*;
 3 //loginDAO封装了   登录逻辑
 4 public class loginDAO {
 5     public int login(Login login) {
 6         String URL = "jdbc:oracle:thin:@localhost:1521:ORCL";
 7         String USERNAME = "scott";
 8         String PWD = "tiger";
 9         try {
10             Class.forName("oracle.jdbc.OracleDriver");
11         } catch (ClassNotFoundException e) {
12             // TODO Auto-generated catch block
13             e.printStackTrace();
14             return -1;
15         }
16         // 与数据库建立连接
17         try (Connection connection = DriverManager.getConnection(URL, USERNAME, PWD);) {
18             String sql = "select count(*) from login where name=? and pwd=?";
19             PreparedStatement pstmt = connection.prepareStatement(sql); // 预编译
20 
21 //            这两句本质就是拿到用户名和密码,用来进行数据库操作,不妨通过传参得到
22 //            String name = request.getParameter("uname");
23 //            String pwd = request.getParameter("upwd");
24             
25 //            封装用户数据的login
26             pstmt.setString(1, login.getName());
27             pstmt.setString(2, login.getPwd());
28             ResultSet rs = pstmt.executeQuery();
29 
30             int count = -1;
31             if (rs.next()) {
32                 count = rs.getInt(1);
33             }
34 //            不能直接打印,不妨通过返回count,若count=1,check页面显示登陆成功,若为0,登陆失败,若为-1,出现异常
35 //            if (count > 0) {
36 //                out.println("登陆成功");
37 //            } else {
38 //                out.println("登陆失败");
39 //            }
40             return count;
41         } catch (SQLException e) {
42             // TODO Auto-generated catch block
43             e.printStackTrace();
44             return -1;
45         }
46     }
47 }

check.jsp:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8" %>
 3 <!-- 注意JSP中import的写法 -->
 4 <%@page
 5 import="org.myDAO.loginDAO"
 6 import="org.myDAO.Login"
 7  %>
 8 <!DOCTYPE html>
 9 <html>
10 <head>
11 <meta charset="UTF-8">
12 <title>Insert title here</title>
13 </head>
14 <body>
15     <%
16         String name=request.getParameter("uname");
17         String pwd=request.getParameter("upwd");
18         loginDAO dao=new loginDAO();
19         Login login=new Login(name,pwd);
20         int result=dao.login(login);
21         if(result>0){
22             out.print("登陆成功!");
23         }else if(result==0){
24             out.print("用户名或密码错误!");
25         }else{
26             out.print("系统出现异常!");
27         }
28         
29     %>
30 </body>
31 </html>

index.jsp:

 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>Insert title here</title>
 8 </head>
 9 <body>
10     <form action="check.jsp" method="post">
11         用户名:<input type="text" name="uname" ><br>
12         密码:<input type="password" name="upwd" ><br>
13         <input type="submit" value="登录" ><br>
14     
15     </form>
16 </body>
17 </html>

 

 

 

 

 

 

 

 

 

 
 
 
 
posted @ 2020-02-15 19:14  细雨轻风  阅读(701)  评论(0编辑  收藏  举报