【jsp】兴唐第三十节课作业

本文介绍了一个使用JSP实现的学生信息管理系统,该系统包括学生信息的查询、添加、删除及更新等功能,并支持用户登录验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写一个jsp调取数据库的文件显示在主页面,并实现查找、添加、删除和数据更新以及用户登录的功能

stuList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "java.sql.*" %>
<%@ page import = "com.zzxtit.jsp30.*"%>
<!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>

<%
	request.setCharacterEncoding("utf-8");
	String id = request.getParameter("id");
	String real_name = request.getParameter("real_name");
	Class.forName("com.mysql.jdbc.Driver");
	String url = "jdbc:mysql://localhost:3306/system";
	System.out.println("=========================");
	Connection conn = DriverManager.getConnection(url,"root","root");
	Statement stat = null;
	ResultSet rs = null;
	String sql = "select * from user_infer where 1 = 1 ";
	if(id != null && !"".equals(id)){
		sql += " and id = '"+id+"'";
	}
	if(real_name != null && !"".equals(real_name)){
		sql += " and real_name  like '%"+real_name+"%'";
	}
	System.out.println(sql);
	try{
		stat = conn.createStatement();
		rs = stat.executeQuery(sql);	
%>
<div align = "center">
<form action = "" method = "post">
<table>
<tr>
	<td>学生学号</td> <td><input type = "text" name = "id"></td>
	<td>学生姓名</td> <td><input type = "text" name = "real_name"></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="查询"> </td><td colspan="2"><input type="reset" value="重置"> </td>
</tr>
</table>

</form>
</div>


	<div align = "center">
		<h3>用户信息列表</h3>
		<hr />
		<table border = "1px" width = "70%" bordercolor = "red" cellSpacing = "0px" >
			<tr>
				<th>学生编号</th><th>学生姓名</th><th>真实姓名</th><th>年龄</th>
			</tr>
			<%
			while(rs.next()){
			%>
			<tr align = "center">
				<td><%out.print(rs.getString(1)); %>  </td>
				<td><%out.print(rs.getString(2)); %>  </td>
				<td><%out.print(rs.getString(4)); %>  </td>
				<td><%out.print(rs.getInt(5)); %>  </td>
<td><a href="update.jsp?id=<%=rs.getString(1) %>">修改    </a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="delete.jsp?id=<%=rs.getString(1) %>">删除</a></td></tr>
		<% System.out.println("-----" + rs.getString(1));
			}
			}catch(Exception e){
		
	}finally{
		if(rs != null){
			rs.close();
		}
		if(stat != null){
			stat.close();
		}
		if(conn != null){
			conn.close();
		}
	}
	%>
		</table>
	</div>
</body>
</html>

注:

String sql = "select * from user_infer where 1 = 1 ";

这条语句写的很精妙,实现了知道学号、姓名用其中任意一个就可以查询的目的。

AddStudent.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>学生信息添加</title>
</head>
<body>
	<div><span><a href = "Rquest/Request.jsp">添加</a></span></div>
	<div align = "center">
	<h3>添加学生信息</h3>
	<hr>
	<form action = "doAddStu.jsp" method = "post">
	编号:<input type = "text" name = "id"><br/>
	网名:<input type = "text" name = "name"><br/>
	密码: <input type = "password" name = "password"><br/>
	真实姓名:<input type = "text" name = "real_name"><br>
	年龄:<input type = "text" name = "age"><br/>
	<input type = "submit" value = "提交">
	<input type = "reset" value = "重置">
	</form>
	<div>
</body>
</html>

doAddStudent.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import = "java.sql.*"	
    %>
<!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.setCharacterEncoding("utf-8");
	String id = request.getParameter("id");
	String name = request.getParameter("name");
	String password = request.getParameter("password");
	String real_name = request.getParameter("real_name");
	String age = request.getParameter("age");
	
	String sql = "insert into user_infer values (?,?,?,?,?)";
	Class.forName("com.mysql.jdbc.Driver");
	Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/system","root","root");
	PreparedStatement ps = null;
	try{
		ps = conn.prepareStatement(sql);
		ps.setInt(1,Integer.parseInt(id));
		ps.setString(2,name);
		ps.setString(3,password);
		ps.setString(4,real_name);
		ps.setInt(5,Integer.parseInt(age));
		if(ps.executeUpdate() > 0){
			response.sendRedirect("stuList.jsp");
		}else{
			out.println("数据保存失败!!!");
		}
	}catch(SQLException e){
		e.printStackTrace();
	}finally{
		if(ps != null){
			ps.close();
		}
		
		if(conn != null){
			conn.close();
		}
		
	}
%>
</body>
</html>

update.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "java.sql.*"%>
<%@ page import = "com.zzxtit.jsp30.*"%>
<!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 id = request.getParameter("id");
	Connection conn = DbUtil.getDBConn();
	Statement stat = null;
	ResultSet rs = null;
	User user= new User();
	try{
		stat = conn.createStatement();
		String sql = "select * from user_infer where id = '"+id+"'";
			System.out.println(sql);
		rs = stat.executeQuery(sql);
		if(rs.next()){
			user.setId(rs.getString(1));
			System.out.println(user.getId() + "=====");
			user.setUser_name(rs.getString(2));
			user.setPassword(rs.getString(3));
			user.setReal_name(rs.getString(4));
			user.setAge(rs.getInt(5));
		}	
	}catch(SQLException e){
		e.printStackTrace();
	}finally{
		DbUtil.close(conn,stat,rs);
	}
	
%>

<div align = "center">
	<hr>
	<form action = "doUpdate.jsp" method = "post" >
	编号:<input type = "text" name = "id" value = "<%=user.getId() %>"><br/>
	网名:<input type = "text" name = "name" value = "<%=user.getUser_name() %>"><br/>
	密码: <input type = "password" name = "password "value = "<%= user.getPassword() %>"><br/>
	真实姓名:<input type = "text" name = "real_name" value = "<%= user.getReal_name()%>"><br>
	年龄:<input type = "text" name = "age" value = "<%= user.getAge()%>"><br/>
	<input type = "submit" value = "提交">
	<input type = "reset" value = "重置">
	</form>
	<div>
</body>
</html>

doupdate.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "java.sql.*"%>
<%@ page import = "com.zzxtit.jsp30.*"%>
<!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.setCharacterEncoding("utf-8");
	String id = request.getParameter("id");
	String user_name = request.getParameter("user_name");
	String password = request.getParameter("password");
	String real_name = request.getParameter("real_name");
	String age = request.getParameter("age");
	String sql = "update user_infer set user_name = '"+user_name+"', password = '"+password+
			"', real_name = '"+real_name+"', age = "+age+" where id = '"+id+"'";
	Connection conn = DbUtil.getDBConn();
	Statement stat = null;
	try{
		stat = conn.createStatement();
		if(stat.executeUpdate(sql) > 0){
			response.sendRedirect("stuList.jsp");
		}
	}catch(SQLException e){
		e.printStackTrace();
	}finally{
		DbUtil.close(conn, stat);
	}
%>
</body>
</html>

delete.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "java.sql.*"%>
<%@ page import = "com.zzxtit.jsp30.*"%>
<!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 id = request.getParameter("id");
	Connection conn = DbUtil.getDBConn();
	Statement stat = null;
	try{
		stat = conn.createStatement();
		String sql = "delete from user_infer where id = '"+id+"'";
		System.out.println(sql);
	if(stat.executeUpdate(sql) > 0){
		response.sendRedirect("stuList.jsp");
	}	
	}catch(SQLException e){
		e.printStackTrace();
	}finally{
		DbUtil.close(conn,stat);
	}
	
%>

</body>
</html>

login.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "java.sql.*" %>
<%@ page import = "com.zzxtit.jsp30.*"%>
<!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>
<%	
	Object msg = request.getAttribute("msg");
        

%>
<body>
    
</body>
</html>

dologin.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "java.sql.*" %>
<%@ page import = "com.zzxtit.jsp30.*"%>
<!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 userName = request.getParameter("userName");
String passwd = request.getParameter("passwd");
Connection conn = DbUtil.getDBConn();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user_infer where id = ? and password = ?";
try{
	ps = conn.prepareStatement(sql);
	
	ps.setString(1,userName);
	ps.setString(2,passwd);
	rs = ps.executeQuery();
	System.out.println(sql);
	if(rs.next()){
			
		//用户与服务器的会话对象 HttpSession session:
		//session有效范围:在用户的整个会话期间都有效,当关闭浏览器或者长时间不与服务器交互,
		//session会过期,此时需要重新登录,session默认失效时间:30分钟,在web.xml中设置过期时间
		//作业:将用户信息查询出来,存储session中
		//session.setAttribute("user_infer", "login");
		
		response.sendRedirect("stuList.jsp");
		
	}else{
		//向请求页面发送数据
		request.setAttribute("msg", "用户名或密码错误!");
		//服务器端跳转:地址不变,内容变为跳转后的页面内容
		request.getRequestDispatcher("login.jsp").forward(request, response);
	}
}catch(SQLException e){
	e.printStackTrace();
}finally{
	DbUtil.close(conn, ps);
}
%>

</body>
</html>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值