Java酒店客房管理系统数据库操作实例

本文将介绍如何使用Java代码动态操作数据库,实现酒店客房管理系统中常用的功能,并提供详细的代码示例。

1. 添加客房信息

public void addRoom(Room room) {
    // 连接数据库
    Connection connection = getConnection();
    
    // 准备SQL语句
    String sql = 'INSERT INTO rooms (room_number, room_type, price) VALUES (?, ?, ?)';
    
    try {
        // 创建PreparedStatement对象
        PreparedStatement statement = connection.prepareStatement(sql);
        
        // 设置参数
        statement.setInt(1, room.getRoomNumber());
        statement.setString(2, room.getRoomType());
        statement.setDouble(3, room.getPrice());
        
        // 执行SQL语句
        statement.executeUpdate();
        
        // 关闭连接和资源
        statement.close();
        connection.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

2. 更新客房信息

public void updateRoom(Room room) {
    // 连接数据库
    Connection connection = getConnection();
    
    // 准备SQL语句
    String sql = 'UPDATE rooms SET room_type = ?, price = ? WHERE room_number = ?';
    
    try {
        // 创建PreparedStatement对象
        PreparedStatement statement = connection.prepareStatement(sql);
        
        // 设置参数
        statement.setString(1, room.getRoomType());
        statement.setDouble(2, room.getPrice());
        statement.setInt(3, room.getRoomNumber());
        
        // 执行SQL语句
        statement.executeUpdate();
        
        // 关闭连接和资源
        statement.close();
        connection.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

3. 删除客房信息

public void deleteRoom(int roomNumber) {
    // 连接数据库
    Connection connection = getConnection();
    
    // 准备SQL语句
    String sql = 'DELETE FROM rooms WHERE room_number = ?';
    
    try {
        // 创建PreparedStatement对象
        PreparedStatement statement = connection.prepareStatement(sql);
        
        // 设置参数
        statement.setInt(1, roomNumber);
        
        // 执行SQL语句
        statement.executeUpdate();
        
        // 关闭连接和资源
        statement.close();
        connection.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

4. 查询客房信息

public Room getRoom(int roomNumber) {
    Room room = null;
    
    // 连接数据库
    Connection connection = getConnection();
    
    // 准备SQL语句
    String sql = 'SELECT * FROM rooms WHERE room_number = ?';
    
    try {
        // 创建PreparedStatement对象
        PreparedStatement statement = connection.prepareStatement(sql);
        
        // 设置参数
        statement.setInt(1, roomNumber);
        
        // 执行查询
        ResultSet resultSet = statement.executeQuery();
        
        // 处理查询结果
        if (resultSet.next()) {
            int number = resultSet.getInt('room_number');
            String type = resultSet.getString('room_type');
            double price = resultSet.getDouble('price');
            
            // 创建Room对象
            room = new Room(number, type, price);
        }
        
        // 关闭连接和资源
        resultSet.close();
        statement.close();
        connection.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    
    return room;
}

总结

本文介绍了如何使用Java代码实现酒店客房管理系统中常用的数据库操作,包括添加、更新、删除和查询客房信息。你可以根据实际需求进行适当的修改和扩展,例如查询所有客房信息、查询可用客房、预订客房等。

Java酒店客房管理系统数据库操作实例

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

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