Login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="check.jsp" method="post"><!--action里面写要转的jsp页面 --> 卡号:<input type="text" name="cardid"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="登陆"><br> </form> </body> </html>
check.jsp ojdbc6.jar 放lib里
<%@page import="java.sql.ResultSet"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.Connection"%> <%@page import="java.sql.PreparedStatement"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <!--这个页面是 接收用户输入的卡号和密码,进行验证,验证需要连数据库 request对象负责接收--> <% //接收数据 String cardid=request.getParameter("cardid"); String password=request.getParameter("password"); //前两部接收数据,接受完了,就该验证数据 //一定要确定数据收到了 //数据验证 检查用户名 密码 if(cardid==null||password==null ||cardid.equals("")||password.equals(""))//得保证都有数据??? { out.write("请正确登陆系统"); } else { out.write(cardid); out.write(password); } //连接数据库 Class.forName("oracle.jdbc.driver.OracleDriver"); Connection conn=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:orcl", "test0816","123456"); PreparedStatement ps=conn.prepareStatement( "select * from t_bankcard where cardid=?" +"and password=? and state='1'"); ps.setString(1,cardid); ps.setString(2,password); ResultSet rs=ps.executeQuery(); if(rs.next()) { out.write("用户名和密码正确"); } else { out.write("用户名或密码错误"); } //释放资源 rs.close(); ps.close(); conn.close(); %> </body> </html>
Login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="check2.jsp" method="post"><!--action里面写要转的jsp页面 --> 卡号:<input type="text" name="cardid"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="登陆"><br> </form> </body> </html>
CardDAO.java 建在java Resources src 包下面
package com.hanqi.web; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.mchange.v2.c3p0.ComboPooledDataSource; public class CardDAO { private ComboPooledDataSource cpds=new ComboPooledDataSource("helloc3p0"); //通过配置文件构建连接池对象 public boolean checkLogin(String cardid,String password) { boolean rtn=false; try { Connection conn=cpds.getConnection(); PreparedStatement ps=conn.prepareStatement( "select * from t_bankcard where cardid=?" +"and password=? and state='1'"); ps.setString(1,cardid); ps.setString(2,password); ResultSet rs=ps.executeQuery(); rtn=rs.next();// } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return rtn; } }
check2.jsp webContent
<%@page import="com.hanqi.web.CardDAO"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String cardid=request.getParameter("cardid"); String password=request.getParameter("password"); if(cardid==null||password==null ||cardid.equals("")||password.equals("")) { out.write("请正确登陆"); } else { //检查登陆信息 CardDAO cd=new CardDAO(); if(cd.checkLogin(cardid,password)) { out.write("登陆成功"); } else { out.write("登陆失败"); } } %> </body> </html>