java+mysql小型学生成绩管理系统 控制台运行
时间: 2023-09-26 07:14:08 浏览: 153
好的,您需要一个控制台运行的Java+MySQL小型学生成绩管理系统。这个系统需要实现以下功能:
1. 学生信息的添加、删除、修改和查询;
2. 学生成绩的添加、删除、修改和查询;
3. 学生信息和成绩的统计和排序;
4. 数据库的备份和恢复。
首先,您需要安装Java和MySQL,并且创建一个数据库和表来存储学生信息和成绩。
然后,您可以使用Java的JDBC API来连接MySQL数据库,并且实现以上功能。您可以使用控制台来进行交互,接收用户输入,并且输出查询结果。您可以使用Java的控制台输入输出API来实现这些功能。
具体实现细节可以参考Java和MySQL的相关文档和教程。希望能够帮助您实现这个小型学生成绩管理系统。
相关问题
java+mysql小型学生成绩管理系统 控制台运行要求代码
好的,以下是一个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();
}
}
```
以上代码示例实现了添加、删除、修改和查询学生信息和成绩,以及统计和排序学生成绩,和备份和恢复数据库的功能。您可以根据您的具体需求进行修改和扩展。
用java+mysql写一个小型学生成绩管理系统 用控制台输入
好的,这是一个比较简单的学生成绩管理系统,主要包括学生信息管理和成绩管理两个功能。以下是代码实现:
```java
import java.sql.*;
public class StudentGradeManagementSystem {
// 数据库连接信息
private static final String url = "jdbc:mysql://localhost:3306/student";
private static final String username = "root";
private static final String password = "123456";
// 数据库连接对象
private Connection conn = null;
// SQL执行对象
private Statement stmt = null;
// 结果集对象
private ResultSet rs = null;
// 构造方法,连接数据库
public StudentGradeManagementSystem() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
// 查询学生信息
public void queryStudent() {
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM student");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String gender = rs.getString("gender");
int age = rs.getInt("age");
System.out.println(id + "\t" + name + "\t" + gender + "\t" + age);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
}
// 添加学生信息
public void addStudent(String name, String gender, int age) {
try {
stmt = conn.createStatement();
String sql = "INSERT INTO student(name, gender, age) VALUES('" + name + "', '" + gender + "', " + age + ")";
int count = stmt.executeUpdate(sql);
if (count > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
}
// 修改学生信息
public void updateStudent(int id, String name, String gender, int age) {
try {
stmt = conn.createStatement();
String sql = "UPDATE student SET name='" + name + "', gender='" + gender + "', age=" + age + " WHERE id=" + id;
int count = stmt.executeUpdate(sql);
if (count > 0) {
System.out.println("修改成功!");
} else {
System.out.println("修改失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
}
// 删除学生信息
public void deleteStudent(int id) {
try {
stmt = conn.createStatement();
String sql = "DELETE FROM student WHERE id=" + id;
int count = stmt.executeUpdate(sql);
if (count > 0) {
System.out.println("删除成功!");
} else {
System.out.println("删除失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
}
// 查询学生成绩
public void queryGrade(int id) {
try {
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM grade WHERE student_id=" + id);
while (rs.next()) {
int gradeId = rs.getInt("id");
int studentId = rs.getInt("student_id");
int grade = rs.getInt("grade");
String subject = rs.getString("subject");
System.out.println(gradeId + "\t" + studentId + "\t" + subject + "\t" + grade);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
}
// 添加学生成绩
public void addGrade(int id, String subject, int grade) {
try {
stmt = conn.createStatement();
String sql = "INSERT INTO grade(student_id, subject, grade) VALUES(" + id + ", '" + subject + "', " + grade + ")";
int count = stmt.executeUpdate(sql);
if (count > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
}
// 修改学生成绩
public void updateGrade(int gradeId, int grade) {
try {
stmt = conn.createStatement();
String sql = "UPDATE grade SET grade=" + grade + " WHERE id=" + gradeId;
int count = stmt.executeUpdate(sql);
if (count > 0) {
System.out.println("修改成功!");
} else {
System.out.println("修改失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
}
// 删除学生成绩
public void deleteGrade(int gradeId) {
try {
stmt = conn.createStatement();
String sql = "DELETE FROM grade WHERE id=" + gradeId;
int count = stmt.executeUpdate(sql);
if (count > 0) {
System.out.println("删除成功!");
} else {
System.out.println("删除失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
}
// 关闭数据库连接
private void close() {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
StudentGradeManagementSystem system = new StudentGradeManagementSystem();
int choice = 0;
do {
System.out.println("请选择操作:");
System.out.println("1.查询学生信息");
System.out.println("2.添加学生信息");
System.out.println("3.修改学生信息");
System.out.println("4.删除学生信息");
System.out.println("5.查询学生成绩");
System.out.println("6.添加学生成绩");
System.out.println("7.修改学生成绩");
System.out.println("8.删除学生成绩");
System.out.println("9.退出");
System.out.print("请选择:");
choice = Integer.parseInt(System.console().readLine());
switch (choice) {
case 1:
system.queryStudent();
break;
case 2:
System.out.print("请输入学生姓名:");
String name = System.console().readLine();
System.out.print("请输入学生性别:");
String gender = System.console().readLine();
System.out.print("请输入学生年龄:");
int age = Integer.parseInt(System.console().readLine());
system.addStudent(name, gender, age);
break;
case 3:
System.out.print("请输入要修改的学生ID:");
int id = Integer.parseInt(System.console().readLine());
System.out.print("请输入学生姓名:");
name = System.console().readLine();
System.out.print("请输入学生性别:");
gender = System.console().readLine();
System.out.print("请输入学生年龄:");
age = Integer.parseInt(System.console().readLine());
system.updateStudent(id, name, gender, age);
break;
case 4:
System.out.print("请输入要删除的学生ID:");
id = Integer.parseInt(System.console().readLine());
system.deleteStudent(id);
break;
case 5:
System.out.print("请输入要查询成绩的学生ID:");
id = Integer.parseInt(System.console().readLine());
system.queryGrade(id);
break;
case 6:
System.out.print("请输入学生ID:");
id = Integer.parseInt(System.console().readLine());
System.out.print("请输入科目:");
String subject = System.console().readLine();
System.out.print("请输入成绩:");
int grade = Integer.parseInt(System.console().readLine());
system.addGrade(id, subject, grade);
break;
case 7:
System.out.print("请输入要修改的成绩ID:");
int gradeId = Integer.parseInt(System.console().readLine());
System.out.print("请输入成绩:");
grade = Integer.parseInt(System.console().readLine());
system.updateGrade(gradeId, grade);
break;
case 8:
System.out.print("请输入要删除的成绩ID:");
gradeId = Integer.parseInt(System.console().readLine());
system.deleteGrade(gradeId);
break;
case 9:
System.out.println("谢谢使用!");
break;
default:
System.out.println("选择有误,请重新输入!");
break;
}
} while (choice != 9);
}
}
```
注意,这里使用的是 JDBC 连接数据库,需要先在本地安装 MySQL 并创建名为 student 的数据库,其中包含两个表 student 和 grade,具体表结构如下:
```sql
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`gender` varchar(10) NOT NULL,
`age` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4;
CREATE TABLE `grade` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_id` int(11) NOT NULL,
`subject` varchar(20) NOT NULL,
`grade` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_student_id` (`student_id`),
CONSTRAINT `fk_student_id` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4;
```
此外,为了可以在控制台输入,代码中使用了 System.console().readLine() 方法,如果是在 IDE 中运行需要改为 Scanner 的方式读取输入。
阅读全文