用java和mysql做一个简单的系统
时间: 2024-01-11 09:06:18 浏览: 21
好的,我们可以做一个简单的学生管理系统。
首先,我们需要创建一个数据库,例如命名为`student_db`。
然后,我们需要创建一个名为`students`的表,用于存储学生的信息。
```sql
CREATE TABLE students (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT NOT NULL,
gender VARCHAR(10) NOT NULL,
PRIMARY KEY (id)
);
```
接下来,我们可以使用Java编写一个简单的控制台应用程序,用于实现学生管理系统的基本功能,例如添加学生、修改学生、删除学生和查询学生信息等。
```java
import java.sql.*;
public class StudentManagementSystem {
private static final String URL = "jdbc:mysql://localhost:3306/student_db";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
public static void main(String[] args) {
try {
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
System.out.println("Connected to the database.");
while (true) {
System.out.println("Please select an option:");
System.out.println("1. Add student");
System.out.println("2. Modify student");
System.out.println("3. Delete student");
System.out.println("4. Query student");
System.out.println("5. Exit");
int option = Integer.parseInt(System.console().readLine());
if (option == 1) {
System.out.println("Please enter the name of the student:");
String name = System.console().readLine();
System.out.println("Please enter the age of the student:");
int age = Integer.parseInt(System.console().readLine());
System.out.println("Please enter the gender of the student:");
String gender = System.console().readLine();
String sql = "INSERT INTO students (name, age, gender) VALUES (?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setInt(2, age);
statement.setString(3, gender);
int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("Student added successfully.");
}
} else if (option == 2) {
System.out.println("Please enter the ID of the student:");
int id = Integer.parseInt(System.console().readLine());
System.out.println("Please enter the new name of the student:");
String name = System.console().readLine();
System.out.println("Please enter the new age of the student:");
int age = Integer.parseInt(System.console().readLine());
System.out.println("Please enter the new gender of the student:");
String gender = System.console().readLine();
String sql = "UPDATE students SET name = ?, age = ?, gender = ? WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, name);
statement.setInt(2, age);
statement.setString(3, gender);
statement.setInt(4, id);
int rowsUpdated = statement.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("Student updated successfully.");
}
} else if (option == 3) {
System.out.println("Please enter the ID of the student:");
int id = Integer.parseInt(System.console().readLine());
String sql = "DELETE FROM students WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
int rowsDeleted = statement.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("Student deleted successfully.");
}
} else if (option == 4) {
System.out.println("Please enter the ID of the student:");
int id = Integer.parseInt(System.console().readLine());
String sql = "SELECT * FROM students WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
System.out.println("ID: " + resultSet.getInt("id"));
System.out.println("Name: " + resultSet.getString("name"));
System.out.println("Age: " + resultSet.getInt("age"));
System.out.println("Gender: " + resultSet.getString("gender"));
} else {
System.out.println("Student not found.");
}
} else if (option == 5) {
System.out.println("Exiting the system...");
break;
} else {
System.out.println("Invalid option.");
}
}
connection.close();
System.out.println("Disconnected from the database.");
} catch (SQLException e) {
System.out.println("SQL error occurred: " + e.getMessage());
}
}
}
```
这个程序的主要功能如下:
1. 连接到数据库
2. 提供用户界面,让用户选择要执行的操作
3. 根据用户选择的操作,执行相应的SQL语句
4. 处理SQL语句的执行结果
5. 关闭数据库连接
请注意,这只是一个简单的示例,实际应用程序可能需要更多的功能和更复杂的查询。此外,需要注意的是,为了使程序更健壮,需要添加适当的错误处理代码。