Jsp访问数据库2

  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="check2.jsp" method="post">
 11         用户名:<input type="text" name="uname"/><br>
 12        密码:<input type="password" name="upwd"/><br>
 13        <input type="submit" value="登录"/><br>    
 14        </form>
 15 </body>
 16 </html>
 17 
 18 
 19 
 20 package com.ding.entity;
 21 
 22 /**
 23  * 
 24  * <p>Title: Login</p>  
 25  * <p>Description: </p>  
 26  * @author: 丁帅帅
 27  * @date: 2021-4-5 13:12:23
 28  * @version v1.0
 29  */
 30 public class Login {
 31     private int id ; 
 32     private String name;
 33     private String pwd ;
 34     
 35     public Login() {
 36     }
 37     public Login(int id, String name, String pwd) {
 38         this.id = id;
 39         this.name = name;
 40         this.pwd = pwd;
 41     }
 42     public Login( String name, String pwd) {
 43         this.name = name;
 44         this.pwd = pwd;
 45     }
 46     public int getId() {
 47         return id;
 48     }
 49     public void setId(int id) {
 50         this.id = id;
 51     }
 52     public String getName() {
 53         return name;
 54     }
 55     public void setName(String name) {
 56         this.name = name;
 57     }
 58     public String getPwd() {
 59         return pwd;
 60     }
 61     public void setPwd(String pwd) {
 62         this.pwd = pwd;
 63     } 
 64     
 65 }
 66 
 67 
 68 
 69 package com.ding.entity;
 70 import java.sql.*;
 71 /**
 72  * 
 73  * <p>Title: LoginDao</p>  
 74  * <p>Description: </p>  
 75  * @author: 丁帅帅
 76  * @date: 2021-4-5 13:05:50
 77  * @version v1.0
 78  */
 79 public class LoginDao {
 80         
 81         public LoginDao() {
 82         
 83     }
 84         public int login(Login login) { //1:登录成功   0:登录失败(用户名或密码有误)  -1:系统异常
 85             String URL = "jdbc:oracle:thin:@localhost:1521:ORCL";
 86             String USERNAME = "scott";
 87             String PWD = "tiger";
 88             Connection con = null;
 89             PreparedStatement pstmt = null;
 90             ResultSet rs = null ; 
 91             
 92             try {
 93                 Class.forName("oracle.jdbc.OracleDriver");
 94                 con = DriverManager.getConnection(URL, USERNAME, PWD);
 95                 String sql = "select count(*) from login where uname='"+login.getName()+"' and upwd ='"+login.getPwd()+"'" ;
 96                 pstmt = con.prepareStatement(sql);
 97                 rs = pstmt.executeQuery(); 
 98                 // d.处理结果
 99                 int count = -1;
100                 if(rs.next()) {
101                     count = rs.getInt(1) ;
102                 }
103                 return count ;
104             } catch (ClassNotFoundException e) {
105                 e.printStackTrace();
106                 return -1 ;
107             } catch (SQLException e) {
108                 e.printStackTrace();
109                 return -1 ;
110             } catch(Exception e) {
111                 e.printStackTrace();
112                 return -1 ;
113             }
114             finally {
115                 try {
116                     if(rs!=null) rs.close(); 
117                      if(pstmt!=null) pstmt.close();// 对象.方法
118                      if(con!=null)con.close();
119                 }catch(SQLException e) {
120                     e.printStackTrace();
121                 }
122             }
123         }
124 }
125 
126 
127 
128 
129 <%@ page language="java" contentType="text/html; charset=UTF-8"
130     pageEncoding="UTF-8"%>
131 <%@ page import="com.ding.entity.*" %>
132 <!DOCTYPE html>
133 <html>
134 <head>
135 <meta charset="UTF-8">
136 <title>Insert title here</title>
137 </head>
138 <body>
139 <%
140         
141             String name = request.getParameter("uname") ;
142             String pwd = request.getParameter("upwd") ;
143             Login login = new Login(name,pwd) ;
144             
145             
146             LoginDao dao = new LoginDao();
147             
148             int result = dao.login(login) ;
149             if(result >0){
150                 out.print("登录成功!");
151             }else if(result==0){
152                 out.print("用户名或密码有误!!");
153             }else{
154                 out.print("系统异常!!");
155             }
156         
157         %>
158 </body>
159 </html>

 

posted @ 2021-04-05 13:13  丁帅帅dss  阅读(59)  评论(0)    收藏  举报