以下是一个示例的学生资源管理系统的Java代码,使用MySQL数据库完成增删改查功能:

import java.sql.*;

public class StudentManagementSystem {

    // 数据库连接参数
    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/student_db";
    private static final String JDBC_USERNAME = "root";
    private static final String JDBC_PASSWORD = "password";

    public static void main(String[] args) {
        try {
            // 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");

            // 建立数据库连接
            Connection connection = DriverManager.getConnection(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD);

            // 创建学生表(如果不存在)
            createStudentTable(connection);

            // 插入学生数据
            insertStudent(connection, 1, "张三", 18);
            insertStudent(connection, 2, "李四", 20);
            insertStudent(connection, 3, "王五", 19);

            // 查询所有学生信息
            System.out.println("查询所有学生信息:");
            selectAllStudents(connection);

            // 更新学生信息
            updateStudent(connection, 2, "李四", 21);

            // 查询指定学生信息
            System.out.println("查询指定学生信息:");
            selectStudent(connection, 2);

            // 删除学生
            deleteStudent(connection, 3);

            // 查询所有学生信息
            System.out.println("查询所有学生信息:");
            selectAllStudents(connection);

            // 关闭数据库连接
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 创建学生表
    private static void createStudentTable(Connection connection) throws SQLException {
        Statement statement = connection.createStatement();
        String sql = "CREATE TABLE IF NOT EXISTS students (id INT PRIMARY KEY, name VARCHAR(100), age INT)";
        statement.executeUpdate(sql);
        statement.close();
    }

    // 插入学生数据
    private static void insertStudent(Connection connection, int id, String name, int age) throws SQLException {
        String sql = "INSERT INTO students (id, name, age) VALUES (?, ?, ?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        preparedStatement.setString(2, name);
        preparedStatement.setInt(3, age);
        preparedStatement.executeUpdate();
        preparedStatement.close();
    }

    // 查询所有学生信息
    private static void selectAllStudents(Connection connection) throws SQLException {
        Statement statement = connection.createStatement();
        String sql = "SELECT * FROM students";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");
            System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
        }
        resultSet.close();
        statement.close();
    }

    // 查询指定学生信息
    private static void selectStudent(Connection connection, int id) throws SQLException {
        String sql = "SELECT * FROM students WHERE id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            String name = resultSet.getString("name");
            int age = resultSet.getInt("age");
            System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
        }
        resultSet.close();
        preparedStatement.close();
    }

    // 更新学生信息
    private static void updateStudent(Connection connection, int id, String name, int age) throws SQLException {
        String sql = "UPDATE students SET name = ?, age = ? WHERE id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, name);
        preparedStatement.setInt(2, age);
        preparedStatement.setInt(3, id);
        preparedStatement.executeUpdate();
        preparedStatement.close();
    }

    // 删除学生
    private static void deleteStudent(Connection connection, int id) throws SQLException {
        String sql = "DELETE FROM students WHERE id = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        preparedStatement.executeUpdate();
        preparedStatement.close();
    }
}

注意事项:

  1. 需要先安装并启动MySQL数据库,并创建一个名为student_db的数据库。
  2. 需要将JDBC_URLJDBC_USERNAMEJDBC_PASSWORD的值修改为你自己MySQL数据库的连接参数。
  3. 以上代码仅为示例,实际开发中可能需要根据具体需求进行修改和扩展
使用java数据库mysql完成一个学生资源管理系统有增删改查功能

原文地址: https://www.cveoy.top/t/topic/ihTD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录