Jsp练习——连接数据库模拟登录
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 <style type="text/css"> 9 *{ 10 margin:0px; 11 padding:0px; 12 } 13 .yi{ 14 width:100%; 15 height:500px; 16 background:url(background.jpg); 17 top:50px; 18 position:relative; 19 } 20 .er{ 21 width:300px; 22 height:400px; 23 background:#CCCCCC; 24 border:3px solid #FFFFFF; 25 left:800px; 26 top:50px; 27 position:relative; 28 font-size:24px; 29 font-weight:bold; 30 color:#FF3300; 31 32 } 33 </style> 34 </head> 35 36 <body> 37 <div class="yi" > 38 <div class="er"> 39 <form action="index1.jsp" method="post"> 40 <input type="text" name="name" value="请输入账号" style="width:200px; height:30px; 41 margin-top:80px; margin-left:50px;" ><br> 42 <input type="password" name="password" style="width:200px; height:30px; 43 margin-top:30px; margin-left:50px;" ><br> 44 <input type="submit" value="登录" style="width:200px; height:30px; 45 margin-top:30px; margin-left:50px;" > 46 </form> 47 </div> 48 </div> 49 50 </body> 51 </html>
1 <%@page import="com.mchange.v2.c3p0.ComboPooledDataSource"%> 2 <%@page import="java.sql.ResultSet"%> 3 <%@page import="java.sql.PreparedStatement"%> 4 <%@page import="java.sql.Statement"%> 5 <%@page import="java.sql.DriverManager"%> 6 <%@page import="java.sql.Connection"%> 7 <%@ page language="java" contentType="text/html; charset=UTF-8" 8 pageEncoding="UTF-8"%> 9 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 10 <html> 11 <head> 12 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 13 <title>Insert title here</title> 14 </head> 15 <body> 16 <% 17 String name = request.getParameter("name"); 18 String password = request.getParameter("password"); 19 try{ 20 //ComboPooledDataSource cp = new ComboPooledDataSource("helloc3p0"); 21 //Connection conn=cp.getConnection(); 22 Class.forName("oracle.jdbc.driver.OracleDriver"); 23 Connection conn = DriverManager.getConnection( 24 "jdbc:oracle:thin:@localhost:1521:orcl ", 25 "test01", "131281huazioo"); 26 String sql = "select * from bankcard where card=?"; 27 PreparedStatement ps = conn.prepareStatement(sql); 28 ps.setString(1, name); 29 ResultSet rs = ps.executeQuery(); 30 String nameRe=null; 31 String passwordRe=null; 32 if(rs.next()){ 33 nameRe=rs.getString("card"); 34 passwordRe=rs.getString("password"); 35 } 36 if(name.equals(nameRe)){ 37 if(password.equals(passwordRe)){ 38 out.write("登录成功!"); 39 }else{ 40 out.write("密码输入错误!"+"<br>"); 41 out.write("<a href='Login.jsp'>"+"返回上一页"+"</a>"); 42 } 43 }else{ 44 out.write("卡号输入错误!"+"<br>"); 45 out.write("<a href='Login.jsp'>"+"返回上一页"+"</a>"); 46 } 47 rs.close(); 48 ps.close(); 49 conn.close(); 50 }catch(Exception e){ 51 e.printStackTrace(); 52 } 53 %> 54 </body> 55 </html>