目标:学会使用JSP+Servlet+Javabean模式进行简单的业务系统开发
新建两张父子表:1、学生表 2、学生发表的论文表
实现学生基本信息增删改查,针对某一个学生、对发表的论文进行增删改查,
说明:不允许使用其它框架,纯粹使用Model1(JSP+Servlet+Javabean)来进行开发
1.创建index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type = "text/css">
a:link,a:visited{
color:#0044DD;
text:-decoration:none;}
table th{
color:#2467fa;
background:#dadada;
font-size:16px;
border:1px solid #fffff;
}
table td{
background:#dadada;
text-align:center;
border-align:1px solid #ffffff;
}
</style>
<title>主界面</title>
</head>
<body>
<div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">
<h1 style="font-family:仿宋">账号信息管理系统</h1>
<table align="center" width="400px" border="1">
<tr><th>用户名</th><th>密码</th><th>年龄</th><th>操作</th></tr>
<%
Class.forName("com.mysql.cj.jdbc.Driver");
//Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT+8","root","laotu123");
String sql = "select * from userinfo";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
%>
<tr><td><%=rs.getString("name") %></td>
<td><%=rs.getString("password") %></td>
<td><%= rs.getInt("age") %></td>
<td><button type = "button" onclick="alert('请先登陆!')">修改</button>
<button type = "button" onclick="alert('请先登陆!')">删除</button>
</td>
</tr>
<%
}
rs.close();
stmt.close();
con.close();
%>
</table>
<br>
<button type = "button" align = "center" onclick="window.location.href='login.jsp'" ><h4>登陆</button>
<br><br>
</div>
</body>
</html>
2.创建login.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type = "text/css">
a:link,a:visited{
color:#0044DD;
text:-decoration:none;}
table th{
color:#2467fa;
background:#dadada;
font-size:16px;
border:1px solid #fffff;
}
table td{
background:#dadada;
text-align:center;
border-align:1px solid #ffffff;
}
</style>
<title>登陆界面</title>
</head>
<body>
<div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">
<h1 style="font-family:仿宋">账号信息管理系统</h1>
<form name = "form1" action = "login_do.jsp" method="POST">
<table align="center" width="300px" border="1">
<tr><th>用户名</th>
<td><input type = "text" name = "username"></td>
</tr>
<tr><th>密码</th>
<td><input type = "password" name = "password"></td>
</tr>
<tr><th colspan = "2" align = "center"><input type = "reset" value = "重置" >
<input type = "submit" value = "登陆"></th>
</tr>
</table>
</form>
<br>
</div>
</body>
</html>
3.创建login_success.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*" %>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type = "text/css">
a:link,a:visited{
color:#0044DD;
text:-decoration:none;}
table th{
color:#2467fa;
background:#dadada;
font-size:16px;
border:1px solid #fffff;
}
table td{
background:#dadada;
text-align:center;
border-align:1px solid #ffffff;
}
</style>
<title>登陆成功</title>
</head>
<body>
<%String name1 = (String)session.getAttribute("username");
if(name1 == null) {
String mgs = "您尚未登陆,请先登陆,3秒后跳转登陆界面!";%>
<h1 align="center" />
<%=mgs %>
<%
response.setHeader("Refresh", "3;login.jsp");
}
else{
%>
<div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">
<h1 style="font-family:仿宋">账号信息管理系统</h1>
<h2 style="font-family:仿宋"><%=name1 %>,欢迎您!</h2>
<table align="center" width="400px" border="1">
<tr><th>用户名</th><th>密码</th><th>年龄</th><th>操作</th></tr>
<%
Class.forName("com.mysql.cj.jdbc.Driver");
//Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT+8","root","laotu123");
String sql = "select * from userinfo";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
%>
<tr><td><%=rs.getString("name") %></td>
<td><%=rs.getString("password") %></td>
<td><%= rs.getInt("age") %></td>
<td><input type="button" onclick="window.location.href='edit.jsp?name=<%=rs.getString("name") %>'" value="修改">
<a href="del.jsp?name=<%=rs.getString("name") %>" onclick="return confirm('确定删除吗?')"><input type = "button" value="删除"></a>
</td>
</tr>
<%
}
rs.close();
stmt.close();
con.close();
%>
</table>
<br>
<table align="center" >
<tr>
<td><input type="button" onclick="window.location.href='query_paper.jsp'" value="查询论文"></td>
<td> </td>
<td><input type="button" onclick="window.location.href='add.jsp'" value="增加用户"></td>
<td> </td>
<td><input type="button" onclick="window.location.href='query.jsp'" value="查询用户"></td>
<td> </td>
<td><a href="login_out.jsp" onclick="return confirm('确定退出吗?')"><input type="button" value="退出登陆"></a></td>
</tr>
</table>
<br>
</div>
<%} %>
</body>
</html>
创建login_out.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>退出登陆处理界面</title>
</head>
<body>
<%session.setAttribute("username",null);
String mgs = "退出成功,3秒后跳转到主界面!";%>
<h1 align="center" /><%=mgs %>
<% response.setHeader("Refresh", "3;index.jsp");%>
</body>
</html>
创建login_do.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登陆验证页面</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("username");
String password = request.getParameter("password");
String sql = "select count(*) from userinfo where name = ? and password = ?";
Class.forName("com.mysql.cj.jdbc.Driver");
//Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT+8","root","laotu123");
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2,password);
ResultSet rs = pstmt.executeQuery();
String a = "1";
while(rs.next()) {
if(a.equals(rs.getString("count(*)"))) {
session.setAttribute("username", name);
%>
<jsp:forward page="login_success.jsp"></jsp:forward>
<% }
else {
String mgs = "登录失败,3秒后跳转到主界面!";%>
<h1 align="center" />
<%=mgs %>
<%
response.setHeader("Refresh", "3;index.jsp");
}
}
rs.close();
pstmt.close();
con.close();
%>
</body>
</html>
创建query.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type = "text/css">
a:link,a:visited{
color:#0044DD;
text:-decoration:none;}
table th{
color:#2467fa;
background:#dadada;
font-size:16px;
border:1px solid #fffff;
}
table td{
background:#dadada;
text-align:center;
border-align:1px solid #ffffff;
}
</style>
<title>查询用户</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name1 = (String)session.getAttribute("username");%>
<div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">
<h1 style="font-family:仿宋">账号信息管理系统</h1>
<h2 style="font-family:仿宋"><%=name1 %>,欢迎您查询信息!</h2>
<h3 style="font-family:仿宋">请输入您要查询的用户名!</h2>
<form name="form1" action="query_do.jsp" method="POST" >
<table align="center">
<tr><th>用户名:</th><td><input type="text" name="username1" ></td><td><input type="submit" value="查询"></td></tr>
</table>
</form>
<br>
</div>
</body>
</html>
创建query_do.jsp<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type = "text/css">
a:link,a:visited{
color:#0044DD;
text:-decoration:none;}
table th{
color:#2467fa;
background:#dadada;
font-size:16px;
border:1px solid #fffff;
}
table td{
background:#dadada;
text-align:center;
border-align:1px solid #ffffff;
}
</style>
<title>查询处理页面</title>
</head>
<body>
<% request.setCharacterEncoding("UTF-8");
String name = request.getParameter("username1");
Class.forName("com.mysql.cj.jdbc.Driver");
//Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT+8","root","laotu123");
String sql = "select * from userinfo ";
Statement pstmt = con.createStatement();
ResultSet rs = pstmt.executeQuery(sql);
boolean flag = true;
while(rs.next()) {
if(name.equals(rs.getString("name"))) {
flag = false;
String username = rs.getString("name");
String password = rs.getString("password");
String age = rs.getString("age");
%>
<div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">
<h1 style="font-family:仿宋">账号信息管理系统</h1>
<h3 style="font-family:仿宋">查找成功,您查找的用户信息如下:</h3>
<table align="center">
<tr><th>用户名:</th><td><input type="text" value = "<%=username%>"></td></tr>
<tr><th>密码:</th><td><input type="text" value = "<%=password%>"></td></tr>
<tr><th>年龄:</th><td><input type="text" value = "<%=age%>"></td></tr>
</table>
<button type = "button" align = "center" onClick="window.location.href='login_success.jsp'" ><h4>返回</button>
<br><br>
<%}
}
if(flag) {
String mgs = "查询失败,3秒后跳转到登陆成功界面!";%>
<h1 align="center" /><%=mgs %>
<%
response.setHeader("Refresh", "3;login_success.jsp");}
pstmt.close();
con.close();
%>
</body>
</html>
创建query_paper.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*" %>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type = "text/css">
a:link,a:visited{
color:#0044DD;
text:-decoration:none;}
table th{
color:#2467fa;
background:#dadada;
font-size:16px;
border:1px solid #fffff;
}
table td{
background:#dadada;
text-align:center;
border-align:1px solid #ffffff;
}
</style>
<title>登陆成功</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name1 = (String)session.getAttribute("username");
if(name1 == null) {
String mgs = "您尚未登陆,请先登陆,3秒后跳转登陆界面!";%>
<h1 align="center" />
<%=mgs %>
<%
response.setHeader("Refresh", "3;login.jsp");
}
else{
%>
<div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">
<h1 style="font-family:仿宋">学生论文信息管理</h1>
<h2 style="font-family:仿宋"><%=name1 %>,欢迎您!</h2>
<table align="center" width="400px" border="1">
<tr><th>用户名</th><th>论文</th><th>操作</th></tr>
<%
Class.forName("com.mysql.cj.jdbc.Driver");
//Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT+8","root","laotu123");
String sql = "select * from userpaper";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
%>
<tr><td><%=rs.getString("name") %></td>
<td><%=rs.getString("paper") %></td>
<td><input type="button" onclick="window.location.href='edit_paper.jsp?paper=<%=rs.getString("paper") %>'" value="修改">
<a href="del_paper.jsp?paper=<%=rs.getString("paper") %>" onclick="return confirm('确定删除吗?')"><input type = "button" value="删除"></a>
</td>
</tr>
<%
}
rs.close();
stmt.close();
con.close();
%>
</table>
<br>
<table align="center" >
<tr>
<td><input type="button" onclick="window.location.href='add_paper.jsp'" value="增加论文"></td>
<td> </td>
<td><input type="button" onclick="window.location.href='query_query_paper.jsp'" value="查询论文"></td>
<td> </td>
<td><a href="login_out.jsp" onclick="return confirm('确定退出吗?')"><input type="button" value="退出登陆"></a></td>
</tr>
</table>
<br>
</div>
<%} %>
</body>
</html>
创建query_query_paper.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type = "text/css">
a:link,a:visited{
color:#0044DD;
text:-decoration:none;}
table th{
color:#2467fa;
background:#dadada;
font-size:16px;
border:1px solid #fffff;
}
table td{
background:#dadada;
text-align:center;
border-align:1px solid #ffffff;
}
</style>
<title>查询用户</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name1 = (String)session.getAttribute("username");%>
<div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">
<h1 style="font-family:仿宋">账号信息管理系统</h1>
<h2 style="font-family:仿宋"><%=name1 %>,欢迎您查询信息!</h2>
<h3 style="font-family:仿宋">请输入您要查询的用户名!</h2>
<form name="form1" action="query_query_paper_do.jsp" method="POST" >
<table align="center">
<tr><th>论文名:</th><td><input type="text" name="userpaper1" ></td><td><input type="submit" value="查询"></td></tr>
</table>
</form>
<br>
</div>
</body>
</html>
创建query_query_paper_do.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type = "text/css">
a:link,a:visited{
color:#0044DD;
text:-decoration:none;}
table th{
color:#2467fa;
background:#dadada;
font-size:16px;
border:1px solid #fffff;
}
table td{
background:#dadada;
text-align:center;
border-align:1px solid #ffffff;
}
</style>
<title>查询处理页面</title>
</head>
<body>
<% request.setCharacterEncoding("UTF-8");
String paper = request.getParameter("userpaper1");
Class.forName("com.mysql.cj.jdbc.Driver");
//Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT+8","root","laotu123");
String sql = "select * from userpaper ";
Statement pstmt = con.createStatement();
ResultSet rs = pstmt.executeQuery(sql);
boolean flag = true;
while(rs.next()) {
if(paper.equals(rs.getString("paper"))) {
flag = false;
String username = rs.getString("name");
String userpaper = rs.getString("paper");
%>
<div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">
<h1 style="font-family:仿宋">论文信息管理</h1>
<h3 style="font-family:仿宋">查找成功,您查找的论文信息如下:</h3>
<table align="center">
<tr><th>作者:</th><td><input type="text" value = "<%=username%>"></td></tr>
<tr><th>论文:</th><td><input type="text" value = "<%=userpaper%>"></td></tr>
</table>
<button type = "button" align = "center" onClick="window.location.href='query_paper.jsp'" ><h4>返回</button>
<br><br>
<%}
}
if(flag) {
String mgs = "查询失败,3秒后跳转到登陆成功界面!";%>
<h1 align="center" /><%=mgs %>
<%
response.setHeader("Refresh", "3;query_paper.jsp");}
pstmt.close();
con.close();
%>
</body>
</html>
创建edit.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type = "text/css">
a:link,a:visited{
color:#0044DD;
text:-decoration:none;}
table th{
color:#2467fa;
background:#dadada;
font-size:16px;
border:1px solid #fffff;
}
table td{
background:#dadada;
text-align:center;
border-align:1px solid #ffffff;
}
</style>
<title>修改用户信息</title>
</head>
<body>
<% String name1 = (String)session.getAttribute("username");
String name = request.getParameter("name");
request.setCharacterEncoding("UTF-8");
String sql = "select * from userinfo where name = ?";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT+8","root","laotu123");
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
String username = rs.getString("name");
String password = rs.getString("password");
String age = rs.getString("age");
%>
<div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">
<h1 style="font-family:仿宋">账号信息管理系统</h1>
<h2 style="font-family:仿宋"><%=name1 %>,欢迎您修改<%=name %>的信息!</h2>
<form name = "form1" action = "edit_do.jsp?name=<%=name %>" method="POST">
<table align="center" width="300px" border="1">
<tr><th>用户名</th>
<td><input type = "text" name = "username" value = "<%=username%>"></td>
</tr>
<tr><th>密码</th>
<td><input type = "password" name = "password" value = "<%=password%>"></td>
</tr>
<tr><th>年龄</th>
<td><input type = "text" name = "age" value = "<%=age%>"></td>
</tr>
<tr><th colspan = "2" align = "center"><input type = "reset" value = "重置" >
<input type = "submit" value = "修改"></th>
</tr>
</table>
</form>
<br>
<%
}
rs.close();
pstmt.close();
con.close();
%>
</body>
</html>
12创建edit_paper.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type = "text/css">
a:link,a:visited{
color:#0044DD;
text:-decoration:none;}
table th{
color:#2467fa;
background:#dadada;
font-size:16px;
border:1px solid #fffff;
}
table td{
background:#dadada;
text-align:center;
border-align:1px solid #ffffff;
}
</style>
<title>修改用户信息</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name1 = (String)session.getAttribute("username");
String paper = request.getParameter("paper");
String sql = "select * from userpaper where paper = ?";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT+8","root","laotu123");
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, paper);
ResultSet rs = pstmt.executeQuery();
if(rs.next()) {
String username = rs.getString("name");
String userpaper = rs.getString("paper");
%>
<div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">
<h1 style="font-family:仿宋">账号信息管理系统</h1>
<h2 style="font-family:仿宋"><%=name1 %>,欢迎您修改<%=paper %>的信息!</h2>
<form name = "form1" action = "edit_paper_do.jsp?paper=<%=paper %>" method="POST">
<table align="center" width="300px" border="1">
<tr><th>作者</th>
<td><input type = "text" name = "username" value = "<%=username%>"></td>
</tr>
<tr><th>论文</th>
<td><input type = "text" name = "userpaper" value = "<%=userpaper%>"></td>
</tr>
<tr><th colspan = "2" align = "center"><input type = "reset" value = "重置" >
<input type = "submit" value = "修改"></th>
</tr>
</table>
</form>
<br>
<%
}
rs.close();
pstmt.close();
con.close();
%>
</body>
</html>
创建edit_paper_do.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>修改处理页面</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String paper1 = request.getParameter("paper");
String name = request.getParameter("username");
String paper = request.getParameter("userpaper");
String sql = "update userpaper set name=?,paper=? where paper =?";
Class.forName("com.mysql.cj.jdbc.Driver");
//Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT+8","root","laotu123");
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, paper);
pstmt.setString(3, paper1);
int result = pstmt.executeUpdate();
if ( result == 1) {
String mgs = "修改成功,3秒后跳转到登陆成功界面!";%>
<h1 align="center" /><%=mgs %>
<%
response.setHeader("Refresh", "3;query_paper.jsp");
}else {
String mgs = "修改失败,3秒后跳转到登陆成功界面!";%>
<h1 align="center" /><%=mgs %>
<%
response.setHeader("Refresh", "3;query_paper.jsp");
}
pstmt.close();
con.close();
%>
</body>
</html>
创建edit_do.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>修改处理页面</title>
</head>
<body>
<%
String name1 = request.getParameter("name");
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("username");
String password = request.getParameter("password");
String age = request.getParameter("age");
String sql = "update userinfo set name=?,password=?,age=? where name =?";
Class.forName("com.mysql.cj.jdbc.Driver");
//Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT+8","root","laotu123");
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2,password);
pstmt.setString(3,age);
pstmt.setString(4,name1);
int result = pstmt.executeUpdate();
if ( result == 1) {
String mgs = "修改成功,3秒后跳转到登陆成功界面!";%>
<h1 align="center" /><%=mgs %>
<%
response.setHeader("Refresh", "3;login_success.jsp");
}else {
String mgs = "修改失败,3秒后跳转到登陆成功界面!";%>
<h1 align="center" /><%=mgs %>
<%
response.setHeader("Refresh", "3;login_success.jsp");
}
pstmt.close();
con.close();
%>
</body>
</html>
创建del.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>删除处理页面</title>
</head>
<body>
<% request.setCharacterEncoding("UTF-8");
String name1 = request.getParameter("name");
String name2 = (String)session.getAttribute("username");
if(name1.equals(name2)) {
%>
<div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">
<h1 style="font-family:仿宋">账号信息管理系统</h1>
<h3 style="font-family:仿宋">不好意思,您正在删除自己的信息,请先退出登陆!</h3>
<button type = "button" align = "center" onclick="window.location.href='login_success.jsp'" ><h4>返回主页</button>
<br>
<%}else{
String sql = "delete from userinfo where name=?";
Class.forName("com.mysql.cj.jdbc.Driver");
//Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=utf-8&serverTimezone=GMT+8","root","laotu123");
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, name1);
int result = pstmt.executeUpdate();
if ( result == 1) {
String mgs = "删除成功,3秒后跳转到登陆成功界面!";%>
<h1 align="center" /><%=mgs %>
<%
response.setHeader("Refresh", "3;login_success.jsp");
}else {
String mgs = "删除失败,3秒后跳转到登陆成功界面!";%>
<h1 align="center" /><%=mgs %>
<%
response.setHeader("Refresh", "3;login_success.jsp");
}pstmt.close();
con.close();
}
%>
</body>
</html>
创建del_paper.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>删除处理页面</title>
</head>
<body>
<% request.setCharacterEncoding("UTF-8");
String paper = request.getParameter("paper");
String sql = "delete from userpaper where paper=?";
Class.forName("com.mysql.cj.jdbc.Driver");
//Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","root");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?serverTimezone=GMT+8","root","laotu123");
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, paper);
int result = pstmt.executeUpdate();
if ( result == 1) {
String mgs = "删除成功,3秒后跳转到论文信息管理界面!";%>
<h1 align="center" ><%=mgs%></h1>
<%
response.setHeader("Refresh", "3;query_paper.jsp");
}else {
String mgs = "删除失败,3秒后跳转到论文信息管理界面!";%>
<h1 align="center" ><%=mgs%></h1>
<%
response.setHeader("Refresh", "3;query_paper.jsp");
}
pstmt.close();
con.close();
%>
</body>
</html>
创建add.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type = "text/css">
a:link,a:visited{
color:#0044DD;
text:-decoration:none;}
table th{
color:#2467fa;
background:#dadada;
font-size:16px;
border:1px solid #fffff;
}
table td{
background:#dadada;
text-align:center;
border-align:1px solid #ffffff;
}
</style>
<title>添加用户</title>
</head>
<body>
<%String name1 = (String)session.getAttribute("username");%>
<div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">
<h1 style="font-family:仿宋">账号信息管理系统</h1>
<h2 style="font-family:仿宋"><%=name1 %>,欢迎您添加新用户!</h2>
<form name="form1" action="add_do.jsp">
<table align="center" width="300px" border="1">
<tr><th>用户名:</th><td><input type="text" name="newname"></td></tr>
<tr><th>密码:</th><td><input type="text" name="newpassword"></td></tr>
<tr><th>年龄:</th><td><input type="text" name="newage"></td></tr>
<tr><th colspan = "2" align = "center"><input type = "reset" value = "重置" >
<input type = "submit" value = "增加"></th></tr>
</table>
</form>
<br>
</div>
</body>
</html>
创建add_paper.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type = "text/css">
a:link,a:visited{
color:#0044DD;
text:-decoration:none;}
table th{
color:#2467fa;
background:#dadada;
font-size:16px;
border:1px solid #fffff;
}
table td{
background:#dadada;
text-align:center;
border-align:1px solid #ffffff;
}
</style>
<title>添加用户</title>
</head>
<body>
<%String name1 = (String)session.getAttribute("username");%>
<div style="width:700px;margin:100px auto;text-align:center;line-height:100px;background:#ffffaa">
<h1 style="font-family:仿宋">账号信息管理系统</h1>
<h2 style="font-family:仿宋"><%=name1 %>,欢迎您添加新用户!</h2>
<form name="form1" action="add_paper_do.jsp">
<table align="center" width="300px" border="1">
<tr><th>作者:</th><td><input type="text" name="newname"></td></tr>
<tr><th>论文:</th><td><input type="text" name="newpaper"></td></tr>
<tr><th colspan = "2" align = "center"><input type = "reset" value = "重置" >
<input type = "submit" value = "增加"></th></tr>
</table>
</form>
<br>
</div>
</body>
</html>
18.创建add_do.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>增加处理页面</title>
</head>
<body>
<%String name1 = (String)session.getAttribute("username");
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("newname");
String password = request.getParameter("newpassword");
String age = request.getParameter("newage");
String sql = "insert into userinfo value(null,?,?,?)";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?serverTimezone=GMT+8","root","laotu123");
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2,password);
pstmt.setString(3,age);
int result = pstmt.executeUpdate();
if ( result == 1) {
String mgs = "添加成功,3秒后跳转到登陆成功界面!";%>
<h1 align="center" /><%=mgs %>
<%
response.setHeader("Refresh", "3;login_success.jsp");
}else {
String mgs = "添加失败,3秒后跳转到登陆成功界面!";%>
<h1 align="center" /><%=mgs %>
<%
response.setHeader("Refresh", "3;login_success.jsp");
}
pstmt.close();
con.close();
%>
</body>
</html>
测试:
创建数据库student,及创建userinfo表及userpaper表
进入账号信息管理系统
登陆
登陆成功
修改信息
删除信息
删除成功
查询userinfo表信息
查询成功
插入新用户
插入成功
查询论文信息
修改信息
修改成功
增添新论文
添加成功
删除论文
删除成功
退出系统
在实验过程中我遇到了修改论文表时上传数据为乱码问题
通过百度的方式,我发现自己的 request.setCharacterEncoding("UTF-8");不是放在最开头,使得在此之前就获取数据,使得上床中文时有论文,而其他方式的数据没有乱码,我把它放在开头解决了这个问题。