Login.jsphtml
<%@ 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裏java
<%@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.jspweb
<%@ 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 包下面sql
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>