学生成绩查询系统(eclipse+SQLserver+tomact)
- 开发环境
- 系统实现
- 总结+参考
开发环境
比较懒。
如果要做这种类似的系统大概都知道这些软件吧。所以这个部分比较粗糙
- Java环境
下载安装eclipse和JDK,配置相应的Java环境
安装JDK的教程
安装eclipse的教程 - MySQL数据库
安装MySQL,配置相应的服务
MySQL安装及图形化的教程 - Tomact服务器
下载Tomact的压缩包,配置环境变量
安装Tomact的教程
系统实现
-
数据库
创建课程信息表、学生信息表、成绩基本信息表,其中课程信息表中的课程号是成绩信息表中课程号的外键,学生信息表中的学号是成绩信息表中学号的外键。 -
实现功能
登陆功能;课程信息管理;学生信息管理;查询成绩。 -
项目结构
-
部分代码
数据库连接:
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/studentmanagement?useUnicode=true&characterEncoding=UTF-8";//连接数据库
private static final String username = "root";
private static final String password = "123456";
private static Connection conn;
static {
try {
Class.forName(driver);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
if (conn == null) {
conn = DriverManager.getConnection(url, username, password);
return conn;
}
return conn;
}
}
登录:
public class LoginServlet extends HttpServlet{
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
HttpSession session = request.getSession();
String path = request.getContextPath();
String action = request.getParameter("action");
if ("login".equals(action)) {
String inumber = request.getParameter("inumber");
String password = request.getParameter("password");
String identity = request.getParameter("identity");
String[] isUserCookies = request.getParameterValues("isUseCookie");
if ("student".equals(identity)) {
// 如果登录的是学生
StudentDao studentDao = new StudentDao();
Student student = new Student();
try {
student.setSid(Integer.parseInt(inumber));
} catch (Exception ex) {
response.sendRedirect(path + "/failure.jsp");
return ;
}
student.setSpwd(password);
if (studentDao.isValid(student)) {
// 账号密码合法
session.setAttribute("student", student);
if (isUserCookies != null && isUserCookies.length > 0) {
saveCookie(inumber, password, response);
} else {
notSaveCookie(inumber, password, request, response);
}
response.sendRedirect(path + "/studentMain.jsp");
} else {
// 不合法
response.sendRedirect(path + "/failure.jsp");
}
} else if ("admin".equals(identity)) {
// 如果登录的是管理员
if ("001".equals(inumber) && "001".equals(password)) {
// 账号密码合法
session.setAttribute("admin", "管理员");
if (isUserCookies != null && isUserCookies.length > 0) {
saveCookie(inumber, password, response);
} else {
notSaveCookie(inumber, password, request, response);
}
response.sendRedirect(path + "/adminMain.jsp");
} else {
// 不合法
response.sendRedirect(path + "/failure.jsp");
}
} else {
response.sendRedirect(path + "/teacherMain.jsp");
}
} else if ("logout".equals(action)) {
// 退出登录
session.invalidate();
response.sendRedirect(path + "/index.jsp");
}
}
// 记住账号密码
public void saveCookie(String inumber, String password, HttpServletResponse response) {
Cookie inumberCookie = new Cookie("inumber", inumber);
Cookie passwordCookie = new Cookie("password", password);
// 设置Cookie存储路径 否则index中取不到……
inumberCookie.setPath("/");
passwordCookie.setPath("/");
inumberCookie.setMaxAge(864000); // 10 days
passwordCookie.setMaxAge(864000);
response.addCookie(inumberCookie);
response.addCookie(passwordCookie);
}
// 不记住账号密码
public void notSaveCookie(String inumber, String password,
HttpServletRequest request, HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
for (Cookie cookie: cookies) {
if (cookie.getName().equals("inumber") || cookie.getName().equals("password")) {
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
}
public void init() throws ServletException {
// Put your code here
}
}
学生登录的页面:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ page import="model.Student" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>学生登录</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">
-->
<style type="text/css">
* {
/* border:1px solid #F00; */
}
body {
background:#AAFFEE url(images/studentMain.jpg);
text-align:center;
}
#wel {
margin: 100px 400px 0 400px;
border:1px solid #000;
}
a {
font-size:1.5em;
background: #BBFFEE;
}
a:hover {background: #00BBFF;}
</style>
</head>
<body>
<div id="wel">
<h1>欢迎登录,<%=((Student)session.getAttribute("student")).getSname()%></h1>
<p><a href="servlet/StudentServlet?action=lookup">选课</a></p>
<p><a href="studentSelected.jsp">查看已选课程</a></p>
<p><a href="servlet/LoginServlet?action=logout">退出登录</a></p>
</div>
</body>
</html>
- 效果
总结+参考
部分功能存在一些问题,但是跳转是没有问题的。是在别人的代码基础之上自己调试出来的,由于时间关系没有好好完善页面、功能等。jsp感觉和html网页差不多,只是在里面加入了Java语言,不难上手。
Jsp+serlvet+mysql实现学生成绩管理系统
学生成绩查询系统(eclipse+SQLserver+tomact)
- 开发环境
- 系统实现
- 总结+参考
开发环境
比较懒。
如果要做这种类似的系统大概都知道这些软件吧。所以这个部分比较粗糙
- Java环境
下载安装eclipse和JDK,配置相应的Java环境
安装JDK的教程
安装eclipse的教程 - MySQL数据库
安装MySQL,配置相应的服务
MySQL安装及图形化的教程 - Tomact服务器
下载Tomact的压缩包,配置环境变量
安装Tomact的教程
系统实现
-
数据库
创建课程信息表、学生信息表、成绩基本信息表,其中课程信息表中的课程号是成绩信息表中课程号的外键,学生信息表中的学号是成绩信息表中学号的外键。 -
实现功能
登陆功能;课程信息管理;学生信息管理;查询成绩。 -
项目结构
-
部分代码
数据库连接:
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/studentmanagement?useUnicode=true&characterEncoding=UTF-8";//连接数据库
private static final String username = "root";
private static final String password = "123456";
private static Connection conn;
static {
try {
Class.forName(driver);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
if (conn == null) {
conn = DriverManager.getConnection(url, username, password);
return conn;
}
return conn;
}
}
登录:
public class LoginServlet extends HttpServlet{
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
HttpSession session = request.getSession();
String path = request.getContextPath();
String action = request.getParameter("action");
if ("login".equals(action)) {
String inumber = request.getParameter("inumber");
String password = request.getParameter("password");
String identity = request.getParameter("identity");
String[] isUserCookies = request.getParameterValues("isUseCookie");
if ("student".equals(identity)) {
// 如果登录的是学生
StudentDao studentDao = new StudentDao();
Student student = new Student();
try {
student.setSid(Integer.parseInt(inumber));
} catch (Exception ex) {
response.sendRedirect(path + "/failure.jsp");
return ;
}
student.setSpwd(password);
if (studentDao.isValid(student)) {
// 账号密码合法
session.setAttribute("student", student);
if (isUserCookies != null && isUserCookies.length > 0) {
saveCookie(inumber, password, response);
} else {
notSaveCookie(inumber, password, request, response);
}
response.sendRedirect(path + "/studentMain.jsp");
} else {
// 不合法
response.sendRedirect(path + "/failure.jsp");
}
} else if ("admin".equals(identity)) {
// 如果登录的是管理员
if ("001".equals(inumber) && "001".equals(password)) {
// 账号密码合法
session.setAttribute("admin", "管理员");
if (isUserCookies != null && isUserCookies.length > 0) {
saveCookie(inumber, password, response);
} else {
notSaveCookie(inumber, password, request, response);
}
response.sendRedirect(path + "/adminMain.jsp");
} else {
// 不合法
response.sendRedirect(path + "/failure.jsp");
}
} else {
response.sendRedirect(path + "/teacherMain.jsp");
}
} else if ("logout".equals(action)) {
// 退出登录
session.invalidate();
response.sendRedirect(path + "/index.jsp");
}
}
// 记住账号密码
public void saveCookie(String inumber, String password, HttpServletResponse response) {
Cookie inumberCookie = new Cookie("inumber", inumber);
Cookie passwordCookie = new Cookie("password", password);
// 设置Cookie存储路径 否则index中取不到……
inumberCookie.setPath("/");
passwordCookie.setPath("/");
inumberCookie.setMaxAge(864000); // 10 days
passwordCookie.setMaxAge(864000);
response.addCookie(inumberCookie);
response.addCookie(passwordCookie);
}
// 不记住账号密码
public void notSaveCookie(String inumber, String password,
HttpServletRequest request, HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
for (Cookie cookie: cookies) {
if (cookie.getName().equals("inumber") || cookie.getName().equals("password")) {
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
}
public void init() throws ServletException {
// Put your code here
}
}
学生登录的页面:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ page import="model.Student" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>学生登录</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">
-->
<style type="text/css">
* {
/* border:1px solid #F00; */
}
body {
background:#AAFFEE url(images/studentMain.jpg);
text-align:center;
}
#wel {
margin: 100px 400px 0 400px;
border:1px solid #000;
}
a {
font-size:1.5em;
background: #BBFFEE;
}
a:hover {background: #00BBFF;}
</style>
</head>
<body>
<div id="wel">
<h1>欢迎登录,<%=((Student)session.getAttribute("student")).getSname()%></h1>
<p><a href="servlet/StudentServlet?action=lookup">选课</a></p>
<p><a href="studentSelected.jsp">查看已选课程</a></p>
<p><a href="servlet/LoginServlet?action=logout">退出登录</a></p>
</div>
</body>
</html>
- 效果
总结+参考
部分功能存在一些问题,但是跳转是没有问题的。是在别人的代码基础之上自己调试出来的,由于时间关系没有好好完善页面、功能等。jsp感觉和html网页差不多,只是在里面加入了Java语言,不难上手。
Jsp+serlvet+mysql实现学生成绩管理系统