Jsp访问数据库1
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="check1.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 21 package com.ding; 22 import java.sql.*; 23 /** 24 * <p>Title: LoginDao</p> 25 * <p>Description: </p> 26 * @author: 丁帅帅 27 * @date: 2021-4-5 12:15:21 28 * @version v1.0 29 */ 30 public class LoginDao { 31 32 public LoginDao() { 33 34 } 35 36 public int login(String name,String pwd) { //1:登录成功 0:登录失败(用户名或密码有误) -1:系统异常 37 String URL = "jdbc:oracle:thin:@localhost:1521:ORCL"; 38 String USERNAME = "scott"; 39 String PWD = "tiger"; 40 Connection con = null; 41 PreparedStatement pstmt = null; 42 ResultSet rs = null ; 43 44 try { 45 Class.forName("oracle.jdbc.OracleDriver"); 46 con = DriverManager.getConnection(URL, USERNAME, PWD); 47 String sql = "select count(*) from login where uname='"+name+"' and upwd ='"+pwd+"'" ; 48 pstmt = con.prepareStatement(sql); 49 rs = pstmt.executeQuery(); 50 // d.处理结果 51 int count = -1; 52 if(rs.next()) { 53 count = rs.getInt(1) ; 54 } 55 return count ; 56 } catch (ClassNotFoundException e) { 57 e.printStackTrace(); 58 return -1 ; 59 } catch (SQLException e) { 60 e.printStackTrace(); 61 return -1 ; 62 } catch(Exception e) { 63 e.printStackTrace(); 64 return -1 ; 65 } 66 finally { 67 try { 68 if(rs!=null) rs.close(); 69 if(pstmt!=null) pstmt.close();// 对象.方法 70 if(con!=null)con.close(); 71 }catch(SQLException e) { 72 e.printStackTrace(); 73 } 74 } 75 } 76 } 77 78 79 80 81 <%@ page language="java" contentType="text/html; charset=UTF-8" 82 pageEncoding="UTF-8"%> 83 <%@ page import="java.sql.*" %> 84 <%@ page import="com.ding.LoginDao" %> 85 <!DOCTYPE html> 86 <html> 87 <head> 88 <meta charset="UTF-8"> 89 <title>Insert title here</title> 90 </head> 91 <body> 92 <% 93 String name = request.getParameter("uname") ; 94 String pwd = request.getParameter("upwd") ; 95 LoginDao dao = new LoginDao(); 96 97 int result = dao.login(name,pwd) ; 98 if(result >0){ 99 out.print("登录成功!"); 100 }else if(result==0){ 101 out.print("用户名或密码有误!!"); 102 }else{ 103 out.print("系统异常!!"); 104 } 105 106 %> 107 </body> 108 </html>
道阻且长,行则将至

浙公网安备 33010602011771号