java编写学生管理演示系统的程序初稿,要连接mysql数据库,并且包括学生id、姓名、性别、年龄、电话号码、家庭住址等。代码中要实现学生信息添加、删除、修改等功能。代码中实现在键盘上输入1、2、3等数字,实现学生信息的增加、删除、修改等操作。并将代码整合到一起。
时间: 2023-12-20 15:07:53 浏览: 173
以下是一个初步的Java程序,用于连接MySQL数据库,实现学生信息的添加、删除和修改。
首先需要创建一个名为“Student”的Java类,包含学生ID、姓名、性别、年龄、电话号码和家庭住址等属性:
```
public class Student {
private int id;
private String name;
private String gender;
private int age;
private String phone;
private String address;
public Student(int id, String name, String gender, int age, String phone, String address) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.phone = phone;
this.address = address;
}
// Getters and setters for each attribute
}
```
接下来需要创建一个名为“StudentManagement”的Java类,用于实现学生信息的增加、删除和修改。该类需要连接MySQL数据库,因此需要使用Java JDBC API。以下是一个基本的示例代码:
```
import java.sql.*;
public class StudentManagement {
private Connection conn;
public StudentManagement() throws SQLException {
// Connect to the database
String url = "jdbc:mysql://localhost:3306/students";
String user = "root";
String password = "password";
conn = DriverManager.getConnection(url, user, password);
}
public void addStudent(Student student) throws SQLException {
// Insert a new student into the database
String sql = "INSERT INTO student (id, name, gender, age, phone, address) VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, student.getId());
stmt.setString(2, student.getName());
stmt.setString(3, student.getGender());
stmt.setInt(4, student.getAge());
stmt.setString(5, student.getPhone());
stmt.setString(6, student.getAddress());
stmt.executeUpdate();
}
public void deleteStudent(int id) throws SQLException {
// Delete a student from the database
String sql = "DELETE FROM student WHERE id = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.executeUpdate();
}
public void updateStudent(Student student) throws SQLException {
// Update a student in the database
String sql = "UPDATE student SET name = ?, gender = ?, age = ?, phone = ?, address = ? WHERE id = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, student.getName());
stmt.setString(2, student.getGender());
stmt.setInt(3, student.getAge());
stmt.setString(4, student.getPhone());
stmt.setString(5, student.getAddress());
stmt.setInt(6, student.getId());
stmt.executeUpdate();
}
public void close() throws SQLException {
// Close the database connection
conn.close();
}
}
```
最后,需要编写一个主程序,以实现在键盘上输入数字1、2、3等来进行学生信息的增加、删除和修改操作。以下是一个基本的示例代码:
```
import java.sql.SQLException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws SQLException {
StudentManagement sm = new StudentManagement();
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("1. Add student");
System.out.println("2. Delete student");
System.out.println("3. Update student");
System.out.println("4. Quit");
int choice = scanner.nextInt();
if (choice == 1) {
System.out.print("Enter student ID: ");
int id = scanner.nextInt();
System.out.print("Enter student name: ");
String name = scanner.next();
System.out.print("Enter student gender: ");
String gender = scanner.next();
System.out.print("Enter student age: ");
int age = scanner.nextInt();
System.out.print("Enter student phone number: ");
String phone = scanner.next();
System.out.print("Enter student address: ");
String address = scanner.next();
Student student = new Student(id, name, gender, age, phone, address);
sm.addStudent(student);
System.out.println("Student added successfully!");
}
else if (choice == 2) {
System.out.print("Enter student ID: ");
int id = scanner.nextInt();
sm.deleteStudent(id);
System.out.println("Student deleted successfully!");
}
else if (choice == 3) {
System.out.print("Enter student ID: ");
int id = scanner.nextInt();
System.out.print("Enter updated student name: ");
String name = scanner.next();
System.out.print("Enter updated student gender: ");
String gender = scanner.next();
System.out.print("Enter updated student age: ");
int age = scanner.nextInt();
System.out.print("Enter updated student phone number: ");
String phone = scanner.next();
System.out.print("Enter updated student address: ");
String address = scanner.next();
Student student = new Student(id, name, gender, age, phone, address);
sm.updateStudent(student);
System.out.println("Student updated successfully!");
}
else if (choice == 4) {
sm.close();
break;
}
else {
System.out.println("Invalid choice!");
}
}
}
}
```
这个程序仅仅是一个初稿,还有很多可以改进的地方。例如,可以添加输入验证和异常处理,以及实现更多的学生管理功能。
阅读全文