学生信息管理:包括添加、修改、删除学生信息、查询学生信息等功能,实现学生信息的录入和管理。用java语言编写
时间: 2023-12-28 13:03:24 浏览: 49
好的,这个功能比较复杂,需要设计数据库、界面和后端代码。以下是一个简单的Java代码示例,可以作为参考:
```java
import java.sql.*;
import java.util.Scanner;
public class StudentInfoSystem {
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/student_db";
private static final String JDBC_USER = "root";
private static final String JDBC_PASSWORD = "password";
private static final String TABLE_NAME = "students";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
// 显示菜单
System.out.println("1. 添加学生信息");
System.out.println("2. 修改学生信息");
System.out.println("3. 删除学生信息");
System.out.println("4. 查询学生信息");
System.out.println("5. 退出");
System.out.print("请选择操作:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudentInfo();
break;
case 2:
updateStudentInfo();
break;
case 3:
deleteStudentInfo();
break;
case 4:
queryStudentInfo();
break;
case 5:
System.out.println("退出程序");
System.exit(0);
default:
System.out.println("无效操作,请重新选择");
break;
}
}
}
private static void addStudentInfo() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入学生性别(0:女,1:男):");
int gender = scanner.nextInt();
Connection conn = null;
Statement stmt = null;
try {
// 连接数据库
conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
stmt = conn.createStatement();
// 执行 SQL 语句
String sql = "INSERT INTO " + TABLE_NAME + " (name, age, gender) VALUES ('" + name + "', " + age + ", " + gender + ")";
int rows = stmt.executeUpdate(sql);
System.out.println(rows + " 行记录受影响");
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接和语句
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void updateStudentInfo() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的学生ID:");
int id = scanner.nextInt();
System.out.println("请输入学生姓名:");
String name = scanner.nextLine();
System.out.println("请输入学生年龄:");
int age = scanner.nextInt();
System.out.println("请输入学生性别(0:女,1:男):");
int gender = scanner.nextInt();
Connection conn = null;
Statement stmt = null;
try {
// 连接数据库
conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
stmt = conn.createStatement();
// 执行 SQL 语句
String sql = "UPDATE " + TABLE_NAME + " SET name='" + name + "', age=" + age + ", gender=" + gender + " WHERE id=" + id;
int rows = stmt.executeUpdate(sql);
System.out.println(rows + " 行记录受影响");
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接和语句
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void deleteStudentInfo() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的学生ID:");
int id = scanner.nextInt();
Connection conn = null;
Statement stmt = null;
try {
// 连接数据库
conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
stmt = conn.createStatement();
// 执行 SQL 语句
String sql = "DELETE FROM " + TABLE_NAME + " WHERE id=" + id;
int rows = stmt.executeUpdate(sql);
System.out.println(rows + " 行记录受影响");
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接和语句
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void queryStudentInfo() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的学生姓名(留空查询全部):");
String name = scanner.nextLine();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 连接数据库
conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
stmt = conn.createStatement();
// 执行 SQL 语句
String sql = "SELECT * FROM " + TABLE_NAME;
if (!name.isEmpty()) {
sql += " WHERE name='" + name + "'";
}
rs = stmt.executeQuery(sql);
// 输出结果
while (rs.next()) {
int id = rs.getInt("id");
String studentName = rs.getString("name");
int age = rs.getInt("age");
int gender = rs.getInt("gender");
System.out.println("ID:" + id + ",姓名:" + studentName + ",年龄:" + age + ",性别:" + (gender == 0 ? "女" : "男"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭连接、语句和结果集
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
这段代码实现了一个简单的学生信息管理系统,使用了Java语言和MySQL数据库。其中,JDBC_URL、JDBC_USER和JDBC_PASSWORD分别为数据库连接的URL、用户名和密码,TABLE_NAME为学生信息表的名称。您可以根据实际情况修改这些参数。
这段代码实现了四个功能:添加学生信息、修改学生信息、删除学生信息和查询学生信息。用户可以通过控制台菜单选择所需的操作,并输入相应的参数。程序会根据用户输入执行相应的SQL语句,从而实现对数据库的增删改查操作。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)