【JavaWeb實現數據庫應用系統】第四次進度(好多BUG!查詢優化(模糊查詢)、密碼加密存儲、更新表格信息)

  • 本次處理,完全更新了表結構
  • 因爲表結構改變代碼相應的修改
  • 對用戶密碼進行了加密和解密處理(自寫函數封裝在工具類中)
  • 註冊加密,登陸解密(之前存儲的用戶信息清空)
  • 優化查詢,可以進行模糊查詢,不限制輸入
  • 頁面發生相應的改變
  • 這次幾乎全部都進行了修改,遇到了很多錯誤(特別是對查詢的優化,卡了好久)


代碼結構:
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

  • 表結構:

在這裏插入圖片描述

  • 其中:sysuser用來存儲用戶信息(賬號、密碼、學校、生日等等,其中密碼加密存儲):

在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述

SC表

在這裏插入圖片描述

Course表

在這裏插入圖片描述
註冊界面發生相應的改變(驗證方式相應的也改變):
在這裏插入圖片描述

  • 一般用戶界面:左側顯示當前登錄用戶信息,查詢時候不再限制輸入,都可匹配:
    在這裏插入圖片描述
    學生選課信息欄(可以輸入任何信息進行查詢):
    在這裏插入圖片描述

  • 代碼如下:(Course.java)

package entity;

public class Course {
	private String sno;
	private String name;
	private int grade;
	private String cno;
	private String course_name;
	
	public String getCourse_name() {
		return course_name;
	}

	public void setCourse_name(String course_name) {
		this.course_name = course_name;
	}

	public Course(String sno , String name ,String cname, int grade) {
		this.course_name = cname;
		this.grade = grade;
		this.sno = sno;
		this.name = name;
	}
	
	public String getSno() {
		return sno;
	}

	public void setSno(String sno) {
		this.sno = sno;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getGrade() {
		return grade;
	}

	public void setGrade(int grade) {
		this.grade = grade;
	}

	public String getCno() {
		return cno;
	}

	public void setCno(String cno) {
		this.cno = cno;
	}
}

  • User.java
package entity;

public class User {
	
	private String username;
	private String password;
	private String image_path;
	private String school_name;
	
	private String mobile;
	private String birthday;
	
	public String getImage_path() {
		return image_path;
	}

	public void setImage_path(String image_path) {
		this.image_path = image_path;
	}

	public String getSchool_name() {
		return school_name;
	}

	public void setSchool_name(String school_name) {
		this.school_name = school_name;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}


	private int is_admin;
	
	public User(String name , String pwd , int is_admin , String school , String birth , String mobile){
		this.is_admin = is_admin;
		this.username = name;
		this.password = pwd;
		this.school_name = school;
		this.birthday = birth;
		this.mobile = mobile;
	}
	
	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public int getIs_admin() {
		return is_admin;
	}


	public void setIs_admin(int is_admin) {
		this.is_admin = is_admin;
	}
}

  • Servlet類:
  • CourseResultServlet.java
package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CourseResultServlet
 */
public class CourseResultServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String path;
		String stu;
		String ss;
		if(request.getParameter("student_num").equals("")) {
			path = "course.jsp";
		}else {
			stu = (request.getParameter("student_num"));
			//轉換編碼:
			ss = new String(stu.getBytes("ISO-8859-1"),"utf-8");
			path = "search_result/course_result.jsp";
			request.setAttribute("stu", ss);
		}
		
		RequestDispatcher dispatcher = request.getRequestDispatcher(path);
		
		dispatcher.forward(request, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}

  • RegistServlet.java
package servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Connection;

import db_connect.DBConnect;
import util.Encryption;

/**
 * Servlet implementation class RegistServlet
 */
public class RegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Connection connection = (Connection) DBConnect.getConnection();
		Statement statement = null;		
		ResultSet resultSet = null;
		
		
		String username = (String)request.getParameter("username");
		
		String password1 = (String)request.getParameter("password");
		System.out.println(password1);
		String password2 = (String)request.getParameter("password2");
		
		Encryption asd = new Encryption();
		String mi;
		
		String school_1 = (String)request.getParameter("school");
		String school = new String(school_1.getBytes("ISO-8859-1"),"utf-8");
		
		String birthday = (String)request.getParameter("birthday");
		String birth = new String(birthday.getBytes("ISO-8859-1"),"utf-8");
		
		String phone = (String)request.getParameter("phone");
		
		System.out.println(school + " " + birth + " " + phone);
		
		String path = "";
		String msg = "";
		int temp = 0;//判斷註冊賬戶是否已經存在
		
		//接收客戶端瀏覽器提交上來的驗證碼
		
		String use_code = request.getParameter("check");
		
		//提取圖片的驗證碼
		String system_code =  (String) request.getSession().getAttribute("checkcode");
		
	
		
		String user = "^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
		//密碼必須同時有字母和數字
		String pwd = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
		if(username != null && password1 != null && password2 != null && use_code != null 
				&& system_code != null) {
			//轉換成小寫
			use_code = use_code.toLowerCase();
			system_code = system_code.toLowerCase();
			if(use_code.equals(system_code)) {
				if(username.matches(user)) {
					if(password1.equals(password2)) {
						if(password1.matches(pwd)) {
							mi = asd.secreat(password1);
							String sql = "insert into sysuser values(\'"+ username + "\',\'" +
									mi + "\',\'" + school + "\',\'" + phone + "\'," + "\'" + 
									birth + "\',0," + "\'\')";
							//將數據庫中的數據提取出來
							try {
								statement = connection.createStatement();
							}catch(SQLException e) {
								e.printStackTrace();
							}
							//查詢語句
							String sql2 = "select * from sysuser";
							
							try {
								//將所有信息存入結果集
								resultSet = statement.executeQuery(sql2);
							}catch(SQLException e) {
								e.printStackTrace();
							}
							
							try {
								while(resultSet.next()) {
									String user_name = (String)resultSet.getString("UserID");
									System.out.println(user_name);
									if(username.equals(user_name)) {
										temp = 1;
										break;
									}
								}
							} catch (SQLException e) {
								e.printStackTrace();
							}
							System.out.println(temp);
							if(temp == 0) {
								try {
									statement.executeUpdate(sql);
								} catch (SQLException e) {
									e.printStackTrace();
								}
								path = "login.jsp";
								msg = "註冊成功,請登錄";
							}else {
								path = "regist.jsp";
								msg = "用戶已經存在,請換一個用戶名註冊";
							}
						}else {
							path = "regist.jsp";
							msg = "密碼格式錯誤";
						}
					}else {
						path = "regist.jsp";
						msg = "兩次輸入密碼不相同";
					}
				}else {
					path = "regist.jsp";
					msg = "賬號格式錯誤";
				}
			}else {
				msg = "驗證碼輸入錯誤";
				path = "regist.jsp";
			}
			
		System.out.println(msg + path);
		request.setAttribute("username", username);
		request.setAttribute("msg", msg);
		
		RequestDispatcher dispatcher = request.getRequestDispatcher(path);
		
		dispatcher.forward(request, response);
		
	}
}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

  • 加密方法:
package util;

public class Encryption {
	String pwd = null;
	char [] c;
	public String secreat(String a) {
		
		c = a.toCharArray();
		for(int i = 0 ; i < c.length ; i++) {
			c[i] = (char)(c[i]+64);
		}
		pwd = new String(c);
		return pwd;
	}
	
	public String free(String a) {
		c = a.toCharArray();
		for(int i = 0 ; i < c.length ; i++) {
			c[i] = (char)(c[i]-64);
		}
		pwd = new String(c);
		return pwd;
	}
	
	public static void main(String [] args) {
	//用來測試
		String name = "Hello!";
		Encryption a = new Encryption();
		String mi = a.secreat(name);
		System.out.println(mi);
		String free = a.free(mi);
		System.out.println(free);
	}
}

  • jsp文件幾乎都發生了改變,先列出一個吧:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="util.Read_user" %>
<%@ page import="util.Read_student" %>
<%@ page import = "util.Read_course" %>
<%@ page import = "entity.Student" %>
<%@ page import = "entity.Course" %>
<%@ page import="entity.User" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

	<SCRIPT LANGUAGE=javascript> 
		function logout() { 
		 var msg = "您真的確定要退出嗎?"; 
		 if (confirm(msg)==true){ 
		  	return true; 
		 }else{ 
		  		return false; 
		 	} 
		} 
	</SCRIPT> 
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title></title>
		<style type="text/css">
			/* 媒體查詢 根據 用戶設備的屏幕尺寸來顯示, */
			#all { width: 80%; height: 550px; background-color: pink;
			margin: auto;overflow-y: auto;}
			#nav{height: 100px; background-color: lightblue; text-align:center;color:yellow;}
			#mid{height: 350px;}
			#mid #midleft{width: 20%; height:350px ;float: left; text-align:center;}
			#mid #midmid{width: 70%; height:350px ; background-color: lime;float: left;overflow-y: auto;overflow-x: auto;}
			#mid #midright{width: 10%; height:350px ; background-color: khaki;float: left;}
			#foot{height: 90px; background-color: yellow; text-align:center;padding-top:15px;
				padding-left:30px;align:center;}
			
			@media only screen and (max-width:1400px ) {
				#all{width: 80%;}
			}
			@media only screen and (max-width:700px ) {
				#all{width: 100%;}
			}
		
			#all ul{ margin: 0px; padding: 0px; list-style-type: none;
				height: 60px; background-color: #C71585;}
			#all li{ float: left; width: 20%;height: 60px;}
			#all ul li a{display: block;height: 45px; text-align: center; font-size: 22px;
			font-family: "courier new"; font-weight: 700; color: white; text-decoration: none;
				/* 塞泡沫 */
				padding-top:15px;
				border-right: 2px solid white;
			}
			
			
			#all ul li a#a1{background-color: blue;}
			#all ul li a#a2{background-color: forestgreen;}
			#all ul li a#a3{background-color: black;}
			#all ul li a#a4{background-color: forestgreen;}
			#all ul li a#a5{background-color: blue;}
			#all ul li a:hover {background-color: crimson;}
			#all ul li a#a1:hover {background-color: gray;}
			#all ul li a#a2:hover {background-color: gray;}
			
		
	        .search{
	        	margin-left:36%;
	            width: 30%;            
	            display: flex;
	            text-align:center;
	            float:left;
	            /*border: 1px solid red;*/
	        }
	        .search input{
	            float: left; 
	            flex: 3;
	            height: 30px;
	            outline: none;
	            border: 1px solid red;
	            box-sizing: border-box;
	            padding-left: 15px;
	        }
	        .search button{
	            float: right;
	            flex: 1;
	            height: 30px;
	            background-color: red;
	            color: white;
	            border-style: none;
	            outline: none;
	        }
	        .search button i{
	            font-style: normal;
	        }
	        .search button:hover{
	            font-size: 16px;
	        }
			
		</style>
</head>
<body>
	
	<div id="all">
			<div id="nav">
				<ul>
					<li><a href="user_main.jsp" id="a1" >學生列表</a></li>
					<li><a href="" id="a2">選修課程</a></li>
					<li><a href="" id = "a3">課程成績</a></li>
					<li><a href="" id = "a4">綜合排名</a></li>
					<li><a href="login.jsp" id = "a5" onclick="javascript:return logout()">退出登錄</a></li>
				</ul>
				課程信息如下
				
			</div>
			
			
			<div id="mid">
				<div id="midleft">
					<br>
					當前用戶:
					<br><br>
					<table border = '1' cellspacing="1">
						
						<tr>
							<%	
								String username = (String)session.getAttribute("username");
								System.out.println(username);
								Read_user readuser = new Read_user();
								List<?> list = readuser.read_user(username);
								for(Object u1:list){
									User u = (User)u1;
								%>
							
							<td width="200px">姓名</td>
							<td width="200px"><%=u.getUsername() %></td>
						</tr>
						<tr>
							<td>學校</td>
							<td><%=u.getSchool_name() %></td>
						</tr>
						<tr>
							<td>生日</td>
							<td><%=u.getBirthday() %>
						</tr>
						<tr>
							<td>電話</td>
							<td><%=u.getMobile() %></td>
						</tr>
						<tr>
							<td>身份</td>
							<td>
								<%
									if(u.getIs_admin() == 1){
										out.print("管 理 員");
									}else{
										out.print("普通用戶");
									}
								%>
							</td>
						</tr>
						<tr>
							<td>頭像</td>
							<td>null</td>
						</tr>
							<%
								}
							%>
					
					</table>
				
				</div>
				<div id="midmid">
					<table border = '1' style="margin:auto ;text-align:center" >
					<tr>
						<td width="200px">學號</td>
						<td width="200px">姓名</td>
						<td width="200px">課程</td>
						<td width="200px">分數</td>
					</tr>
					<%
						Read_course readcourse = new Read_course();
						List<?> list2 = readcourse.read_course();
						for(Object u1:list2){
							Course u = (Course)u1;
					%>
						<tr>
							<td> <%=u.getSno() %> </td>
							<td> <%=u.getName() %> </td>
							<td> <%=u.getCourse_name()%> </td>
							<td> <%=u.getGrade() %> </td>
						</tr>
					<%
						}
					%>
					
				</table>
				</div>
				<div id="midright"></div>
			</div>
			<div id="foot">
				<!--  	<li><a href=" " id="a1" >增加信息</a></li>
					<li><a href=" " id="a2">刪除信息</a></li>
					<li><a href=" ">修改信息</a></li>
				-->
				 <form action="CourseResultServlet">
				 	<div class="search" >
						
						<input type="text" id = "v" placeholder="學號、姓名、課程名(模糊查詢)" name="student_num" />
			            <button><i>搜索</i></button>
					
		        	</div>
				 </form>
					
				
			</div>
		</div>

</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章