JSP实现学生管理系统
时间: 2023-12-17 08:28:41 浏览: 147
JSP可以用于实现学生信息管理系统。下面是一个简单的JSP实现学生管理系统的例子:
1. 首先,创建一个JSP页面,用于显示学生信息列表。在JSP页面中,使用JavaBean来获取学生信息,并使用HTML和CSS来格式化和显示学生信息列表。
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@ page import="com.example.Student" %>
<%@ page import="com.example.StudentDAO" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理系统</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h1>学生信息列表</h1>
<table>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
<%
StudentDAO dao = new StudentDAO();
List<Student> students = dao.getAllStudents();
for (Student student : students) {
%>
<tr>
<td><%= student.getId() %></td>
<td><%= student.getName() %></td>
<td><%= student.getAge() %></td>
<td><%= student.getGender() %></td>
</tr>
<% } %>
</table>
</body>
</html>
```
2. 创建一个JavaBean,用于获取学生信息。在JavaBean中,使用JDBC来连接数据库,并使用SQL语句来查询学生信息。
```java
package com.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDAO {
private static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=student";
private static final String USERNAME = "sa";
private static final String PASSWORD = "123456";
public List<Student> getAllStudents() {
List<Student> students = new ArrayList<>();
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "SELECT * FROM student";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setGender(rs.getString("gender"));
students.add(student);
}
rs.close();
pstmt.close();
conn.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return students;
}
}
```
3. 创建一个Java类,用于表示学生信息。
```java
package com.example;
public class Student {
private int id;
private String name;
private int age;
private String gender;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
```
阅读全文