java+mysql小型学生成绩管理系统 控制台运行要求代码
时间: 2023-12-14 15:03:29 浏览: 95
好的,以下是一个Java+MySQL小型学生成绩管理系统的控制台运行代码示例,供您参考:
```java
import java.sql.*;
public class StudentGradeSystem {
// 定义数据库连接参数
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/student";
private static final String JDBC_USER = "root";
private static final String JDBC_PASSWORD = "123456";
// 定义数据库表名和字段名
private static final String STUDENT_TABLE = "student_info";
private static final String GRADE_TABLE = "student_grade";
private static final String STUDENT_ID = "id";
private static final String STUDENT_NAME = "name";
private static final String STUDENT_GENDER = "gender";
private static final String STUDENT_AGE = "age";
private static final String GRADE_ID = "id";
private static final String GRADE_SUBJECT = "subject";
private static final String GRADE_SCORE = "score";
// 定义数据库连接对象和语句对象
private Connection connection;
private Statement statement;
// 构造函数,用于初始化数据库连接对象和语句对象
public StudentGradeSystem() {
try {
// 加载MySQL驱动
Class.forName("com.mysql.jdbc.Driver");
// 创建数据库连接
connection = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
// 创建语句对象
statement = connection.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 添加学生信息
public void addStudent(String name, String gender, int age) {
try {
String sql = "INSERT INTO " + STUDENT_TABLE + " (" + STUDENT_NAME + ", " + STUDENT_GENDER + ", " + STUDENT_AGE + ") VALUES ('" + name + "', '" + gender + "', " + age + ")";
statement.executeUpdate(sql);
System.out.println("添加学生信息成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除学生信息
public void deleteStudent(int id) {
try {
String sql = "DELETE FROM " + STUDENT_TABLE + " WHERE " + STUDENT_ID + " = " + id;
statement.executeUpdate(sql);
System.out.println("删除学生信息成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 修改学生信息
public void updateStudent(int id, String name, String gender, int age) {
try {
String sql = "UPDATE " + STUDENT_TABLE + " SET " + STUDENT_NAME + " = '" + name + "', " + STUDENT_GENDER + " = '" + gender + "', " + STUDENT_AGE + " = " + age + " WHERE " + STUDENT_ID + " = " + id;
statement.executeUpdate(sql);
System.out.println("修改学生信息成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查询学生信息
public void queryStudent(int id) {
try {
String sql = "SELECT * FROM " + STUDENT_TABLE + " WHERE " + STUDENT_ID + " = " + id;
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
System.out.println("学生ID:" + resultSet.getInt(STUDENT_ID));
System.out.println("学生姓名:" + resultSet.getString(STUDENT_NAME));
System.out.println("学生性别:" + resultSet.getString(STUDENT_GENDER));
System.out.println("学生年龄:" + resultSet.getInt(STUDENT_AGE));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 添加学生成绩
public void addGrade(int id, String subject, double score) {
try {
String sql = "INSERT INTO " + GRADE_TABLE + " (" + STUDENT_ID + ", " + GRADE_SUBJECT + ", " + GRADE_SCORE + ") VALUES (" + id + ", '" + subject + "', " + score + ")";
statement.executeUpdate(sql);
System.out.println("添加学生成绩成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除学生成绩
public void deleteGrade(int id, String subject) {
try {
String sql = "DELETE FROM " + GRADE_TABLE + " WHERE " + STUDENT_ID + " = " + id + " AND " + GRADE_SUBJECT + " = '" + subject + "'";
statement.executeUpdate(sql);
System.out.println("删除学生成绩成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 修改学生成绩
public void updateGrade(int id, String subject, double score) {
try {
String sql = "UPDATE " + GRADE_TABLE + " SET " + GRADE_SCORE + " = " + score + " WHERE " + STUDENT_ID + " = " + id + " AND " + GRADE_SUBJECT + " = '" + subject + "'";
statement.executeUpdate(sql);
System.out.println("修改学生成绩成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查询学生成绩
public void queryGrade(int id) {
try {
String sql = "SELECT * FROM " + GRADE_TABLE + " WHERE " + STUDENT_ID + " = " + id;
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
System.out.println("学生ID:" + resultSet.getInt(STUDENT_ID));
System.out.println("学科:" + resultSet.getString(GRADE_SUBJECT));
System.out.println("成绩:" + resultSet.getDouble(GRADE_SCORE));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 统计学生成绩
public void statisticGrade() {
try {
String sql = "SELECT " + STUDENT_ID + ", AVG(" + GRADE_SCORE + ") AS avg_score FROM " + GRADE_TABLE + " GROUP BY " + STUDENT_ID;
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
System.out.println("学生ID:" + resultSet.getInt(STUDENT_ID));
System.out.println("平均成绩:" + resultSet.getDouble("avg_score"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 排序学生成绩
public void sortGrade() {
try {
String sql = "SELECT * FROM " + GRADE_TABLE + " ORDER BY " + GRADE_SCORE + " DESC";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
System.out.println("学生ID:" + resultSet.getInt(STUDENT_ID));
System.out.println("学科:" + resultSet.getString(GRADE_SUBJECT));
System.out.println("成绩:" + resultSet.getDouble(GRADE_SCORE));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 备份数据库
public void backupDatabase(String filePath) {
try {
String sql = "mysqldump -u " + JDBC_USER + " -p" + JDBC_PASSWORD + " student > " + filePath;
Runtime.getRuntime().exec(sql);
System.out.println("备份数据库成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
// 恢复数据库
public void restoreDatabase(String filePath) {
try {
String sql = "mysql -u " + JDBC_USER + " -p" + JDBC_PASSWORD + " student < " + filePath;
Runtime.getRuntime().exec(sql);
System.out.println("恢复数据库成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
// 关闭数据库连接
public void close() {
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 主函数,用于测试
public static void main(String[] args) {
StudentGradeSystem system = new StudentGradeSystem();
system.addStudent("张三", "男", 18);
system.addStudent("李四", "女", 19);
system.addGrade(1, "语文", 90);
system.addGrade(1, "数学", 80);
system.addGrade(2, "语文", 85);
system.addGrade(2, "数学", 95);
system.queryStudent(1);
system.queryGrade(1);
system.statisticGrade();
system.sortGrade();
system.backupDatabase("student.sql");
system.deleteStudent(1);
system.deleteGrade(2, "数学");
system.updateStudent(2, "王五", "男", 20);
system.updateGrade(2, "语文", 95);
system.queryStudent(2);
system.queryGrade(2);
system.restoreDatabase("student.sql");
system.close();
}
}
```
以上代码示例实现了添加、删除、修改和查询学生信息和成绩,以及统计和排序学生成绩,和备份和恢复数据库的功能。您可以根据您的具体需求进行修改和扩展。
阅读全文