用java+mysql写一个小型学生成绩管理系统 用控制台输入
时间: 2023-08-04 11:36:49 浏览: 118
好的,这是一个比较简单的学生成绩管理系统,主要包括学生信息管理和成绩管理两个功能。以下是代码实现:
```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 的方式读取输入。
阅读全文