要實現此功能,須要電腦安裝JAVA EE、SQL Server 2008和Tomcat等軟件,並進行配置環境成功。html
對這門課的但願和本身的目標:java
但願:能夠徹底掌握老師所講的內容。sql
目標:可以完整的作出一個網站。數據庫
計劃每週花多少時間在這門課上:jsp
每週的天天均儘可能花1個小時的時間在敲代碼上。sqlserver
接下來~post
1、首先須要建立數據庫,並在數據庫中建立數據表,列表以下:網站
建成以後的數據表如圖所示:ui
2、而後,打開JAVA EE,開始建立WEB項目。url
3、建立jsp文件,以下:
login.jsp
<%@ page import="java.sql.*" 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>登陸界面</title> </head> <body> <center> <h1 style="color:red">登陸</h1> <form id="indexform" name="indexForm" action="logincheck.jsp" method="post"> <table border="0"> <tr> <td>帳號:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="password"> </td> </tr> </table> <br> <input type="submit" value="登陸" style="color:#BC8F8F"> </form> </center> </body> </html>
logincheck.jsp
<%@ page import="java.sql.*" 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> <jsp:useBean id="db" class="Bean.DBBean" scope="page"/> <% request.setCharacterEncoding("UTF-8"); String username=(String)request.getParameter("username"); String password=(String)request.getParameter("password");//取出login.jsp的值 //下面是數據庫操做 *表明全部值 String sql="select * from lhT where username="+"'"+username+"'";//定義一個查詢語句 ResultSet rs=db.executeQuery(sql);//運行上面的語句 if(rs.next()) { /* if(password.equals(rs.getString(2))) { } */ if(password.equals(rs.getObject("password"))){ response.sendRedirect("loginsuccess.jsp"); } else{ out.print("<script language='javaScript'> alert('密碼錯誤');</script>"); response.setHeader("refresh", "0;url=login.jsp"); } } else { out.print("<script language='javaScript'> alert('請輸入用戶名——else');</script>"); response.setHeader("refresh", "0;url=login.jsp"); } %> </body> </html>
loginsuccess.jsp
<%@ page import="java.sql.*" 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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>登陸成功 </h1> </body> </html>
4、而後是其餘文件的建立:
建立DBBean.java,鏈接數據庫
package Bean; import java.sql.*; public class DBBean { private String driverStr = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; private String connStr = "jdbc:sqlserver://localhost:1433; DatabaseName=lhsjk"; private String dbusername = "ABC"; private String dbpassword = "123"; private Connection conn = null; private Statement stmt = null; public DBBean() { try { Class.forName(driverStr); conn = DriverManager.getConnection(connStr, dbusername, dbpassword); stmt = conn.createStatement(); } catch (Exception ex) { System.out.println(ex.getMessage()); System.out.println("數據鏈接失敗!"); } } public int executeUpdate(String s) { int result = 0; System.out.println("--更新語句:"+s+"\n"); try { result = stmt.executeUpdate(s); } catch (Exception ex) { System.out.println("執行更新錯誤!"); } return result; } public ResultSet executeQuery(String s) { ResultSet rs = null; System.out.print("--查詢語句:"+s+"\n"); try { rs = stmt.executeQuery(s); } catch (Exception ex) { System.out.println("ִ執行查詢錯誤!"); } return rs; } public void execQuery(String s){ try { stmt.executeUpdate(s); } catch (SQLException e) { // TODO Auto-generated catch block System.out.println("執行插入錯誤!"); } } public void close() { try { stmt.close(); conn.close(); } catch (Exception e) { } } }
而後是其餘的,列表以下:
5、運行。
主頁面:
登陸成功時:
登陸失敗時(包括用戶名錯誤,密碼錯誤)