关闭

J2EE实验5:JDBC应用

标签: j2eejdbcjspmysqlsql
793人阅读 评论(3) 收藏 举报

实验5:JDBC应用

实验要求:

  1. 应具有注册、登录功能,并可以对当前用户好友信息进行全部或模糊查询(添加新记录,删除或修改原有记录功能留到下一实验完成)。
  2. 注册页面为RegisterForm.jsp,登录页面为loginForm.jsp,登录之前不能访问与注册、登录无关的页面。登录和注册功能应由register.jsp和login.jsp实现。

    注册页面的效果如下图所示:

    登陆页面的效果如下图所示:

     

  3. 编写主页index.jsp,内容包括查询所有好友信息和按姓名模糊查询好友信息两个链接。登录之前访问index.jsp应自动转到登录页面。

  4. 编写实现“查询所有好友信息”功能的JSP页面allfriend.jsp,用来显示index.jsp页面第一个链接所需的登录用户好友信息查询功能。

  5. 编写实现“按姓名模糊查询”功能的页面querybyname.jsp。模糊查询功能只需实现按姓名查询,如输入“李”,并利用allfriend.jsp查询所有姓名中带有“李”字的好友信息。

  6.  数据库访问功能应由独立的Java类实现。



数据库使用方法参照

http://blog.csdn.net/kydkong/article/details/49964363

DataBaseOperate.java

package com.myfriend;
import java.sql.*;
import java.util.*;

public class DataBaseOperate {
	static public void main(String[] args){
		
		
	}
	
	/**
	 * 
	 * @param UserName
	 * @param PassWord
	 * @return 1:注册成功 0:用户名已经存在
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	static public int RegisterUser(String UserName,String PassWord) throws SQLException, ClassNotFoundException{
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库
		Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
		Statement DataStatement = DataConnection.createStatement();
		//查询用户名以否已经存在
		ResultSet UserIsExistResult = DataStatement.executeQuery("select name from user where name = '" + UserName + "'");
		if (UserIsExistResult.next()){
			//存在
			return 0;
		}
		else{
			//不存在,添加
			//获取最大id
			ResultSet MaxIdResult = DataStatement.executeQuery("select max(userid) from user");
			int MaxId;
			if (MaxIdResult.next()){
				MaxId = MaxIdResult.getInt("max(userid)");
			}
			else{
				MaxId = 0;
			}
			MaxId ++;
			DataStatement.executeUpdate("INSERT INTO user VALUES ('" + MaxId + "', '" + UserName + "', '" + PassWord + "')");
			return 1;
		}
	}
	
	/**
	 * 
	 * @param UserName
	 * @param PassWord
	 * @return 0:登录成功 1:用户名不存在 2:密码不正确
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 */
	static public int LoginUser(String UserName,String PassWord) throws ClassNotFoundException, SQLException{
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库
		Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
		Statement DataStatement = DataConnection.createStatement();
		//查询用户名以否已经存在
		ResultSet UserIsExistResult = DataStatement.executeQuery("select * from user where name = '" + UserName + "'");
		if (UserIsExistResult.next()){
			String DBPassWord = UserIsExistResult.getString("password");
			if (DBPassWord.equals(PassWord)){
				//登录成功
				return 0;
			}
			else{
				return 2;
			}
		}
		else{
			return 1;
		}
	}
	
	static public String[] GetFriend(String FriendId) throws ClassNotFoundException, SQLException{
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库
		Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
		Statement DataStatement = DataConnection.createStatement();
		//查询用户名关联的朋友
		ResultSet MyFriendsResult = DataStatement.executeQuery(
				"select * from myfriend where id = '" + FriendId + "'");
		if (MyFriendsResult.next()){
			String[] RowStringArray = new String[9];
			RowStringArray[0] = String.valueOf(MyFriendsResult.getInt("id"));
			RowStringArray[1] = String.valueOf(MyFriendsResult.getInt("userid"));
			RowStringArray[2] = String.valueOf(MyFriendsResult.getString("name"));
			RowStringArray[3] = String.valueOf(MyFriendsResult.getString("sex"));
			RowStringArray[4] = String.valueOf(MyFriendsResult.getInt("age"));
			RowStringArray[5] = String.valueOf(MyFriendsResult.getString("qq"));
			RowStringArray[6] = String.valueOf(MyFriendsResult.getString("telephone"));
			RowStringArray[7] = String.valueOf(MyFriendsResult.getString("email"));
			RowStringArray[8] = String.valueOf(MyFriendsResult.getString("address"));
			return RowStringArray;
		}
		return null;
	}
	
	/**
	 * 
	 * @param UserName
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	static public String[][] GetAllFriends(String UserName,String SubName) throws ClassNotFoundException, SQLException{
		//System.out.println(UserName + SubName);
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库
		Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
		Statement DataStatement = DataConnection.createStatement();
		//查询用户名关联的朋友
		ResultSet MyFriendsResult = DataStatement.executeQuery(
				"select * from myfriend where userid in (select userid from user where name = '" + UserName + "')");
		if (MyFriendsResult.next()){
			MyFriendsResult.beforeFirst();
			ArrayList<String[]> ResultArray = new ArrayList<String[]>();
			while (MyFriendsResult.next()){
				String[] RowStringArray = null;
				String TempName = String.valueOf(MyFriendsResult.getString("name"));
				if (SubName == "" || TempName.matches("^.*" + SubName + ".*$")){
					RowStringArray = new String[9];
					RowStringArray[0] = String.valueOf(MyFriendsResult.getInt("id"));
					RowStringArray[1] = String.valueOf(MyFriendsResult.getInt("userid"));
					RowStringArray[2] = String.valueOf(MyFriendsResult.getString("name"));
					RowStringArray[3] = String.valueOf(MyFriendsResult.getString("sex"));
					RowStringArray[4] = String.valueOf(MyFriendsResult.getInt("age"));
					RowStringArray[5] = String.valueOf(MyFriendsResult.getString("qq"));
					RowStringArray[6] = String.valueOf(MyFriendsResult.getString("telephone"));
					RowStringArray[7] = String.valueOf(MyFriendsResult.getString("email"));
					RowStringArray[8] = String.valueOf(MyFriendsResult.getString("address"));
					ResultArray.add(RowStringArray);
				}
			}
			String[][] Result = new String[ResultArray.size()][9];
			for (int i = 0;i < ResultArray.size();i ++){
				Result[i] = ResultArray.get(i);
			}
			return Result;
		}
		else{
			return null;
		}
	}
	
	static public int Insert(String UserName,String NewName,String NewSex,String NewAge,String NewQQ,String NewTel,String NewMail,String NewAddr) throws ClassNotFoundException, SQLException{
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库
		Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
		Statement DataStatement = DataConnection.createStatement();
		//查询userid
		ResultSet UserIdResult = DataStatement.executeQuery(
				"select userid from user where name = '" + UserName + "'");
		String userid;
		if (UserIdResult.next()){
			userid = UserIdResult.getString("userid");
		}
		else{
			return -1;
		}
		//获取最大值
		ResultSet MaxIdResult = DataStatement.executeQuery("select max(id) from myfriend");
		int MaxId;
		if (MaxIdResult.next()){
			MaxId = MaxIdResult.getInt("max(id)");
		}
		else{
			MaxId = 0;
		}
		MaxId ++;
		System.out.println(MaxId);
		//插入新纪录
		DataStatement.executeUpdate(
				"INSERT INTO myfriend VALUES ('" + MaxId + "', '" + userid + "', '" + NewName + "', '" + NewSex + "', '" + NewAge + "', '" + NewQQ + "', '" + NewTel + "', '" + NewMail + "', '" + NewAddr + "')");
		return 0;
	}
	
	/**
	 * 
	 * @param FriendId
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	static public void DeleteFriend(String FriendId) throws ClassNotFoundException, SQLException{
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库
		Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
		Statement DataStatement = DataConnection.createStatement();
		//删除id
		DataStatement.executeUpdate(
				"delete from myfriend where id = '" + FriendId + "'");
	}
	
	static public int Modify(String FriendId,String NewName,String NewSex,String NewAge,String NewQQ,String NewTel,String NewMail,String NewAddr) throws ClassNotFoundException, SQLException{
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库
		Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
		Statement DataStatement = DataConnection.createStatement();
		//插入新纪录
		DataStatement.executeUpdate(
				"UPDATE myfriend SET name='" + NewName + "', sex='" + NewSex + "', age='" + NewAge + "', qq='" + NewQQ + "', telephone='" + NewTel + "', email='" + NewMail + "', address='" + NewAddr + "' WHERE id = '" + FriendId + "' ");
		return 0;
	}
}

这个类全是对于数据库的操作,包括登录,注册,新增,查找,修改等


index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>主页</title>
  </head>
  <body>
	<%
	if (session.getAttribute("LoginedUserName") == null){
		out.print("请先登录!<br/> <a href='LoginForm.jsp' />登录</a>");
  		return;
	}
	 %>
	 <h1>我的好友录</h1><br/>
	 <p/>
	 <hr/>
	 <a href="allfriend.jsp">查询所有好友信息</a><br/>
	 <a href="querybyname.jsp">按姓名模糊查询好友信息</a><br/>
	 <a href="logout.jsp">登出</a><br/>
	 <hr/>
	 当前时间:<%=(new java.util.Date()).toString()%>
  </body>
</html>

主页,显示几个功能的链接。


LoginForm.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登录</title>
  </head>
  <body>
  	<%
  	if (session.getAttribute("LoginedUserName") != null){
  		out.print("您已经登录了!<br/> <a href='index.jsp' />主页</a>");
  		return;
  	}
  	 %>
  	<script type="text/javascript">
	function login(){
		if (document.LoginForm.UserName.value == ""){
			alert("用户名不能为空!");
			return false;
		}
		return true;
	}
	</script>
	<h1>我的好友录</h1>
  	<hr/>
  	用户注册
  	<form name=LoginForm action="login.jsp" method="post">
  		<table border="0" width="100%">
  			<tr>
  				<td width="47%" align="right">登录名称</td>
  				<td><input type=text name=UserName></td>
  			</tr>
  			<tr>
  				<td align="right">登录密码</td>
  				<td><input type=password name=PassWord></td>
  			</tr>
  			<tr>
  				<td colspan="2" align="center">
  					<input type="submit" onclick="return login();" value="登录">
  					<input type="reset" value="重置">
  					<input type="button" value="注册" onclick="window.location.href='RegisterForm.jsp'">
  				</td>
			</tr>
  		</table>
  	</form>
  	<hr/>
  	当前时间:<%=(new java.util.Date()).toString()%>
  </body>
</html>

登录页面


login.jsp

<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body>
    <%
    String UserName = request.getParameter("UserName");
    String PassWord = request.getParameter("PassWord");
    if (UserName == null || PassWord == null){
    	response.sendRedirect("LoginForm.jsp");
    }
    int LoginUserResult = DataBaseOperate.LoginUser(UserName,PassWord);
    if (LoginUserResult == 0){
    	//登录成功
		session.setAttribute("LoginedUserName", request.getParameter("UserName"));
		out.print("<h1>登录成功!</h1> <br /> <a href='index.jsp' />主页</a>");
    }
    else if (LoginUserResult == 1){
    	out.print("<h1>用户名不存在!</h1> <br /> <a href='LoginForm.jsp' />重新登录</a>");
    }
    else if (LoginUserResult == 2){
    	out.print("<h1>密码不正确!</h1> <br /> <a href='LoginForm.jsp' />重新登录</a>");
    }
     %>
  </body>
</html>
登录的功能实现页面


logout.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  
  <body>
    <%
    if (session.getAttribute("LoginedUserName") == null){
    	out.print("未登录!<br/> <a href='LoginForm.jsp' />登录</a>");
  		return;
  	}
  	else{
  		session.removeAttribute("LoginedUserName");
  		out.print("已经登出。<br/> <a href='LoginForm.jsp' />登录</a>");
  	}
     %>
  </body>
</html>

登出的功能实现页面


RegisterForm.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>注册</title>
  </head>
  <body>
	<script type="text/javascript">
	function register(){
		if (document.RegisterForm.UserName.value == ""){
			alert("用户名不能为空!");
			return false;
		}
		if (document.RegisterForm.PassWord.value.length < 6){
			alert("密码不能少于6位!");
			return false;
		}
		if (document.RegisterForm.PassWord.value != document.RegisterForm.PassWordAgain.value){
			alert("两次密码必须一致!");
			return false;
		}
		return true;
	}
	</script>
  	<h1>我的好友录</h1>
  	<hr/>
  	用户注册
  	<form name=RegisterForm action="register.jsp" method="post">
  		<table border="0" width="100%">
  			<tr>
  				<td width="45%" align="right">登录名称</td>
  				<td><input type=text name=UserName></td>
  			</tr>
  			<tr>
  				<td align="right">登录密码</td>
  				<td><input type=password name=PassWord></td>
  			</tr>
  			<tr>
  				<td align="right">密码确认</td>
  				<td><input type=password name=PassWordAgain></td>
  			</tr>
  			<tr>
  				<td colspan="2" align="center">
  					<input type="submit" onclick="return register();" value="注册">
  					<input type="reset" value="重置">
  				</td>
			</tr>
  		</table>
  	</form>
  	<hr/>
  	当前时间:<%=(new java.util.Date()).toString()%>
  </body>
</html>

注册页面


register.jsp

<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body>
  	<%
  	String UserName = request.getParameter("UserName");
    String PassWord = request.getParameter("PassWord");
    if (UserName == null || PassWord == null){
    	response.sendRedirect("RegisterForm.jsp");
    }
  	int RegisterResult = DataBaseOperate.RegisterUser(UserName,PassWord);
  	if (RegisterResult == 1){
  		out.print("<h1>注册成功!</h1> <br /> <a href='LoginForm.jsp' />登录</a>");
  	}
  	else if (RegisterResult == 0){
  		out.print("<h1>用户名已经存在!</h1> <br /> <a href='RegisterForm.jsp' />重新注册</a>");
  	}
	%>
  </body>
</html>



allfriend.jsp

<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>查询所有好友信息</title>
  </head>
  
  <body>
    <%
	if (session.getAttribute("LoginedUserName") == null){
		out.print("请先登录!<br/> <a href='LoginForm.jsp' />登录</a>");
  		return;
	}
	 %>
	 <h1>我的好友录</h1><br/>
	 <a href="index.jsp">主页</a>
	 <hr/>
	 <form name='TableForm' action="allfriend.jsp" method="get">
		 <input type="hidden" name="SubmitOperate" value="Nothing">
		 <a href="InsertNewRowForm.jsp">添加新纪录</a>
		 <a href="javascript:TableForm.submit()" onclick="document.TableForm.SubmitOperate.value='Modify'";>修改选中记录</a>
		 <a href="javascript:TableForm.submit()" onclick="document.TableForm.SubmitOperate.value='Delete'">删除选中记录</a>
		 <hr/>
		 	<%
	    	request.setCharacterEncoding("utf-8");
		 	String SubStr = request.getParameter("FriendName");
		 	if (SubStr == null){
		 		SubStr = "";
		 	}
		 	String SubmitOperate = request.getParameter("SubmitOperate");
		 	if (SubmitOperate != null){
			 	if (SubmitOperate.equals("Modify")){
			 		//获取修改的id
			 		response.sendRedirect("ModifyForm.jsp?id=" + request.getParameter("Modify"));
			 	}
			 	else if (SubmitOperate.equals("Delete")){
			 		//获取需要删除的id数组
			 		String[] CheckBoxs = request.getParameterValues("Delete");
				 	if (CheckBoxs != null){
				 		for (int i = 0;i < CheckBoxs.length;i ++){
				 			DataBaseOperate.DeleteFriend(CheckBoxs[i]);
				 		}
				 	}
			 	}
		 	}
		 	String[][] FriendList = DataBaseOperate.GetAllFriends((String)session.getAttribute("LoginedUserName"),SubStr);
		 	session.setAttribute("TempRows", FriendList);
		 	%>
		 	<table width='100%' border='1'>
			 	<tr>
			 		<td><strong>修改</strong></td>
			 		<td><strong>删除</strong></td>
			 		<td><strong>姓名</strong></td>
			 		<td><strong>性别</strong></td>
			 		<td><strong>年龄</strong></td>
			 		<td><strong>QQ</strong></td>
			 		<td><strong>电话</strong></td>
			 		<td><strong>E-Mail</strong></td>
			 		<td><strong>地址</strong></td>
			 	</tr>
		 	<%
		 	if (FriendList != null){
				for (int i = 0;i < FriendList.length;i ++){
					out.print("<tr>");
					out.print("<td><input type='radio' name='Modify' value='" + FriendList[i][0] + "'/></td>");
					out.print("<td><input type='checkbox' name='Delete' value='" + FriendList[i][0] + "'/></td>");
					for (int j = 2; j < FriendList[i].length;j ++){
						out.print("<td>" + FriendList[i][j] + "</td>");
					}
					out.print("</tr>");
				}
			}
			else{
				%>
				<tr>
			 		<td colspan='9'>没有记录</td>
			 	</tr>
			 	<%
			}
			%>
			</table>
		</form>
	 	<hr/>
	 	当前时间:<%=(new java.util.Date()).toString()%>
  </body>
</html>

显示全部好友页面,SubName是参数,只显示包含SubName的记录,主要与querybyname.jsp配合,显示特定的朋友。

allfriend中有2个超链接,其中用超链接提交表单的用法是:

<a href="javascript:TableForm.submit()" onclick="document.TableForm.SubmitOperate.value='Delete'">删除选中记录</a>
由于有修改和删除2个超链接,后台页面并不知道是这次提交是修改还是删除,所以又使用了一个

<input type="hidden" name="SubmitOperate" value="Nothing">

用于记录这次的操作是删除还是修改。


querybyname.jsp

<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>按姓名模糊查询好友信息</title>
  </head>
  
  <body>
    <%
	if (session.getAttribute("LoginedUserName") == null){
		out.print("请先登录!<br/> <a href='LoginForm.jsp' />登录</a>");
  		return;
	}
	%>
	<script>
		function FindByName(){
			if (document.QueryNameForm.FriendName.value == ""){
				alert("查询名字不能为空!");
				return false;
			}
			return true;
		}
	</script>
	<h1>我的好友录</h1><br/>
	<a href="index.jsp">主页</a>
	<hr/>
	<form name='QueryNameForm' action='allfriend.jsp' method='post'>
		输入要查询的名字:<input type='text' name='FriendName'/><input type='submit' onclick='return FindByName();'/>
	</form>
	<hr/>
	当前时间:<%=(new java.util.Date()).toString()%>
  </body>
</html>

模糊查询好友页面,具体实现为allfriend.jsp


InsertNewRowForm.jsp

<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>添加新纪录</title>
  </head>
  
  <body>
    <%
	if (session.getAttribute("LoginedUserName") == null){
		out.print("请先登录!<br/> <a href='LoginForm.jsp' />登录</a>");
  		return;
	}
	 %>
	 <script>
	 function insert(){
	 	if (document.NewRowForm.NewName.value == ""){
	 		alert("姓名不能为空!");
	 		return false;
	 	}
	 	else if (document.NewRowForm.NewAge.value == ""){
	 		alert("年龄不能为空!");
	 		return false;
	 	}
	 	return true;
	 }
	 </script>
	 <h1>我的好友录</h1><br/>
	 <a href="index.jsp">主页</a>
	 <hr/>
	 <form name='NewRowForm' action="insertrow.jsp" method="post">
		<hr/>
		<table width='100%' border='1'>
			<tr>
				<td><strong>姓名</strong></td>
				<td><strong>性别</strong></td>
				<td><strong>年龄</strong></td>
				<td><strong>QQ</strong></td>
				<td><strong>电话</strong></td>
				<td><strong>E-Mail</strong></td>
				<td><strong>地址</strong></td>
			</tr>
			<tr>
				<td><input type="text" name="NewName"/></td>
				<td><input type="text" name="NewSex"/></td>
				<td><input type="text" name="NewAge"/></td>
				<td><input type="text" name="NewQQ"/></td>
				<td><input type="text" name="NewTel"/></td>
				<td><input type="text" name="NewMail"/></td>
				<td><input type="text" name="NewAddr"/></td>
			</tr>
			<tr>
				<td colspan="7" align="center"><input type="submit" value="添加新纪录" onclick="return insert();"/></td>
		 	</tr>
		</table>
		</form>
	 	<hr/>
	 	当前时间:<%=(new java.util.Date()).toString()%>
  </body>
</html>

插入新纪录页面


insertrow.jsp

<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<body>
    <%
	if (session.getAttribute("LoginedUserName") == null){
		out.print("请先登录!<br/> <a href='LoginForm.jsp' />登录</a>");
  		return;
	}
	else{
		request.setCharacterEncoding("utf-8");
		String NewName = request.getParameter("NewName");
		String NewSex = request.getParameter("NewSex");
		String NewAge = request.getParameter("NewAge");
		String NewQQ = request.getParameter("NewQQ");
		String NewTel = request.getParameter("NewTel");
		String NewMail = request.getParameter("NewMail");
		String NewAddr = request.getParameter("NewAddr");
		if (NewName == null || NewSex == null || NewAge == null || NewQQ == null || NewTel == null || NewMail == null || NewAddr == null){
			response.sendRedirect("InsertNewRowForm.jsp");
		}
		DataBaseOperate.Insert((String)session.getAttribute("LoginedUserName"), NewName, NewSex, NewAge, NewQQ, NewTel, NewMail, NewAddr);
		response.sendRedirect("allfriend.jsp");
	}
	%>
  	</body>
</html>

插入新纪录实现页面


ModifyForm.jsp

<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>添加新纪录</title>
  </head>
  
  <body>
    <%
	if (session.getAttribute("LoginedUserName") == null){
		out.print("请先登录!<br/> <a href='LoginForm.jsp' />登录</a>");
  		return;
	}
	 %>
	 <script>
	 function insert(){
	 	if (document.NewRowForm.NewName.value == ""){
	 		alert("姓名不能为空!");
	 		return false;
	 	}
	 	else if (document.NewRowForm.NewAge.value == ""){
	 		alert("年龄不能为空!");
	 		return false;
	 	}
	 	return true;
	 }
	 </script>
	 <%
	 String FriendId = request.getParameter("id");
	 if (FriendId == null){
	 	response.sendRedirect("index.jsp");
	 }
	 String[] FriendContent = DataBaseOperate.GetFriend(FriendId);
	 if (FriendContent == null){
	 	response.sendRedirect("allfriend.jsp");
	 	return;
	 }
	  %>
	 <h1>我的好友录</h1><br/>
	 <a href="index.jsp">主页</a>
	 <hr/>
	 <form name='NewRowForm' action="Modify.jsp" method="post">
		<hr/>
		<input type="hidden" name="FriendId" value="<%=FriendId %>"/>
		<table width='100%' border='1'>
			<tr>
				<td><strong>姓名</strong></td>
				<td><strong>性别</strong></td>
				<td><strong>年龄</strong></td>
				<td><strong>QQ</strong></td>
				<td><strong>电话</strong></td>
				<td><strong>E-Mail</strong></td>
				<td><strong>地址</strong></td>
			</tr>
			<tr>
				<td><input type="text" name="NewName" value="<%=FriendContent[2]%>"/></td>
				<td><input type="text" name="NewSex" value="<%=FriendContent[3]%>"/></td>
				<td><input type="text" name="NewAge" value="<%=FriendContent[4]%>"/></td>
				<td><input type="text" name="NewQQ" value="<%=FriendContent[5]%>"/></td>
				<td><input type="text" name="NewTel" value="<%=FriendContent[6]%>"/></td>
				<td><input type="text" name="NewMail" value="<%=FriendContent[7]%>"/></td>
				<td><input type="text" name="NewAddr" value="<%=FriendContent[8]%>"/></td>
			</tr>
			<tr>
				<td colspan="7" align="center"><input type="submit" value="修改纪录" onclick="return insert();"/></td>
		 	</tr>
		</table>
		</form>
	 	<hr/>
	 	当前时间:<%=(new java.util.Date()).toString()%>
  </body>
</html>
修改记录页面


Modify.jsp

<%@ page language="java" import="java.util.*,com.myfriend.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<body>
    <%
	if (session.getAttribute("LoginedUserName") == null){
		out.print("请先登录!<br/> <a href='LoginForm.jsp' />登录</a>");
  		return;
	}
	else{
		request.setCharacterEncoding("utf-8");
		String NewName = request.getParameter("NewName");
		String NewSex = request.getParameter("NewSex");
		String NewAge = request.getParameter("NewAge");
		String NewQQ = request.getParameter("NewQQ");
		String NewTel = request.getParameter("NewTel");
		String NewMail = request.getParameter("NewMail");
		String NewAddr = request.getParameter("NewAddr");
		if (NewName == null || NewSex == null || NewAge == null || NewQQ == null || NewTel == null || NewMail == null || NewAddr == null){
			response.sendRedirect("ModifyForm.jsp");
		}
		DataBaseOperate.Modify(request.getParameter("FriendId"), NewName, NewSex, NewAge, NewQQ, NewTel, NewMail, NewAddr);
		response.sendRedirect("allfriend.jsp");
	}
	%>
  	</body>
</html>
修改记录实现页面


注意:如果用户没有登录,那他不能访问除登录、注册外的所有页面。

还有有些功能有2个页面组成,一个是表单,一个是具体的程序,后台的页面不应该被除了前台访问以外的任何其他方式访问。














2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:112898次
    • 积分:1986
    • 等级:
    • 排名:千里之外
    • 原创:81篇
    • 转载:76篇
    • 译文:0篇
    • 评论:18条
    最新评论