jsp实现学生信息管理系统,要求能注册登录,登录后对学生信息进行增删改查操作。(在网页实现)
不足之处还望各位大佬斧正,也欢迎大家与我交流,废话不多说,上代码
运行效果
代码
数据库使用的依然是MF
登陆界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Feilong_index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<font face = "宋体" size = "6" color = "#000">欢迎进入学生管理系统</font><hr>
<table width = "200" border ="1" bordercolor = "#00F">
<tr>
<td><input type = "button" value = "登 陆" onclick = "window.open('login.jsp')"></td>
<td><input type = "button" value = "注 册" onclick = "window.open('register.jsp')"></td>
</tr>
</table>
</center>
</body>
</html>
checkRegister
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Feilong_chechRegister.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<h1>完成</h1>
<h2><a href="index.jsp">返回</a></h2>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver");
//sqlserver
//Class.forName("com.microsoft.sqlserver,jdbc.SQlServer");
//2.获取连接
//url 给出主机名 端口号 数据库名
String user = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
String pwd = request.getParameter("password");
String Id = request.getParameter("id");
String em = request.getParameter("email");
String bth = request.getParameter("birthday");
String url = "jdbc:mysql://localhost:3306/shop?useSSL=false";
//用户名
String username = "root";
//密码
String password = "root";
Connection conn = DriverManager.getConnection(url, username, password);
//3.获取statement对象
Statement st = conn.createStatement();
//4.执行sql语句,获取ResultSet
String sql = "insert into user values('"+Id+"','"+user+"','"+pwd+"','"+em+"','"+bth+"')";
int result = st.executeUpdate(sql);
if(result > 0){
System.out.println("注册成功");
}else{
System.out.println("注册失败");
}
//5.关闭
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
%>
</body>
</html>
login
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Feilong_register.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script>
function addCheck(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
var id = document.getElementById("id").value;
if(id==""){
alert("id不能为空!");
document.getElementById("id").focus();
return false;
}
if(username==""){
alert("用户名不能为空!");
document.getElementById("username").focus();
return false;
}
if(password==""){
alert("密码不能为空!");
document.getElementById("password").focus();
return false;
}
}
function validate(){
var flag = addCheck();
if(flag == false)
return false;
return true;
}
</script>
<body>
<center>
<font face="楷体" size="6" color="#000">登录</font>
<form action = "checkRegister.jsp" method = "post" onsubmit = "return validate()">
<table width="300" height = "180" border="5" bordercolor="#A0A0A0">
<tr>
<th>ID:</th>
<td><input type="text" name="id" value="输入16个字符以内" maxlength = "16" onfocus = "if(this.value == '输入16个字符以内') this.value =''"></td>
</tr>
<tr>
<th>用户名:</th>
<td><input type="text" name="username" value="输入16个字符以内" maxlength = "16" onfocus = "if(this.value == '输入16个字符以内') this.value =''"></td>
</tr>
<tr>
<th>输入密码:</th>
<td><input type="text" name="password" value="输入20个字符以内" maxlength = "20" onfocus = "if(this.value == '输入20个字符以内') this.value =''"></td>
</tr>
<tr>
<td colspan = "2" align = "center">
<input type="submit" value="登录">
<input type="reset" value="重 置">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
register
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Feilong_register.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script>
function addCheck(){
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
var newword=document.getElementById("newword").value;
var id = document.getElementById("id").value;
var email = document.getElementById("email").value;
var birthday = document.getElementById("birthday").value
if(birthday==""){
alert("生日不能为空!");
document.getElementById("birthday").focus();
return false;
}
if(email==""){
alert("邮件不能为空!");
document.getElementById("email").focus();
return false;
}
if(id==""){
alert("id不能为空!");
document.getElementById("id").focus();
return false;
}
if(username==""){
alert("用户名不能为空!");
document.getElementById("username").focus();
return false;
}
if(password==""){
alert("密码不能为空!");
document.getElementById("password").focus();
return false;
}
if(password != newword){
alert("两次输入密码不相同!");
document.getElementById("newword").focus();
return false;
}
}
function validate(){
var flag = addCheck();
if(flag == false)
return false;
return true;
}
</script>
<body>
<center>
<font face="楷体" size="6" color="#000">注册界面</font>
<form action = "checkRegister.jsp" method = "post" onsubmit = "return validate()">
<table width="300" height = "180" border="5" bordercolor="#A0A0A0">
<tr>
<th>ID:</th>
<td><input type="text" name="id" value="输入16个字符以内" maxlength = "16" onfocus = "if(this.value == '输入16个字符以内') this.value =''"></td>
</tr>
<tr>
<th>用户名:</th>
<td><input type="text" name="username" value="输入16个字符以内" maxlength = "16" onfocus = "if(this.value == '输入16个字符以内') this.value =''"></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="text" name="email" value="输入16个字符以内" maxlength = "16" onfocus = "if(this.value == '输入16个字符以内') this.value =''"></td>
</tr>
<tr>
<th>Birthday:</th>
<td><input type="text" name="birthday" value="输入16个字符以内" maxlength = "16" onfocus = "if(this.value == '输入16个字符以内') this.value =''"></td>
</tr>
<tr>
<th>输入密码:</th>
<td><input type="text" name="password" value="输入20个字符以内" maxlength = "20" onfocus = "if(this.value == '输入20个字符以内') this.value =''"></td>
</tr>
<tr>
<th>确认密码:</th>
<td><input type="text" name="newword" value="重新输入密码" maxlength = "20" onfocus = "if(this.value == '重新输入密码') this.value =''"></td>
</tr>
<tr>
<td colspan = "2" align = "center">
<input type="submit" value="注 册">
<input type="reset" value="重 置">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
增
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,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>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<h1>插入数据</h1>
<h2><a href="index.jsp">返回</a></h2>
<%
try {
//1
Class.forName("com.mysql.jdbc.Driver");
//2
String url = "jdbc:mysql://localhost:3306/shop?useSSL=false";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url,user,password);
//3
Statement st = conn.createStatement();
//4
String name = new String((request.getParameter("name")).getBytes("ISO-8859-1"),"UTF-8");
String pass = new String((request.getParameter("password")).getBytes("ISO-8859-1"),"UTF-8");
String email = new String((request.getParameter("email")).getBytes("ISO-8859-1"),"UTF-8");
String birthday = new String((request.getParameter("birthday")).getBytes("ISO-8859-1"),"UTF-8");
//String sql = "insert into users(name,password,email,birthday)"
// + " values('赵柳','666666','[email protected]','2001-3-5')";
String sql = "insert into users(name,password,email,birthday)"
+ " values('" + name + "','" + pass + "','"
+ email + "','" + birthday + "')";
st.executeUpdate(sql);
//5
//6
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
%>
</body>
</html>
删
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,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>
<h1>删除数据</h1>
<h2><a href="index.jsp">返回</a></h2>
<%
try {
//1、加载数据库驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2、获取数据库连接
String url = "jdbc:mysql://localhost:3306/shop?useSSL=false";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);
//3、创建Statement执行sql语句
Statement st = conn.createStatement();
//4、执行sql
String sql = "delete from users where id=8";
int result = st.executeUpdate(sql);
if (result == 0) {
System.out.println("删除记录失败");
} else {
System.out.println("删除记录成功");
}
// System.out.println(result);
//5
//6、关闭资源
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
%>
</body>
</html>
改
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,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>
<h1>修改数据</h1>
<h2><a href="index.jsp">返回</a></h2>
<%
try {
//1、加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2、获取连接
String url = "jdbc:mysql://localhost:3306/shop?useSSL=false";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);
//3、创建Statement
Statement st = conn.createStatement();
//4、执行sql
String sql = "update users set birthday='2222-1-11' where name='赵六'";
int result = st.executeUpdate(sql);
if (result > 0) {
System.out.println("修改记录操作成功");
} else {
System.out.println("修改记录失败");
}
//5
//6
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
%>
</body>
</html>
查
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.io.*,java.util.*,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>
<h1>用户信息列表</h1>
<h2><a href="insert.jsp">添加</a></h2>
<h2><a href="index.jsp">返回</a></h2>
<%
try {
//1、加载数据库驱动
//mysql
Class.forName("com.mysql.jdbc.Driver");
//sqlserver
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2、获取连接
//url给出主机名、端口号和数据库的名
String url = "jdbc:mysql://localhost:3306/shop?useSSL=false";
//用户名
String user = "root";
//密码
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);//单例模式连接数据库
//3、获取statement对象
Statement st = conn.createStatement();
//4、执行sql语句,获取ResultSet
String sql = "select * from users";
ResultSet rs = st.executeQuery(sql);
//5、操作结果集
// rs.next();//记录指针指向第一条记录
// System.out.println(rs.getString("name"));
// System.out.println(rs.getString("password"));
// rs.next();//记录指针指向下一条记录
// System.out.println(rs.getString("name"));
// while (rs.next()) {
// out.print(rs.getString("id") + " " + rs.getString(2) + " " + rs.getString(3)
// + rs.getString("email") + rs.getString("birthday") + "<br />");
// }
//将数据显示在表格中
%>
<table border=1>
<tr>
<th>学号</th>
<th>姓名</th>
<th>密码</th>
<th>电子邮箱</th>
<th>生日</th>
<th>操作</th>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td>
<%
out.print(rs.getString("id"));
%>
</td>
<td>
<%=rs.getString(2)%>
</td>
<td>
<%=rs.getString(3) %>
</td>
<td>
<%=rs.getString("email")%>
</td>
<td>
<%=rs.getString("birthday")
%>
</td>
<td>
<a href='update.jsp?id=<%=rs.getString("id")%>'>编辑</a>
</td>
<td>
<a href='delete.jsp?id=<%=rs.getString("id")%>'>删除</a>
</td>
</tr>
<%
}
%>
</table>
<%
//6、关闭资源
rs.close();
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
%>
</body>
</html>