用Java制作学生管理系统代码
时间: 2023-10-16 09:08:18 浏览: 69
以下是一个简单的基于 JDBC 和 JavaFX 的学生管理系统代码示例:
学生类:
```java
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;
}
// getter and setter methods
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
```
数据库连接类:
```java
public class DatabaseHandler {
private static Connection connection;
public static Connection getConnection() {
if (connection == null) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC";
String user = "root";
String password = "root";
connection = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
return connection;
}
public static void closeConnection() {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
界面设计:
```java
public class Main extends Application {
private TableView<Student> tableView;
private TableColumn<Student, Integer> idColumn;
private TableColumn<Student, String> nameColumn;
private TableColumn<Student, String> genderColumn;
private TableColumn<Student, Integer> ageColumn;
private TableColumn<Student, String> phoneColumn;
private TableColumn<Student, String> addressColumn;
private TextField idField;
private TextField nameField;
private TextField genderField;
private TextField ageField;
private TextField phoneField;
private TextField addressField;
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Student Management System");
primaryStage.setWidth(800);
primaryStage.setHeight(600);
tableView = new TableView<>();
idColumn = new TableColumn<>("ID");
nameColumn = new TableColumn<>("Name");
genderColumn = new TableColumn<>("Gender");
ageColumn = new TableColumn<>("Age");
phoneColumn = new TableColumn<>("Phone");
addressColumn = new TableColumn<>("Address");
tableView.getColumns().addAll(idColumn, nameColumn, genderColumn, ageColumn, phoneColumn, addressColumn);
idField = new TextField();
nameField = new TextField();
genderField = new TextField();
ageField = new TextField();
phoneField = new TextField();
addressField = new TextField();
Button addButton = new Button("Add");
Button updateButton = new Button("Update");
Button deleteButton = new Button("Delete");
Button searchButton = new Button("Search");
Button showAllButton = new Button("Show All");
addButton.setOnAction(event -> {
Student student = new Student(Integer.parseInt(idField.getText()), nameField.getText(), genderField.getText(),
Integer.parseInt(ageField.getText()), phoneField.getText(), addressField.getText());
addStudent(student);
});
updateButton.setOnAction(event -> {
Student student = new Student(Integer.parseInt(idField.getText()), nameField.getText(), genderField.getText(),
Integer.parseInt(ageField.getText()), phoneField.getText(), addressField.getText());
updateStudent(student);
});
deleteButton.setOnAction(event -> {
deleteStudent(Integer.parseInt(idField.getText()));
});
searchButton.setOnAction(event -> {
String keyword = nameField.getText();
searchStudent(keyword);
});
showAllButton.setOnAction(event -> {
showAllStudents();
});
VBox vBox = new VBox();
vBox.setSpacing(10);
vBox.setPadding(new Insets(10, 10, 10, 10));
vBox.getChildren().addAll(new Label("ID"), idField, new Label("Name"), nameField, new Label("Gender"), genderField,
new Label("Age"), ageField, new Label("Phone"), phoneField, new Label("Address"), addressField,
addButton, updateButton, deleteButton, searchButton, showAllButton, tableView);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.show();
}
private void addStudent(Student student) {
String sql = "INSERT INTO student(id, name, gender, age, phone, address) VALUES (?, ?, ?, ?, ?, ?)";
try {
PreparedStatement statement = DatabaseHandler.getConnection().prepareStatement(sql);
statement.setInt(1, student.getId());
statement.setString(2, student.getName());
statement.setString(3, student.getGender());
statement.setInt(4, student.getAge());
statement.setString(5, student.getPhone());
statement.setString(6, student.getAddress());
statement.executeUpdate();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void updateStudent(Student student) {
String sql = "UPDATE student SET name = ?, gender = ?, age = ?, phone = ?, address = ? WHERE id = ?";
try {
PreparedStatement statement = DatabaseHandler.getConnection().prepareStatement(sql);
statement.setString(1, student.getName());
statement.setString(2, student.getGender());
statement.setInt(3, student.getAge());
statement.setString(4, student.getPhone());
statement.setString(5, student.getAddress());
statement.setInt(6, student.getId());
statement.executeUpdate();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void deleteStudent(int id) {
String sql = "DELETE FROM student WHERE id = ?";
try {
PreparedStatement statement = DatabaseHandler.getConnection().prepareStatement(sql);
statement.setInt(1, id);
statement.executeUpdate();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void searchStudent(String keyword) {
String sql = "SELECT * FROM student WHERE name LIKE ?";
try {
PreparedStatement statement = DatabaseHandler.getConnection().prepareStatement(sql);
statement.setString(1, "%" + keyword + "%");
ResultSet resultSet = statement.executeQuery();
ObservableList<Student> students = FXCollections.observableArrayList();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String gender = resultSet.getString("gender");
int age = resultSet.getInt("age");
String phone = resultSet.getString("phone");
String address = resultSet.getString("address");
Student student = new Student(id, name, gender, age, phone, address);
students.add(student);
}
tableView.setItems(students);
resultSet.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void showAllStudents() {
String sql = "SELECT * FROM student";
try {
PreparedStatement statement = DatabaseHandler.getConnection().prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
ObservableList<Student> students = FXCollections.observableArrayList();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String gender = resultSet.getString("gender");
int age = resultSet.getInt("age");
String phone = resultSet.getString("phone");
String address = resultSet.getString("address");
Student student = new Student(id, name, gender, age, phone, address);
students.add(student);
}
tableView.setItems(students);
resultSet.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
```
这个示例代码只是一个简单的学生管理系统,仅供参考,实际应用中需要根据具体需求进行修改和优化。
阅读全文