学生选课管理系统javaweb,sqlserver代码实现
时间: 2023-10-10 10:05:02 浏览: 125
以下是一个简单的学生选课管理系统的JavaWeb代码,使用SQL Server作为数据库。本代码实现了学生登录、选课、查看选课情况等功能。请注意该代码仅供参考。
1.数据库设计
学生表(Student)
- ID(学生ID)
- Name(学生姓名)
- Password(学生密码)
课程表(Course)
- ID(课程ID)
- Name(课程名称)
- Teacher(任课教师)
- Credit(学分)
选课表(Selection)
- ID(选课ID)
- StudentID(学生ID)
- CourseID(课程ID)
2.登录页面(index.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生选课管理系统</title>
</head>
<body>
<h1>学生选课管理系统</h1>
<form action="login" method="post">
<label for="id">学号:</label>
<input type="text" id="id" name="id"><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
3.登录处理(LoginServlet.java)
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String password = request.getParameter("password");
String message = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName=StudentCourse";
String user = "sa";
String pass = "123456";
Connection con = DriverManager.getConnection(url, user, pass);
String sql = "SELECT * FROM Student WHERE ID=? AND Password=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
HttpSession session = request.getSession(true);
session.setAttribute("id", id);
response.sendRedirect("course");
return;
} else {
message = "学号或密码错误";
}
rs.close();
pstmt.close();
con.close();
} catch (ClassNotFoundException e) {
message = "数据库驱动程序未找到";
e.printStackTrace();
} catch (SQLException e) {
message = "数据库操作异常";
e.printStackTrace();
}
request.setAttribute("message", message);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
4.选课页面(course.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生选课管理系统</title>
</head>
<body>
<h1>学生选课管理系统</h1>
<h2>欢迎,<%=session.getAttribute("id")%></h2>
<h3>当前选课情况:</h3>
<table>
<tr>
<th>课程编号</th>
<th>课程名称</th>
<th>任课教师</th>
<th>学分</th>
<th>操作</th>
</tr>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%
String id = (String)session.getAttribute("id");
String message = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName=StudentCourse";
String user = "sa";
String pass = "123456";
Connection con = DriverManager.getConnection(url, user, pass);
String sql1 = "SELECT * FROM Selection WHERE StudentID=?";
PreparedStatement pstmt1 = con.prepareStatement(sql1);
pstmt1.setString(1, id);
ResultSet rs1 = pstmt1.executeQuery();
List<String> courses = new ArrayList<String>();
while (rs1.next()) {
courses.add(rs1.getString("CourseID"));
}
rs1.close();
pstmt1.close();
String sql2 = "SELECT * FROM Course";
PreparedStatement pstmt2 = con.prepareStatement(sql2);
ResultSet rs2 = pstmt2.executeQuery();
while (rs2.next()) {
String courseID = rs2.getString("ID");
String courseName = rs2.getString("Name");
String teacher = rs2.getString("Teacher");
String credit = rs2.getString("Credit");
String operation = "";
if (courses.contains(courseID)) {
operation = "<a href='drop?id=" + courseID + "'>退选</a>";
} else {
operation = "<a href='choose?id=" + courseID + "'>选课</a>";
}
out.print("<tr><td>" + courseID + "</td><td>" + courseName + "</td><td>" + teacher + "</td><td>" + credit + "</td><td>" + operation + "</td></tr>");
}
rs2.close();
pstmt2.close();
con.close();
} catch (ClassNotFoundException e) {
message = "数据库驱动程序未找到";
e.printStackTrace();
} catch (SQLException e) {
message = "数据库操作异常";
e.printStackTrace();
}
%>
</table>
<p><%=message%></p>
</body>
</html>
5.选课处理(ChooseServlet.java)
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/choose")
public class ChooseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
String id = (String)session.getAttribute("id");
String courseID = request.getParameter("id");
String message = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName=StudentCourse";
String user = "sa";
String pass = "123456";
Connection con = DriverManager.getConnection(url, user, pass);
String sql = "INSERT INTO Selection(StudentID, CourseID) VALUES(?, ?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, courseID);
pstmt.executeUpdate();
pstmt.close();
con.close();
} catch (ClassNotFoundException e) {
message = "数据库驱动程序未找到";
e.printStackTrace();
} catch (SQLException e) {
message = "数据库操作异常";
e.printStackTrace();
}
request.setAttribute("message", message);
request.getRequestDispatcher("course.jsp").forward(request, response);
}
}
6.退选处理(DropServlet.java)
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/drop")
public class DropServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
String id = (String)session.getAttribute("id");
String courseID = request.getParameter("id");
String message = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName=StudentCourse";
String user = "sa";
String pass = "123456";
Connection con = DriverManager.getConnection(url, user, pass);
String sql = "DELETE FROM Selection WHERE StudentID=? AND CourseID=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, courseID);
pstmt.executeUpdate();
pstmt.close();
con.close();
} catch (ClassNotFoundException e) {
message = "数据库驱动程序未找到";
e.printStackTrace();
} catch (SQLException e) {
message = "数据库操作异常";
e.printStackTrace();
}
request.setAttribute("message", message);
request.getRequestDispatcher("course.jsp").forward(request, response);
}
}
注意:本代码仅供参考,请不要直接用于实际应用中,需要根据实际情况进行修改和完善。
阅读全文