java+mysql数据库学生信息管理系统
时间: 2023-06-29 19:07:02 浏览: 105
一个基本的Java+MySQL数据库学生信息管理系统包含以下几个部分:
1. 登录界面:学生或管理员登录时输入用户名和密码进行身份验证。
2. 学生信息管理:管理员可以添加、修改、删除和查询学生信息。
3. 成绩管理:管理员可以录入学生成绩,并查询和打印学生成绩单。
4. 课程管理:管理员可以添加、修改、删除和查询课程信息。
5. 班级管理:管理员可以添加、修改、删除和查询班级信息。
6. 数据库连接:Java程序需要与MySQL数据库建立连接,以便执行数据库操作。
下面是一个简单的Java+MySQL数据库学生信息管理系统的代码示例,它包含了上述所有功能:
```java
import java.sql.*;
import java.util.Scanner;
public class StudentManagementSystem {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/students";
static final String USER = "root";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
Scanner scanner = new Scanner(System.in);
int choice = 0;
do {
System.out.println("\n\nStudent Management System\n");
System.out.println("1. Add new student");
System.out.println("2. Update student information");
System.out.println("3. Delete student");
System.out.println("4. View all students");
System.out.println("5. Add new course");
System.out.println("6. Update course information");
System.out.println("7. Delete course");
System.out.println("8. View all courses");
System.out.println("9. Add new class");
System.out.println("10. Update class information");
System.out.println("11. Delete class");
System.out.println("12. View all classes");
System.out.println("13. Add student to class");
System.out.println("14. View students in class");
System.out.println("15. Add student grade");
System.out.println("16. View student grade");
System.out.println("0. Exit");
choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent(conn, scanner);
break;
case 2:
updateStudent(conn, scanner);
break;
case 3:
deleteStudent(conn, scanner);
break;
case 4:
viewAllStudents(stmt);
break;
case 5:
addCourse(conn, scanner);
break;
case 6:
updateCourse(conn, scanner);
break;
case 7:
deleteCourse(conn, scanner);
break;
case 8:
viewAllCourses(stmt);
break;
case 9:
addClass(conn, scanner);
break;
case 10:
updateClass(conn, scanner);
break;
case 11:
deleteClass(conn, scanner);
break;
case 12:
viewAllClasses(stmt);
break;
case 13:
addStudentToClass(conn, scanner);
break;
case 14:
viewStudentsInClass(conn, scanner);
break;
case 15:
addStudentGrade(conn, scanner);
break;
case 16:
viewStudentGrade(conn, scanner);
break;
case 0:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
} while (choice != 0);
stmt.close();
conn.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
private static void addStudent(Connection conn, Scanner scanner) throws SQLException {
System.out.println("Enter student ID:");
int id = scanner.nextInt();
scanner.nextLine(); // consume newline character
System.out.println("Enter student name:");
String name = scanner.nextLine();
System.out.println("Enter student email:");
String email = scanner.nextLine();
String sql = "INSERT INTO students(id, name, email) VALUES (" + id + ", '" + name + "', '" + email + "')";
int rowsAffected = conn.createStatement().executeUpdate(sql);
System.out.println(rowsAffected + " row(s) affected.");
}
private static void updateStudent(Connection conn, Scanner scanner) throws SQLException {
System.out.println("Enter student ID:");
int id = scanner.nextInt();
scanner.nextLine(); // consume newline character
System.out.println("Enter new student name:");
String name = scanner.nextLine();
System.out.println("Enter new student email:");
String email = scanner.nextLine();
String sql = "UPDATE students SET name='" + name + "', email='" + email + "' WHERE id=" + id;
int rowsAffected = conn.createStatement().executeUpdate(sql);
System.out.println(rowsAffected + " row(s) affected.");
}
private static void deleteStudent(Connection conn, Scanner scanner) throws SQLException {
System.out.println("Enter student ID:");
int id = scanner.nextInt();
String sql = "DELETE FROM students WHERE id=" + id;
int rowsAffected = conn.createStatement().executeUpdate(sql);
System.out.println(rowsAffected + " row(s) affected.");
}
private static void viewAllStudents(Statement stmt) throws SQLException {
String sql = "SELECT * FROM students";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String email = rs.getString("email");
System.out.println(id + "\t" + name + "\t" + email);
}
rs.close();
}
private static void addCourse(Connection conn, Scanner scanner) throws SQLException {
System.out.println("Enter course ID:");
int id = scanner.nextInt();
scanner.nextLine(); // consume newline character
System.out.println("Enter course name:");
String name = scanner.nextLine();
String sql = "INSERT INTO courses(id, name) VALUES (" + id + ", '" + name + "')";
int rowsAffected = conn.createStatement().executeUpdate(sql);
System.out.println(rowsAffected + " row(s) affected.");
}
private static void updateCourse(Connection conn, Scanner scanner) throws SQLException {
System.out.println("Enter course ID:");
int id = scanner.nextInt();
scanner.nextLine(); // consume newline character
System.out.println("Enter new course name:");
String name = scanner.nextLine();
String sql = "UPDATE courses SET name='" + name + "' WHERE id=" + id;
int rowsAffected = conn.createStatement().executeUpdate(sql);
System.out.println(rowsAffected + " row(s) affected.");
}
private static void deleteCourse(Connection conn, Scanner scanner) throws SQLException {
System.out.println("Enter course ID:");
int id = scanner.nextInt();
String sql = "DELETE FROM courses WHERE id=" + id;
int rowsAffected = conn.createStatement().executeUpdate(sql);
System.out.println(rowsAffected + " row(s) affected.");
}
private static void viewAllCourses(Statement stmt) throws SQLException {
String sql = "SELECT * FROM courses";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(id + "\t" + name);
}
rs.close();
}
private static void addClass(Connection conn, Scanner scanner) throws SQLException {
System.out.println("Enter class ID:");
int id = scanner.nextInt();
scanner.nextLine(); // consume newline character
System.out.println("Enter class name:");
String name = scanner.nextLine();
System.out.println("Enter course ID for class:");
int courseId = scanner.nextInt();
String sql = "INSERT INTO classes(id, name, course_id) VALUES (" + id + ", '" + name + "', " + courseId + ")";
int rowsAffected = conn.createStatement().executeUpdate(sql);
System.out.println(rowsAffected + " row(s) affected.");
}
private static void updateClass(Connection conn, Scanner scanner) throws SQLException {
System.out.println("Enter class ID:");
int id = scanner.nextInt();
scanner.nextLine(); // consume newline character
System.out.println("Enter new class name:");
String name = scanner.nextLine();
System.out.println("Enter new course ID for class:");
int courseId = scanner.nextInt();
String sql = "UPDATE classes SET name='" + name + "', course_id=" + courseId + " WHERE id=" + id;
int rowsAffected = conn.createStatement().executeUpdate(sql);
System.out.println(rowsAffected + " row(s) affected.");
}
private static void deleteClass(Connection conn, Scanner scanner) throws SQLException {
System.out.println("Enter class ID:");
int id = scanner.nextInt();
String sql = "DELETE FROM classes WHERE id=" + id;
int rowsAffected = conn.createStatement().executeUpdate(sql);
System.out.println(rowsAffected + " row(s) affected.");
}
private static void viewAllClasses(Statement stmt) throws SQLException {
String sql = "SELECT * FROM classes";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int courseId = rs.getInt("course_id");
System.out.println(id + "\t" + name + "\t" + courseId);
}
rs.close();
}
private static void addStudentToClass(Connection conn, Scanner scanner) throws SQLException {
System.out.println("Enter student ID:");
int studentId = scanner.nextInt();
System.out.println("Enter class ID:");
int classId = scanner.nextInt();
String sql = "INSERT INTO class_roster(student_id, class_id) VALUES (" + studentId + ", " + classId + ")";
int rowsAffected = conn.createStatement().executeUpdate(sql);
System.out.println(rowsAffected + " row(s) affected.");
}
private static void viewStudentsInClass(Connection conn, Scanner scanner) throws SQLException {
System.out.println("Enter class ID:");
int classId = scanner.nextInt();
String sql = "SELECT s.id, s.name, s.email FROM students s JOIN class_roster cr ON s.id=cr.student_id WHERE cr.class_id=" + classId;
ResultSet rs = conn.createStatement().executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String email = rs.getString("email");
System.out.println(id + "\t" + name + "\t" + email);
}
rs.close();
}
private static void addStudentGrade(Connection conn, Scanner scanner) throws SQLException {
System.out.println("Enter student ID:");
int studentId = scanner.nextInt();
System.out.println("Enter class ID:");
int classId = scanner.nextInt();
System.out.println("Enter grade:");
double grade = scanner.nextDouble();
String sql = "INSERT INTO grades(student_id, class_id, grade) VALUES (" + studentId + ", " + classId + ", " + grade + ")";
int rowsAffected = conn.createStatement().executeUpdate(sql);
System.out.println(rowsAffected + " row(s) affected.");
}
private static void viewStudentGrade(Connection conn, Scanner scanner) throws SQLException {
System.out.println("Enter student ID:");
int studentId = scanner.nextInt();
String sql = "SELECT c.name, g.grade FROM courses c JOIN classes cl ON c.id=cl.course_id JOIN grades g ON cl.id=g.class_id WHERE g.student_id=" + studentId;
ResultSet rs = conn.createStatement().executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
double grade = rs.getDouble("grade");
System.out.println(name + "\t" + grade);
}
rs.close();
}
}
```
注意:这里使用了纯JDBC代码,没有使用任何ORM框架。对于真正的应用程序,最好使用ORM框架来简化数据库操作。
阅读全文